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

[Question] Timer

Started by Matrixmage, 01 September 2012 - 08:36 PM
Matrixmage #1
Posted 01 September 2012 - 10:36 PM
Hi, I probably already know this but I forgot, but what is the code to use a timer, I'm pretty sure that the os API has one, but there might be others, I'm not sure…
MysticT #2
Posted 01 September 2012 - 10:39 PM
Just use:

os.startTimer(time)
and then wait for a "timer" event.

A quick look at the wiki would have answered this.
Matrixmage #3
Posted 01 September 2012 - 10:41 PM
Just use:

os.startTimer(time)
and then wait for a "timer" event.

A quick look at the wiki would have answered this.

Thanks, I had already looked on the wiki, but I couldn't find it for the life of me xD
Matrixmage #4
Posted 01 September 2012 - 11:11 PM
Just use:

os.startTimer(time)
and then wait for a "timer" event.

A quick look at the wiki would have answered this.
I just checked the wiki and it says that it makes a table to show when its done, I don't really know anything about tables, could I have an example of the code that detects when the timer is over? Or can I use a os.pullEvent?
MysticT #5
Posted 01 September 2012 - 11:15 PM
The table is just used as an identifier for the timer, you don't need to use it. Just wait for a "timer" event (using os.pullEvent) and then check if the argument of the event equals the table returned by os.startTimer.
Example:

local timer = os.startTimer(5) -- start a timer for 5 seconds
while true do
  local evt, arg = os.pullEvent("timer") -- wait for a timer event
  if arg == timer then -- check if the timer fired is the one started before
    print("Time's up!")
    break
  end
end
Matrixmage #6
Posted 01 September 2012 - 11:19 PM
The table is just used as an identifier for the timer, you don't need to use it. Just wait for a "timer" event (using os.pullEvent) and then check if the argument of the event equals the table returned by os.startTimer.
Example:

local timer = os.startTimer(5) -- start a timer for 5 seconds
while true do
  local evt, arg = os.pullEvent("timer") -- wait for a timer event
  if arg == timer then -- check if the timer fired is the one started before
	print("Time's up!")
	break
  end
end

Ok, thanks for the help!