-
Notifications
You must be signed in to change notification settings - Fork 37
0a basic concepts
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.
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:
- Opening a console
- 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 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.
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.
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.
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 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
).
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.
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(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.
Here’re some links where you can deepen your knowledge about LÖVE and Lua:
-
The LÖVE getting started page speaks more about
main.lua
- Read the LÖVE callbacks tutorial for more info about
love.load
andlove.draw
- See the first chapter of Programming In Lua for a more extensive presentation of Lua.
- Change the declaration of
secret
so you can use it insidelove.draw