Difference between revisions of "Os.sleep"

From ComputerCraft Wiki
Jump to: navigation, search
m (Fixed/Expanded)
 
Line 2: Line 2:
 
{{Function
 
{{Function
 
|name=os.sleep
 
|name=os.sleep
|args=timeout
+
|returns={{type|nil}}
 +
|args={{type|number}} timeout
 
|api=OS
 
|api=OS
 
|addon=ComputerCraft
 
|addon=ComputerCraft
|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 os.sleep(1.01) and os.sleep(1.05) both wait for 1.05 seconds. You can also use sleep( timeout ).
+
|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 )'''
 
}}
 
}}
 
}}
 
}}
Line 15: Line 16:
 
== Concerns when using events ==
 
== Concerns when using events ==
 
The implementation of the sleep function is as follows:
 
The implementation of the sleep function is as follows:
<pre>
 
function sleep( _nTime )
 
    local timer = os.startTimer( _nTime )
 
repeat
 
local sEvent, param = os.pullEvent( "timer" )
 
until param == timer
 
end
 
</pre>
 
  
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:
+
function sleep( _nTime )
<pre>
+
  local timer = [[os.startTimer]]( _nTime )
-- Replacement for sleep(s), which passes on events instead of dropping them
+
 
function wait(s)
+
  repeat
    local timer = os.startTimer(s)
+
    local sEvent, param = [[os.pullEvent]]( "timer" )
    while true do
+
  until param == timer
        local event = {os.pullEvent()}
+
end
        if (event[1] == "timer" and event[2] == timer) then
+
            break
+
        else
+
            processMeessages(event)
+
        end
+
    end
+
end
+
</pre>
+
  
 +
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]]
 
[[Category:Lua_Core_Functions]]

Latest revision as of 22:46, 3 June 2014


Grid Redstone.png  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

Grid paper.png  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