Skip to content
Reycko edited this page Aug 8, 2022 · 6 revisions

index | 0b Tables »

This is a very short lesson about basic Lua and LÖVE concepts. You should start here if you don’t know very well what is LÖVE, or Lua.

LÖVE

First, you should know what LÖVE is. LÖVE is an engine – a group of libraries. It is designed to be used in 2d game development.

Second, if you haven’t already done it, you will need to install it. There are installation executables on the LÖVE website

When using LÖVE on windows, it is highly recommended that you add the folder containing the love.exe file to your PATH variable.

If you are unsure about whether you have it already, you can test it by:

  1. Opening a console
  2. Executing the love command.

If you see the LÖVE “no application” animation, you already have LÖVE in your PATH. Otherwise, follow these instructions and include the folder with the love.exe file in your path.

Lua

Lua is the language you use to tell LÖVE to do things. Lua is not LÖVE-exclusive; it’s used in other projects, too.

LÖVE comes with its own version of Lua; you don’t need to install Lua separatedly.

main.lua

If you want to create a LÖVE game, the first thing you have to do is creating a folder for it in your hard drive.

Create a single empty file inside that folder and name it main.lua.

Copy and paste the following inside that file.

function love.load()
  Message = "Hello from LOVE"
  local secret = 'This is a local string' -- local variable, apostrophes on the string.
end

function love.draw()
  love.graphics.print(Message, 200, 200)
end

Save main.lua, then open a console and execute the following:

cd /the/folder/where/main.lua/is
love .

Hopefully, you will see a black window with a message on it.

Let’s analyze what those lines mean.

love.load

function love.load()

This is defining a function called love.load. love.load, if defined, will be called by LÖVE before it draws anything on the screen.

It is normally used for loading images, sounds, etc, that later on will be available on the game.

The function definition starts on that line and ends on the end key, two lines below it. Everything between those lines is called the function body.

Global variables

Message = "Hello from LOVE"

This line is part of the body of love.load – it is highly recommended adding some identation to denote that (I use 2 spaces).

The line itself is creating a global variable called Message and assigning the string "Hello from LOVE" to it. Later on, this string can be “recovered” by writing Message in the code.

Message is a global variable; for the scope of this tutorial, you can assume that that means that it is available “globally” – this is, you can use it whenever you want, in all the Lua code that you write, after creating it.

Local variables

local secret = 'This is a local string' -- local variable, apostrophes on the string.

This line is also part of love.load‘s body. It’s creating a variable called secret and assigning it the string 'This is a local string'.

You may have noticed that @secret@’s string is delimited by apostrophes (’) instead of quotes ("). In Lua, you can use both to delimit strings (but if you start a string with an apostrophe, it must end with an apostrophe, and viceversa).

secret is also a local variable. This means that it is only ‘available’ inside love.load. Once the function ends, the variable will be discarded, as opposed to Message, which will be conserved.

In our code, local variables will have lowercase names (secret) while global variables will be uppercase (Message).

Comments

The text starting with -- is a comment. Comments are ignored by Lua. They are meant to be used as clarifications for the human programmer.

end

end marks the ending of the love.load function declaration.

love.draw

function love.draw()

Another key function in LÖVE is love.draw. This function is called periodically, and is responsible for putting stuff on the screen. For example, it can draw on the screen the images that were loaded in love.load.

The line above is declaring love.draw

love.graphics.print

love.graphics.print(Message, 200, 200)

Until now you have seen function declarations. This line, however, is an invocation.

love.load and love.draw are a special class of function because they get executed immediately, nearly right after LÖVE reads them. But most functions don’t work that way – they require to be invoked in addition to being defined.

To invoke a function, you write its name followed up by an opening parenthesis, a list of parameters separated by commas, and a closing parenthesis.

The invocation above is, in consequence, executing love.graphics.print, with the Message global variable as its first parameter, and the number 200 as second and third parameter. It is telling LÖVE to “get the contents of the Message global variable and print them on the screen, on coordinates 200, 200)”.

end

This end finishes the love.draw declaration.

References

Here’re some links where you can deepen your knowledge about LÖVE and Lua:

Exercises

  1. Change the declaration of secret so you can use it inside love.draw

index | 0b Tables »