Difference between revisions of "Os.sleep"
From ComputerCraft Wiki
m (moved Os.sleep( timeout ) to Os.sleep) |
MKlegoman357 (Talk | contribs) m (Fixed/Expanded) |
||
(4 intermediate revisions by 3 users not shown) | |||
Line 2: | Line 2: | ||
{{Function | {{Function | ||
|name=os.sleep | |name=os.sleep | ||
− | |args=timeout | + | |returns={{type|nil}} |
− | |api= | + | |args={{type|number}} timeout |
+ | |api=OS | ||
|addon=ComputerCraft | |addon=ComputerCraft | ||
− | |desc=Pauses the computer for a number of seconds | + | |desc=Pauses the computer for a number of seconds. Fractions of a second are supported, but only down to a game tick, or 1/20 of a second (0.05s). Times are rounded up to the next tick, so <var>os.sleep(1.01)</var> and <var>os.sleep(1.05)</var> both wait for 1.05 seconds. You can also use <var>sleep( timeout )</var>. |
|examples= | |examples= | ||
{{Example | {{Example | ||
− | |desc=Pause for 3 seconds | + | |desc=Pause for 3 seconds. |
− | |code=os.sleep( 3 ) | + | |code='''os.sleep( 3 )''' |
}} | }} | ||
}} | }} | ||
− | + | ||
+ | == Concerns when using events == | ||
+ | The implementation of the sleep function is as follows: | ||
+ | |||
+ | function sleep( _nTime ) | ||
+ | local timer = [[os.startTimer]]( _nTime ) | ||
+ | |||
+ | repeat | ||
+ | local sEvent, param = [[os.pullEvent]]( "timer" ) | ||
+ | until param == timer | ||
+ | end | ||
+ | |||
+ | If you are using events and the sleep function at the same time, you may notice events received during the sleep never firing. The sleep function continually loops, emptying the event queue, until the [[timer_(event)|timer event]] fires. If you still want to sleep, you should create your own sleep function which takes care of the events while sleeping. One such function would be: | ||
+ | |||
+ | ''-- Replacement for sleep, which passes on events instead of dropping them'' | ||
+ | |||
+ | local function wait ( time ) | ||
+ | local timer = [[os.startTimer]](time) | ||
+ | |||
+ | while true do | ||
+ | local event = {[[os.pullEvent]]()} | ||
+ | |||
+ | if (event[1] == "timer" and event[2] == timer) then | ||
+ | break | ||
+ | else | ||
+ | processEvents(event) ''-- a custom function in which you would deal with received events'' | ||
+ | end | ||
+ | end | ||
+ | end | ||
+ | |||
+ | [[Category:Lua_Core_Functions]] |
Latest revision as of 22:46, 3 June 2014
Function os.sleep | |
Pauses the computer for a number of seconds. Fractions of a second are supported, but only down to a game tick, or 1/20 of a second (0.05s). Times are rounded up to the next tick, so os.sleep(1.01) and os.sleep(1.05) both wait for 1.05 seconds. You can also use sleep( timeout ). | |
Syntax | os.sleep(number timeout) |
Returns | nil |
Part of | ComputerCraft |
API | OS |
Examples
Example | |
Pause for 3 seconds. | |
Code |
os.sleep( 3 ) |
Concerns when using events
The implementation of the sleep function is as follows:
function sleep( _nTime ) local timer = os.startTimer( _nTime ) repeat local sEvent, param = os.pullEvent( "timer" ) until param == timer end
If you are using events and the sleep function at the same time, you may notice events received during the sleep never firing. The sleep function continually loops, emptying the event queue, until the timer event fires. If you still want to sleep, you should create your own sleep function which takes care of the events while sleeping. One such function would be:
-- Replacement for sleep, which passes on events instead of dropping them local function wait ( time ) local timer = os.startTimer(time) while true do local event = {os.pullEvent()} if (event[1] == "timer" and event[2] == timer) then break else processEvents(event) -- a custom function in which you would deal with received events end end end