This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
liljpac's profile picture

[Question] Starting a timer in the middle of code.

Started by liljpac, 15 October 2012 - 07:59 AM
liljpac #1
Posted 15 October 2012 - 09:59 AM
Hello pros!

I have been writing a program for a public crafting hub on my server.

So far the program takes a username and password, displayes the username onto a monitor then opens the iron door to let the user in.

I spent a few hours now trying to figure out how to display a timer after the user has entered the room and the door shuts. I want it to show how long the user has been in the crafting area.

My problem is that the timer would start in the middle of the code, but I find it stopping my code after it which waits for the user to input the password again to exit.

I guess I can tell the timer to stop after the user enters his password, wrong or right, but I don't know how to do that.

Thanks for the help!
Fatal_Exception #2
Posted 15 October 2012 - 10:58 AM
I sense a tall, dark stranger, the number 14, and someone whose name starts with 'K'. Wait, scratch that. I'm a terrible psychic. So are the rest of us. Sorry.
liljpac #3
Posted 15 October 2012 - 11:16 AM
I really didn't want to post the code because it is so messy :/

while true do

term.clear()

term.setCursorPos(1, 1)

mon = peripheral.wrap("top")
mon.clear()

local user
term.write("Enter a username: ")
user = read()

mon.setCursorPos(3, 1)
mon.write("User:".. user)
mon.setCursorPos(3, 2)
mon.write("Time:")

local password1
term.write("Enter a password: ")
password1 = read("*")

redstone.setOutput( "left", true)
term.clear()
term.setCursorPos(1, 1)
print("Closing the door in 5 seconds")
sleep(5)
redstone.setOutput( "left", false)

term.clear()
term.setCursorPos(1, 1)
local password2 = 1
print("Enter your password to open the door: ")

mon.setCursorPos(8, 2)

local time = 1

while password2==1 do
  time = time + 1
  password2 = read("*")
  mon.write(time)
  sleep(1)
end

while password1 ~= password2 do
if password1 == password2 then
  print("Correct.")
  redstone.setOutput("left", true)
  sleep(5)
  redstone.setOutput("left", false)
else
  print("wrong password.")
  sleep(1)
  term.clear()
  term.setCursorPos(1, 1)
  print("Enter your password to open the door: ")
  password2 = read("*")
end
end
--above is broken at the moment
end

I'm sure you are all face palming right now, and I am sure that the half of this is messed up, but to be fair I did have it working until I tried to add the timer into the code.
JoshhT #4
Posted 15 October 2012 - 11:48 AM
Well,

password2 = 1
-- Further down...
password2 = read("*")

Someone please, by all means, correct me if I'm wrong.
I don't think you can change a variable from an integer to a string?
And if you can, that's kind of cool.

But in regards to your problem.
I believe the read() method actually waits for user input.
I haven't looked at the exact code, but I assume it utilizes a while loop or something of the sort.
Doyle3694 #5
Posted 15 October 2012 - 11:50 AM
tostring()
JoshhT #6
Posted 15 October 2012 - 11:52 AM
tostring()
Without the use of such methods.

Can you do something like,

local errmahhgerrd = 9001

print(errmahhgerrd)

errmahhgerrd = "It's over 9000!")

print(errmahhgerrd)
Doyle3694 #7
Posted 15 October 2012 - 11:54 AM
yeah sure, variables in lua are variables, not specific to a type. a string can easily be changed to a int and then to a bool, sure
JoshhT #8
Posted 15 October 2012 - 11:57 AM
Lua > Java.

Back on topic though, the read() function waits for user input, which results in your timer stopping.