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

[Programming][Lua][Question] Making a timer?

Started by EmTeaKay, 18 October 2012 - 08:56 PM
EmTeaKay #1
Posted 18 October 2012 - 10:56 PM
How do I make a timer? I know you can make a timer with os.startTimer(x). But I want a timer where you can still type and do other stuff in the program. Anyone know how to do this? If so, please respond. Thank you in advance.
Noodle #2
Posted 18 October 2012 - 10:57 PM
It triggers an event
os.startTimer(1)
while true do
  event, p1 = os.pullEvent("timer")
  if event then
    break -- Or any code here
  end
end
EmTeaKay #3
Posted 18 October 2012 - 11:26 PM
This is great, except I can't type, let's say, a password. Other than that, this is great.
remiX #4
Posted 19 October 2012 - 02:25 PM
Edit: I actually don't know what you wanted ><
digpoe #5
Posted 19 October 2012 - 02:46 PM
So basically a timed system for entering a password? like if you don't enter the password in 30 sec you get locked out of the computer?

Hmmm.. I belive coroutine would help. Although, i've never used it before so I do not know how to use it.
faubiguy #6
Posted 19 October 2012 - 03:47 PM
Try using parallel (which uses coroutine, but handles them for you):
local input = ""
function timeOut()
	local timer = os.startTimer(30)
	while true do
		local event, param = os.pullEvent("timer")
		if param = timer then break end
	end
end

function getInput()
	input = read("*")
end

parallel.waitForAny(timeOut, getInput)
EmTeaKay #7
Posted 19 October 2012 - 08:37 PM
Thank you. I'll try this and get back to you on it. This should help.

EDIT: This does help! Thank you so much.