This article can be read independently or as a follow-up to the article How to Install the LÖVE 2D Game engine on a Raspberry Pi 4/400 In this article, I will present you with the basics of the LÖVE framework.
LÖVE uses different predefined callback functions, to serve different needs.
In programming, a callback function is called at a certain predefined time, opposed to an ordinary function, that does something the moment it is called. For example, the love.load function is called only once at the beginning of the code, and before any other callback, unlike the love.update function which is called continuously. In programming, functions are great to keep your code organized and efficient, or for reusability.
It is the main loop function love.run that takes care of executing all these callback functions. This function is handled by default by LÖVE and, in most cases, you do not need to declare it in your code.
The 3 main callback functions of LÖVE are:
function love.load() -- Your code here end
The love.load function is called only once at startup. This is where you habitually initialize variables and load your resources. You can do all these things anywhere else, but by doing them here you ensure that they are only executed once.
function love.update(dt) -- Your code here end
The love.update function is called continuously (repeatedly) and is where most of your operations and calculations are performed.
(dt) after the name of the callback function means delta time and represents the number of seconds since the last call of this function relative to the speed of your machine (speed of your computer / processor).
function love.draw() -- Your code here end
The love.draw function is dedicated to drawing operations, all functions of type love.graphics.draw must be called within it.
Like the love.update function, this function is also called continuously. All operations performed inside this function, and placed at the end, will affect those at the beginning.
All callback functions are optional, but most projects use at least the 3 basic callback functions above.
Skeleton of a LÖVE program
Here is what the code skeleton of a game / program in LÖVE looks like, once those 3 main callback functions are written together :
function love.load() -- Your code here end function love.update(dt) -- Your code here end function love.draw() -- Your code here end
In programming, we usually comment on some parts of our code, to remember or give information to others about what a specific part of the code do. With LÖVE and Lua, you can write a comment using "--" in front of a text/line. This element will be ignored by the program and not executed.
If you want additional information about the LÖVE framework and the Lua programming language, I invite you to consult the reference links below:
Programming in Lua