Difference between revisions of "love.timer.sleep"

(This change is for 0.8.0, which is still in development.)
(0.8.0 changes)
Line 9: Line 9:
 
=== Returns ===
 
=== Returns ===
 
Nothing.
 
Nothing.
 +
 +
{{newin|[[0.8.0]]|080|type=function}}
 +
=== Synopsis ===
 +
<source lang="lua">
 +
love.timer.sleep( s )
 +
</source>
 +
=== Arguments ===
 +
{{param|number|s|Seconds to sleep for.}}
 +
=== Returns ===
 +
Nothing.
 +
 
== Examples ==
 
== Examples ==
=== Use sleep to cap FPS at 30 ===
+
=== Use sleep to cap FPS at 30 (in 0.7.2 or older) ===
 
<source lang="lua">
 
<source lang="lua">
 
function love.update(dt)
 
function love.update(dt)
Line 18: Line 29:
 
end
 
end
 
</source>
 
</source>
=== More sophisticated way to cap FPS ===
+
=== More sophisticated way to cap FPS (in 0.7.2 or older) ===
 
This takes into account the time spent updating and drawing each frame.
 
This takes into account the time spent updating and drawing each frame.
 
<source lang="lua">
 
<source lang="lua">

Revision as of 17:19, 14 November 2011

Sleeps the program for the specified amount of time.

Function

Synopsis

love.timer.sleep( ms )

Arguments

number ms
Milliseconds to sleep for.

Returns

Nothing.

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

Synopsis

love.timer.sleep( s )

Arguments

number s
Seconds to sleep for.

Returns

Nothing.

Examples

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

function love.update(dt)
   if dt < 1/30 then
      love.timer.sleep(1000 * (1/30 - dt))
   end
end

More sophisticated way to cap FPS (in 0.7.2 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(1000*(next_time - cur_time))
end

See Also


Other Languages