1. Values and variables

A program works with values. Lua has only a few kinds.

Numbers

print(7 + 2)     --> 9
print(7 - 2)     --> 5
print(7 * 2)     --> 14
print(7 / 2)     --> 3.5
print(7 // 2)    --> 3    whole division: how many times 2 fits
print(7 % 2)     --> 1    what is left over
print(2 ^ 10)    --> 1024.0

Use brackets to say what happens first: (1 + 2) * 3 is 9, and 1 + 2 * 3 is 7.

Text

Text is called a string. Write it in double or single quotes.

print("Game over")
print('Game over')

Join strings with two dots. Numbers join too:

print("Score: " .. 120)        --> Score: 120
print("Level " .. 2 .. " of " .. 5)   --> Level 2 of 5

# in front of a string gives its length:

print(#"hello")   --> 5

True and false

These two values are called booleans. Comparisons give them:

print(5 > 3)     --> true
print(5 < 3)     --> false
print(5 == 5)    --> true    equal
print(5 ~= 5)    --> false   not equal
print(5 >= 5)    --> true    greater or equal

Equal is two equals signs, and not equal is ~=. Both catch out people who come from other languages.

Nothing

nil is the value that means "nothing here". A name you never gave a value is nil.

print(nobody)    --> nil

You will meet nil often: a search that finds nothing gives nil, and so does a field nobody filled in.

Variables

A variable is a name for a value. Make one with local:

local health = 100
local name = "Mira"
local alive = true

print(name .. " has " .. health .. " health")   --> Mira has 100 health

Change it by assigning again, this time without local:

local health = 100
health = health - 25
print(health)    --> 75

health = health - 25 reads oddly as maths. Read = as "becomes": health becomes what it was, minus 25. There is no += or -= in Lua.

Always write local

Without local, a name is global: every script in the game can see it and change it.

score = 10         -- global: avoid
local score = 10   -- local: use this

Two scripts that both use a global called score will overwrite each other, and the bug that follows is hard to find. Write local every time you make a variable.

Names

A name is letters, digits and underscores, and does not start with a digit. The engine's own names start with a capital (Input, Vec3, OnUpdate). A common habit is to start your own local variables with a small letter, so the two are easy to tell apart.

Several at once

Lua can assign several variables in one line:

local x, y = 10, 20
print(x, y)    --> 10   20

That is also how to swap two values:

local a, b = 1, 2
a, b = b, a
print(a, b)    --> 2   1

Turning one kind into another

print(tostring(42) .. "!")    --> 42!
print(tonumber("42") + 1)     --> 43
print(tonumber("abc"))        --> nil    not a number

In a game

The engine adds one more kind of value you will use all the time, a vector of three numbers:

-- file: example
local position = Vec3(1, 2, 3)
print(position.x)                    --> 1.0
print(position + Vec3(0, 10, 0))     --> (1, 12, 3)
print(position * 2)                  --> (2, 4, 6)

A position, a direction, a scale and a colour are all Vec3 values.

Try it

  1. Make variables for a player's name, level and gold. Print one line that says Mira is level 3 with 250 gold.
  2. A potion heals 30. Start health at 55, drink a potion and print the result.
  3. What does print(10 / 4, 10 // 4, 10 % 4) show? Guess, then run it.

Next: decisions and loops.