Difference between revisions of "Os.startTimer"
From ComputerCraft Wiki
(it returns an int not a table) |
Bomb Bloke (Talk | contribs) m |
||
| (9 intermediate revisions by 4 users not shown) | |||
| Line 1: | Line 1: | ||
{{lowercase}} | {{lowercase}} | ||
{{Function | {{Function | ||
| − | + | |name=os.startTimer | |
| − | + | |args={{type|number}} time | |
| − | + | |api=OS | |
| − | + | |returns={{type|number}} timerID | |
| − | + | |addon=ComputerCraft | |
| − | + | |desc=Adds a timer which will fire a single [[Timer_(event)|"timer" event]] after <var>time</var> seconds have passed. It also returns a number representing the unique ID of the timer (which will likewise be included with said event, allowing you to identify it when you [[os.pullEvent|pull it]]).<br><br> | |
| − | + | ||
| + | Fractions of a second are supported, but only down to a game tick, or 1/20th of a second (0.05s) - note that even a timer set for 0 seconds will not fire until at least a tick has passed. Likewise, given that [[os.sleep|sleep()]] relies on timer events to function (as do other functions that can wait for a set period of time - eg [[rednet.receive|rednet.receive()]]), sleep(1.01) and sleep(1.05) would both wait for at least 1.05 seconds. | ||
| + | |examples= | ||
| + | {{Example | ||
| + | |desc=Yields until 3 seconds have passed. | ||
| + | |code= local myTimer = os.startTimer(3) | ||
| + | while true do | ||
| + | local event, timerID = os.pullEvent("timer") | ||
| + | if timerID == myTimer then break end | ||
| + | end | ||
| + | }} | ||
| + | {{Example | ||
| + | |desc=Waits until 10 seconds have passed, or a key is pressed - whichever happens first. | ||
| + | |code= print("Press any key to continue...") | ||
| + | |||
| + | local myTimer = os.startTimer(10) | ||
| + | |||
| + | while true do | ||
| + | local event, par1 = os.pullEvent() | ||
| + | |||
| + | if event == "timer" and par1 == myTimer then | ||
| + | print("I'm sick of waiting!") | ||
| + | break | ||
| + | elseif event == "key" then | ||
| + | print("You pressed "..keys.getName(par1).."!") | ||
| + | break | ||
| + | end | ||
| + | end | ||
| + | }} | ||
}} | }} | ||
[[Category:Lua_Core_Functions]] | [[Category:Lua_Core_Functions]] | ||
Latest revision as of 13:24, 17 December 2014
| Adds a timer which will fire a single "timer" event after time seconds have passed. It also returns a number representing the unique ID of the timer (which will likewise be included with said event, allowing you to identify it when you pull it). Fractions of a second are supported, but only down to a game tick, or 1/20th of a second (0.05s) - note that even a timer set for 0 seconds will not fire until at least a tick has passed. Likewise, given that sleep() relies on timer events to function (as do other functions that can wait for a set period of time - eg rednet.receive()), sleep(1.01) and sleep(1.05) would both wait for at least 1.05 seconds. | |
| Syntax | os.startTimer(number time) |
| Returns | number timerID |
| Part of | ComputerCraft |
| API | OS |
Examples
| Yields until 3 seconds have passed. | |
| Code |
local myTimer = os.startTimer(3)
while true do
local event, timerID = os.pullEvent("timer")
if timerID == myTimer then break end
end
|