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

3 seconds to make input

Started by theuhmu, 01 December 2012 - 02:49 AM
theuhmu #1
Posted 01 December 2012 - 03:49 AM
cant get this code to work for two days XD

its about to press any key during 3 secs or the timer fires:

print("KEY OR WAIT")
while true do
local event, arg1 = os.pullEvent()
local timerID = os.startTimer(3)
if event == "timer" and arg1 == timerID then
print("My timer was first!")
return
end
if event == "key" then
print("Key was first")
return
end
end

k input works fine but timer does not fire .Can anyone help?
Kingdaro #2
Posted 01 December 2012 - 04:00 AM
You have to start the timer before pulling the event.
faubiguy #3
Posted 01 December 2012 - 04:02 AM
Also, if the call to os.startTimer is in the loop, the timer will get reset each time it pulls an event.
theuhmu #4
Posted 01 December 2012 - 04:03 AM
print("KEY OR WAIT")
local timerID = os.startTimer(3)
while true do
local event, arg1 = os.pullEvent()
if event == "timer" and arg1 == timerID then
print("My timer was first!")
return
end
if event == "key" then
if arg1 == 45 <—————— got it (push x)
print("Key was first")
return
end
end
end