9 posts
Posted 07 April 2012 - 08:19 PM
Hi guys,
is there a way to check whether a timer has gone off?
hope someone can help me and sorry for my bad english, it's not my native language.
Thanks in advance :P/>/>
1604 posts
Posted 07 April 2012 - 11:00 PM
The timers trigger an event when the time you set passes, so you have to use os.pullEvent() to get the "timer" event.
Example:
local timer = os.startTimer(5)
while true do
local evt, arg = os.pullEvent()
if evt == "timer" and arg == timer then
print("Time's up")
break
end
end
9 posts
Posted 08 April 2012 - 02:27 AM
The timers trigger an event when the time you set passes, so you have to use os.pullEvent() to get the "timer" event.
Example:
local timer = os.startTimer(5)
while true do
local evt, arg = os.pullEvent()
if evt == "timer" and arg == timer then
print("Time's up")
break
end
end
Thanks for your reply, but is there a way to check that the timer has gone off after the event happened?
1604 posts
Posted 08 April 2012 - 02:38 AM
mmm, it would be good to be able to do that. I think there's no way now, but it's a good suggestion to do.
9 posts
Posted 08 April 2012 - 02:54 AM
Okay, so i hope they will add a way to check it.
2447 posts
Posted 08 April 2012 - 03:45 AM
The timers trigger an event when the time you set passes, so you have to use os.pullEvent() to get the "timer" event.
Example:
local timer = os.startTimer(5)
while true do
local evt, arg = os.pullEvent()
if evt == "timer" and arg == timer then
print("Time's up")
break
end
end
Thanks for your reply, but is there a way to check that the timer has gone off after the event happened?
What does that even mean?
Edit: Scratch that, I get it now. It is much better just to check for the timer event as it happens, or look below. Is there any reason you can't catch the timer event in your code?
161 posts
Posted 08 April 2012 - 08:50 AM
You could wrap timer functionality in your own library that keeps track of all the timers that haven’t fired yet in a table. Your application would fall naturally out of that. I actually do it in my own code for two other reasons. First, it allows me to stash a function in the table entry and have a central dispatching point that automatically calls the function when the timer goes off without the central dispatching point needing to know precisely all the timers that might fire and the functions to call. Second, it allows ”cancelling” a timer by removing it from the table; it will still fire, but the dispatching point will have no table entry so it will know to ignore the event.
9 posts
Posted 09 April 2012 - 11:19 PM
The timers trigger an event when the time you set passes, so you have to use os.pullEvent() to get the "timer" event.
Example:
local timer = os.startTimer(5)
while true do
local evt, arg = os.pullEvent()
if evt == "timer" and arg == timer then
print("Time's up")
break
end
end
Thanks for your reply, but is there a way to check that the timer has gone off after the event happened?
What does that even mean?
Edit: Scratch that, I get it now. It is much better just to check for the timer event as it happens, or look below. Is there any reason you can't catch the timer event in your code?
Yes, my problem is: there is a redstone event and a timer event that may occur at the same time, so i have to check whether the timer has gone off after a event happened.
1604 posts
Posted 09 April 2012 - 11:36 PM
Yes, my problem is: there is a redstone event and a timer event that may occur at the same time, so i have to check whether the timer has gone off after a event happened.
And why can't you check for the "timer" event
and the "redstone" event?
Example:
local timer = os.startTimer(10)
while true do
local evt, arg = os.pullEvent()
if evt == "redstone" then
do_redstone_thing()
elseif evt == "timer" then
if arg == timer then
print("10 seconds passed!")
timer = os.startTimer(10)
end
end
end
9 posts
Posted 11 April 2012 - 06:26 PM
Yes, my problem is: there is a redstone event and a timer event that may occur at the same time, so i have to check whether the timer has gone off after a event happened.
And why can't you check for the "timer" event
and the "redstone" event?
Example:
local timer = os.startTimer(10)
while true do
local evt, arg = os.pullEvent()
if evt == "redstone" then
do_redstone_thing()
elseif evt == "timer" then
if arg == timer then
print("10 seconds passed!")
timer = os.startTimer(10)
end
end
end
But i need to use a delay so the timer can go off during the delay
1604 posts
Posted 11 April 2012 - 08:00 PM
But i need to use a delay so the timer can go off during the delay
If you just need to wait some time before doing anything else, use sleep:
sleep(10) -- will wait 10 seconds and then continue
Timers are for when you need to do something after some time, but you need to do something else while waiting.