Difference between revisions of "love.update"

m (add some comments)
m (Remove main thread notice, moved to `love` module.)
 
(6 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
Callback function used to update the state of the game every frame.
 
Callback function used to update the state of the game every frame.
 +
 
== Function ==
 
== Function ==
 
=== Synopsis ===
 
=== Synopsis ===
Line 14: Line 15:
 
dtotal = 0  -- this keeps track of how much time has passed
 
dtotal = 0  -- this keeps track of how much time has passed
 
function love.update(dt)
 
function love.update(dt)
  dtotal = dtotal + dt  -- we add the time passed since the last update, probably a very small number like 0.01
+
  dtotal = dtotal + dt  -- we add the time passed since the last update, probably a very small number like 0.01
  if dtotal > 1 then
+
  if dtotal >= 1 then
    dtotal = dtotal - 1  -- reduce our timer by a second, but don't discard the change... what if our framerate is 2/3 of a second?
+
      dtotal = dtotal - 1  -- reduce our timer by a second, but don't discard the change... what if our framerate is 2/3 of a second?
    npc.think()
+
      npc.think()
  end
+
  end
 
end
 
end
 
</source>
 
</source>
 
Change a variable ''var'' at a constant rate (+/- 3 per second in this example).
 
Change a variable ''var'' at a constant rate (+/- 3 per second in this example).
 
<source lang="lua">
 
<source lang="lua">
var = 10
+
var = 10   -- arbitrary starting value
rate = 3 -- change to change the rate at which the var is changed
+
rate = 3   -- change to change the rate at which the var is changed
 
function love.update(dt)
 
function love.update(dt)
   if love.keyboard.isDown("down") then
+
   if love.keyboard.isDown("down") then   -- reduce the value
 +
      var = var - (dt * rate)
 +
  end
 +
  if love.keyboard.isDown("up") then  -- increase the value
 
       var = var + (dt * rate)
 
       var = var + (dt * rate)
  elseif love.keyboard.isDown("up") then
 
      var = var - (dt * rate)
 
 
   end
 
   end
 
end
 
end
Line 36: Line 38:
 
* [[parent::love]]
 
* [[parent::love]]
 
* [[World:update]]
 
* [[World:update]]
 +
* [[variable]]
 
[[Category:Callbacks]]
 
[[Category:Callbacks]]
 
{{#set:Description=Callback function used to update the state of the game every frame.}}
 
{{#set:Description=Callback function used to update the state of the game every frame.}}
 +
{{#set:Subcategory=General}}
 
{{#set:Since=000}}
 
{{#set:Since=000}}
 
== Other Languages ==
 
== Other Languages ==
 
{{i18n|love.update}}
 
{{i18n|love.update}}

Latest revision as of 05:04, 28 March 2019

Callback function used to update the state of the game every frame.

Function

Synopsis

love.update( dt )

Arguments

number dt
Time since the last update in seconds.

Returns

Nothing.

Examples

Run a function called think inside a table called npc once per second.

dtotal = 0   -- this keeps track of how much time has passed
function love.update(dt)
   dtotal = dtotal + dt   -- we add the time passed since the last update, probably a very small number like 0.01
   if dtotal >= 1 then
      dtotal = dtotal - 1   -- reduce our timer by a second, but don't discard the change... what if our framerate is 2/3 of a second?
      npc.think()
   end
end

Change a variable var at a constant rate (+/- 3 per second in this example).

var = 10   -- arbitrary starting value
rate = 3   -- change to change the rate at which the var is changed
function love.update(dt)
   if love.keyboard.isDown("down") then   -- reduce the value
      var = var - (dt * rate)
   end
   if love.keyboard.isDown("up") then   -- increase the value
      var = var + (dt * rate)
   end
end

See Also


Other Languages