Difference between revisions of "love.timer.sleep"

m
 
(10 intermediate revisions by 4 users not shown)
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.
 +
{{notice|This function causes the entire thread to pause for the duration of the sleep. Graphics will not draw, input events will not trigger, code will not run, and the window will be unresponsive if you use this as "wait()" in the main thread. Use [[love.update]] or a [[love.timer|Timer library]] for that instead.}}
 
== Function ==
 
== Function ==
{{newin|[[0.8.0]]|080|type=behaviour}}
+
{{newin|[[0.8.0]]|080|type=variant}}
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
Line 12: Line 13:
 
Nothing.
 
Nothing.
 
== Function ==
 
== Function ==
{{oldin|[[0.8.0]]|080|type=behaviour}}
+
{{oldin|[[0.8.0]]|080|type=variant}}
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
Line 22: Line 23:
 
Nothing.
 
Nothing.
 
== Examples ==
 
== Examples ==
=== Use sleep to cap FPS at 30 (in 0.7.2 or older) ===
+
=== Use sleep to cap FPS at 30 ===
 
<source lang="lua">
 
<source lang="lua">
 
function love.update(dt)
 
function love.update(dt)
 
   if dt < 1/30 then
 
   if dt < 1/30 then
       love.timer.sleep(1000 * (1/30 - dt))
+
       love.timer.sleep(1/30 - dt)
 
   end
 
   end
 
end
 
end
 
</source>
 
</source>
=== More sophisticated way to cap FPS (in 0.7.2 or older) ===
+
=== More sophisticated way to cap 30 FPS ===
 
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">
 
function love.load()
 
function love.load()
 
   min_dt = 1/30
 
   min_dt = 1/30
   next_time = love.timer.getMicroTime()
+
   next_time = love.timer.getTime()
 
end
 
end
  
Line 41: Line 42:
 
   next_time = next_time + min_dt
 
   next_time = next_time + min_dt
  
   --rest of function here
+
   -- rest of function here
 
end
 
end
  
 
function love.draw()
 
function love.draw()
   --rest of function here
+
   -- rest of function here
  
   local cur_time = love.timer.getMicroTime()
+
   local cur_time = love.timer.getTime()
 
   if next_time <= cur_time then
 
   if next_time <= cur_time then
 
       next_time = cur_time
 
       next_time = cur_time
 
       return
 
       return
 
   end
 
   end
   love.timer.sleep(1000*(next_time - cur_time))
+
   love.timer.sleep(next_time - cur_time)
 
end
 
end
 
</source>
 
</source>
 +
 
== See Also ==
 
== See Also ==
 
* [[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.}}
 +
{{#set:Since=021}}
 +
{{#set:PrettySince=0.2.1}}
 +
 
 
== Other Languages ==
 
== Other Languages ==
 
{{i18n|love.timer.sleep}}
 
{{i18n|love.timer.sleep}}

Latest revision as of 17:00, 27 August 2020

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.

O.png This function causes the entire thread to pause for the duration of the sleep. Graphics will not draw, input events will not trigger, code will not run, and the window will be unresponsive if you use this as "wait()" in the main thread. Use love.update or a Timer library for that instead.  


Function

Available since LÖVE 0.8.0
This variant 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 variant is not supported in that and later versions.

Synopsis

love.timer.sleep( ms )

Arguments

number ms
Milliseconds to sleep for.

Returns

Nothing.

Examples

Use sleep to cap FPS at 30

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

More sophisticated way to cap 30 FPS

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

function love.load()
   min_dt = 1/30
   next_time = love.timer.getTime()
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.getTime()
   if next_time <= cur_time then
      next_time = cur_time
      return
   end
   love.timer.sleep(next_time - cur_time)
end

See Also



Other Languages