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

Check timer

Started by ScSEre, 07 April 2012 - 06:19 PM
ScSEre #1
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/>/>
MysticT #2
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
ScSEre #3
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?
MysticT #4
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.
ScSEre #5
Posted 08 April 2012 - 02:54 AM
Okay, so i hope they will add a way to check it.
Cloudy #6
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?
Hawk777 #7
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.
ScSEre #8
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.
MysticT #9
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
ScSEre #10
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
MysticT #11
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.