Difference between revisions of "love.timer.sleep"

(0.8.0 sample updated)
m (Edited description)
Line 1: Line 1:
 
{{newin|[[0.2.1]]|021|type=function}}
 
{{newin|[[0.2.1]]|021|type=function}}
Sleeps the program for the specified amount of time.
+
Pauses the current thread for the specified amount of time.
 
== Function ==
 
== Function ==
 
{{newin|[[0.8.0]]|080|type=behaviour}}
 
{{newin|[[0.8.0]]|080|type=behaviour}}
Line 66: Line 66:
 
* [[parent::love.timer]]
 
* [[parent::love.timer]]
 
[[Category:Functions]]
 
[[Category:Functions]]
{{#set:Description=Sleeps the program for the specified amount of time.}}
+
{{#set:Description=Pauses the current thread for the specified amount of time.}}
 
== Other Languages ==
 
== Other Languages ==
 
{{i18n|love.timer.sleep}}
 
{{i18n|love.timer.sleep}}

Revision as of 08:02, 25 October 2013

Available since LÖVE 0.2.1
This function is not supported in earlier versions.

Pauses the current thread for the specified amount of time.

Function

Available since LÖVE 0.8.0
This behaviour is not supported in earlier versions.

Synopsis

love.timer.sleep( s )

Arguments

number s
Seconds to sleep for.

Returns

Nothing.

Function

Removed in LÖVE 0.8.0
This behaviour is not supported in that and later versions.

Synopsis

love.timer.sleep( ms )

Arguments

number ms
Milliseconds to sleep for.

Returns

Nothing.

Examples

Follow the the 0.8.0 API for love.timer.sleep but fix the older version

local love_timer_sleep = love.timer.sleep -- in s (like >=0.8.0)
if love._version:find("^0%.[0-7]%.") then -- if version < 0.8.0
   -- love.timer.sleep in ms
   love_timer_sleep = function(s) love.timer.sleep(s*1000) end
end

Use sleep to cap FPS at 30 (in 0.8.0 or older)

function love.update(dt)
   if dt < 1/30 then
      love_timer_sleep(1/30 - dt)
   end
end

More sophisticated way to cap FPS (in 0.8.0 or older)

This takes into account the time spent updating and drawing each frame.

function love.load()
   min_dt = 1/30
   next_time = love.timer.getMicroTime()
end

function love.update(dt)
   next_time = next_time + min_dt

   --rest of function here
end

function love.draw()
   --rest of function here

   local cur_time = love.timer.getMicroTime()
   if next_time <= cur_time then
      next_time = cur_time
      return
   end
   love_timer_sleep(next_time - cur_time)
end

See Also

Other Languages