Difference between revisions of "Tutorial:Callback Functions"

(Tried to explain what callbacks are, and added example code for pausing game when focus is lost)
m (love.focus: Change "his" to "their")
 
(18 intermediate revisions by 10 users not shown)
Line 1: Line 1:
 
The [[:Category:Callbacks|callback]] functions in LÖVE are called by [[love.run]] to perform various tasks and are all optional. However, a fully-featured game experience would probably utilize nearly all of them, so it's wise to know what they are.
 
The [[:Category:Callbacks|callback]] functions in LÖVE are called by [[love.run]] to perform various tasks and are all optional. However, a fully-featured game experience would probably utilize nearly all of them, so it's wise to know what they are.
  
A callback, for those new to programming or otherwise unfamiliar with the term, is a function which works backwards in a sense. In a regular function like [[love.graphics.draw]] or math.floor, you call it and Love or Lua does something. A callback, on the other hand, is a function that you code and Love calls at certain times. This makes it easy to keep your code organized and optimal. For example, since love.load will only get called once when the game is first started (before any other callback), it's a fine place to put code which loads game content and otherwise prepares things.
+
A callback, for those new to programming or otherwise unfamiliar with the term, is a function which works backwards in a sense. In a regular function like [[love.graphics.draw]] or math.floor, you call it and LÖVE or Lua does something. A callback, on the other hand, is a function that you code and LÖVE calls at certain times. This makes it easy to keep your code organized and optimal. For example, since love.load will only get called once when the game is first started (before any other callback), it's a fine place to put code which loads game content and otherwise prepares things.
  
 
==[[love.load]]==
 
==[[love.load]]==
Line 7: Line 7:
 
function love.load()
 
function love.load()
 
   image = love.graphics.newImage("cake.jpg")
 
   image = love.graphics.newImage("cake.jpg")
   local f = love.graphics.newFont(12)
+
   love.graphics.setNewFont(12)
  love.graphics.setFont(f)
+
   love.graphics.setColor(0,0,0)
   love.graphics.setColor(0,0,0,255)
 
 
   love.graphics.setBackgroundColor(255,255,255)
 
   love.graphics.setBackgroundColor(255,255,255)
 
end
 
end
Line 32: Line 31:
 
end
 
end
 
</source>
 
</source>
<code>[[love.draw]]</code> is where all the drawing happens (if that wasn't obvious enough already) and if you call any of the <code>[[love.graphics.draw]]</code> outside of this function then it's not going to have any effect. This function is also called continuously so keep in mind that if you change the font/color/mode/etc at the end of the function then it will have a effect on things at the beginning of the function. For example:  
+
<code>[[love.draw]]</code> is where all the drawing happens (if that wasn't obvious enough already) and if you call any of the <code>[[love.graphics.draw]]</code> outside of this function then it's not going to have any effect. This function is also called continuously so keep in mind that if you change the font/color/mode/etc at the end of the function then it will have an effect on things at the beginning of the function. For example:  
 
<source lang="lua">
 
<source lang="lua">
 
function love.load()
 
function love.load()
Line 46: Line 45:
  
 
==[[love.mousepressed]]==
 
==[[love.mousepressed]]==
 +
{{newin|[[0.10.0]]|100|type=variant}}
 
<source lang="lua">
 
<source lang="lua">
function love.mousepressed(x, y, button)
+
function love.mousepressed(x, y, button, istouch)
   if button == 'l' then
+
   if button == 1 then
 
       imgx = x -- move image to where mouse clicked
 
       imgx = x -- move image to where mouse clicked
 
       imgy = y
 
       imgy = y
Line 54: Line 54:
 
end
 
end
 
</source>
 
</source>
This function is called whenever a mouse button is pressed and it receives the button and the coordinates of where it was pressed. The button can be any of the [[MouseConstant|constants]]. This function goes very well along with <code>[[love.mousereleased]]</code>.  
+
This function is called whenever a mouse button is pressed and it receives the button and the coordinates of where it was pressed. The button can be any of the button index that was pressed. This function goes very well along with <code>[[love.mousereleased]]</code>.
  
 
==[[love.mousereleased]]==
 
==[[love.mousereleased]]==
 +
{{newin|[[0.10.0]]|100|type=variant}}
 
<source lang="lua">
 
<source lang="lua">
function love.mousereleased(x, y, button)
+
function love.mousereleased(x, y, button, istouch)
   if button == 'l' then
+
   if button == 1 then
 
       fireSlingshot(x,y) -- this totally awesome custom function is defined elsewhere
 
       fireSlingshot(x,y) -- this totally awesome custom function is defined elsewhere
 
   end
 
   end
 
end
 
end
 
</source>
 
</source>
This function is called whenever a mouse button is released and it receives the button and the coordinates of where it was released. You can have this function together with <code>[[love.mousepressed]]</code> or separate, they aren't connected in any way.  
+
This function is called whenever a mouse button is released and it receives the button and the coordinates of where it was released. You can have this function together with <code>[[love.mousepressed]]</code> or separate, they aren't connected in any way.
 
 
  
 
==[[love.keypressed]]==
 
==[[love.keypressed]]==
 
<source lang="lua">
 
<source lang="lua">
function love.keypressed(key, unicode)
+
function love.keypressed(key)
 
   if key == 'b' then
 
   if key == 'b' then
 
       text = "The B key was pressed."
 
       text = "The B key was pressed."
Line 81: Line 81:
 
==[[love.keyreleased]]==
 
==[[love.keyreleased]]==
 
<source lang="lua">
 
<source lang="lua">
function love.keyreleased(key, unicode)
+
function love.keyreleased(key)
 
   if key == 'b' then
 
   if key == 'b' then
 
       text = "The B key was released."
 
       text = "The B key was released."
Line 101: Line 101:
 
end
 
end
 
</source>
 
</source>
This function is called whenever the user clicks off and on the LOVE window. For instance, if he is playing a windowed game and a user clicks on his Internet browser, the game could be notified and automatically pause the game.
+
This function is called whenever the user clicks off and on the LÖVE window. For instance, if they are playing a windowed game and a user clicks on their Internet browser, the game could be notified and automatically pause the game.
 
<source lang="lua">
 
<source lang="lua">
 
function love.focus(f) gameIsPaused = not f end
 
function love.focus(f) gameIsPaused = not f end
Line 118: Line 118:
 
end
 
end
 
</source>
 
</source>
This function is called whenever the user clicks the windows close button (often an X). For instance, if the user decides he is done playing, he could click the close button. Then, before it closes, the game can save its state.
+
This function is called whenever the user clicks the window's close button (often an X). For instance, if the user decides they are done playing, they could click the close button. Then, before it closes, the game can save its state.
  
 
Those are the callback functions and their basic usage.
 
Those are the callback functions and their basic usage.
 
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]
 
 
{{#set:LOVE Version=0.6.0}}
 
{{#set:LOVE Version=0.6.0}}
 
{{#set:Description=Callback Functions}}
 
{{#set:Description=Callback Functions}}
 
 
== Other languages ==
 
== Other languages ==
 
{{i18n|Tutorial:Callback Functions}}
 
{{i18n|Tutorial:Callback Functions}}

Latest revision as of 17:07, 14 November 2024

The callback functions in LÖVE are called by love.run to perform various tasks and are all optional. However, a fully-featured game experience would probably utilize nearly all of them, so it's wise to know what they are.

A callback, for those new to programming or otherwise unfamiliar with the term, is a function which works backwards in a sense. In a regular function like love.graphics.draw or math.floor, you call it and LÖVE or Lua does something. A callback, on the other hand, is a function that you code and LÖVE calls at certain times. This makes it easy to keep your code organized and optimal. For example, since love.load will only get called once when the game is first started (before any other callback), it's a fine place to put code which loads game content and otherwise prepares things.

love.load

function love.load()
   image = love.graphics.newImage("cake.jpg")
   love.graphics.setNewFont(12)
   love.graphics.setColor(0,0,0)
   love.graphics.setBackgroundColor(255,255,255)
end

This function gets called only once, when the game is started, and is usually where you would load resources, initialize variables and set specific settings. All those things can be done anywhere else as well, but doing them here means that they are done once only, saving a lot of system resources.

love.update

function love.update(dt)
   if love.keyboard.isDown("up") then
      num = num + 100 * dt -- this would increment num by 100 per second
   end
end

This function is called continuously and will probably be where most of your math is done. 'dt' stands for "delta time" and is the amount of seconds since the last time this function was called (which is usually a small value like 0.025714).

love.draw

function love.draw()
   love.graphics.draw(image, imgx, imgy)
   love.graphics.print("Click and drag the cake around or use the arrow keys", 10, 10)
end

love.draw is where all the drawing happens (if that wasn't obvious enough already) and if you call any of the love.graphics.draw outside of this function then it's not going to have any effect. This function is also called continuously so keep in mind that if you change the font/color/mode/etc at the end of the function then it will have an effect on things at the beginning of the function. For example:

function love.load()
   love.graphics.setColor(0,0,0)
end

function love.draw()
   love.graphics.print("This text is not black because of the line below", 100, 100)
   love.graphics.setColor(255,0,0)
   love.graphics.print("This text is red", 100, 200)
end

love.mousepressed

Available since LÖVE 0.10.0
This variant is not supported in earlier versions.
function love.mousepressed(x, y, button, istouch)
   if button == 1 then
      imgx = x -- move image to where mouse clicked
      imgy = y
   end
end

This function is called whenever a mouse button is pressed and it receives the button and the coordinates of where it was pressed. The button can be any of the button index that was pressed. This function goes very well along with love.mousereleased.

love.mousereleased

Available since LÖVE 0.10.0
This variant is not supported in earlier versions.
function love.mousereleased(x, y, button, istouch)
   if button == 1 then
      fireSlingshot(x,y) -- this totally awesome custom function is defined elsewhere
   end
end

This function is called whenever a mouse button is released and it receives the button and the coordinates of where it was released. You can have this function together with love.mousepressed or separate, they aren't connected in any way.

love.keypressed

function love.keypressed(key)
   if key == 'b' then
      text = "The B key was pressed."
   elseif key == 'a' then
      a_down = true
   end
end

This function is called whenever a keyboard key is pressed and receives the key that was pressed. The key can be any of the constants. This functions goes very well along with love.keyreleased.

love.keyreleased

function love.keyreleased(key)
   if key == 'b' then
      text = "The B key was released."
   elseif key == 'a' then
      a_down = false
   end
end

This function is called whenever a keyboard key is released and receives the key that was released. You can have this function together with love.keypressed or separate, they aren't connected in any way.

love.focus

function love.focus(f)
  if not f then
    print("LOST FOCUS")
  else
    print("GAINED FOCUS")
  end
end

This function is called whenever the user clicks off and on the LÖVE window. For instance, if they are playing a windowed game and a user clicks on their Internet browser, the game could be notified and automatically pause the game.

function love.focus(f) gameIsPaused = not f end

function love.update(dt)
	if gameIsPaused then return end

	-- The rest of your love.update code goes here
end

love.quit

function love.quit()
  print("Thanks for playing! Come back soon!")
end

This function is called whenever the user clicks the window's close button (often an X). For instance, if the user decides they are done playing, they could click the close button. Then, before it closes, the game can save its state.

Those are the callback functions and their basic usage.


Other languages