Jump to content




[Question] Timer


  • You cannot reply to this topic
5 replies to this topic

#1 Matrixmage

  • Members
  • 116 posts
  • LocationAt my Computer coding for the Computer in my Computer

Posted 01 September 2012 - 09: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...

#2 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 01 September 2012 - 09:39 PM

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

A quick look at the wiki would have answered this.

#3 Matrixmage

  • Members
  • 116 posts
  • LocationAt my Computer coding for the Computer in my Computer

Posted 01 September 2012 - 09:41 PM

View PostMysticT, on 01 September 2012 - 09:39 PM, said:

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

#4 Matrixmage

  • Members
  • 116 posts
  • LocationAt my Computer coding for the Computer in my Computer

Posted 01 September 2012 - 10:11 PM

View PostMysticT, on 01 September 2012 - 09:39 PM, said:

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?

#5 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 01 September 2012 - 10: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


#6 Matrixmage

  • Members
  • 116 posts
  • LocationAt my Computer coding for the Computer in my Computer

Posted 01 September 2012 - 10:19 PM

View PostMysticT, on 01 September 2012 - 10:15 PM, said:

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!





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users