LÖVE is an unquestionably awesome 2D game framework, which allows rapid game development and prototyping in Lua.

Report article RSS Feed Callback Functions

This tutorials explains how to use the global callback functions in LÖVE.

Posted by thelinx on Mar 13th, 2009
Basic Client Side Coding.

This tutorial is taken straight from the Wiki.


The callback functions in LÖVE are used by the engine 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.

love.load

lua code:
function love.load()
   image = love.graphics.newImage("cake.jpg")
   local f = love.graphics.newFont(12)
   love.graphics.setFont(f)
   love.graphics.setColor(0,0,0,255)
   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

lua code:
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.0025714).

love.draw

lua code:
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 a effect on things at the beginning of the function. For example:

lua code:
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

lua code:
function love.mousepressed(x, y, button)
   if button == 'l' 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 constants. This function goes very well along with love.mousereleased.

love.mousereleased

lua code:
function love.mousereleased(x, y, button)
   if button == 'l' 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

lua code:
function love.keypressed(key, unicode)
   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

lua code:
function love.keyreleased(key, unicode)
   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.

Those are the callback functions and their basic usage.


You can view more tutorials like this at the Wiki!

Post a Comment
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

Platforms
Windows, Mac, Linux
Contact
Send Message
Official Page
Love2d.org
Licence
Zlib
Release Date
Released Jan 13, 2008
Engine Watch
Track this engine
Tutorial
Browse
Tutorials
Report Abuse
Report article
Related Engines
LÖVE
LÖVE Zlib Released Jan 13, 2008
Related Groups
LÖVE Developers
LÖVE Developers Developer & Publisher with 5 members