Back

Activity 1.2: Variables, local, and nil

divider

The Idea

Lua has no let, no const, and no type names to write. There is exactly one keyword you need, and it is local.

main.lua
local name = "Ada"
local age = 17
local onDuty = true
local assignment = nil
print(name, age, onDuty, assignment)
Output
Ada 17 true nil

The value decides the type, and you can ask what a value is with type().

main.lua
print(type("SKYNEST"))
print(type(17))
print(type(true))
print(type(nil))
print(type(print))
Output
string
number
boolean
nil
function

Notice the last one. A function is a value, the same as a number is. That fact does almost nothing for you today and becomes the most useful thing you know by Activity 1.8.

Nothing pins a variable to the type it started with, either.

main.lua
local reading = 42
print(reading, type(reading))
reading = "offline"
print(reading, type(reading))
Output
42 number
offline string

Why local is not optional

Here is the part of Lua most worth getting right on day one. If you leave off local, the variable is global — visible to every line of every file in your program, for the rest of its run.

A do ... end block is a chunk of code with its own scope. Watch what leaks out of it and what does not.

Without local
do
shieldLevel = 100
end
print(shieldLevel)
Output
100
With local
do
local shieldLevel = 100
end
print(shieldLevel)
Output
nil

Globals are slower, they collide with each other, and they make a bug in one file able to break a different one. Lua will never warn you. Write local every time until leaving it off feels wrong.

nil, and the trap that comes with it

nil is Lua's word for nothing here. It is a real value with its own type, and it is what you get from any variable that was never given anything.

Which produces the single most common Lua bug there is:

main.lua
local playerName = "Ada"
print(playerNmae)
Output
nil

That typo is not an error. playerNmae is simply a global nobody ever assigned, so it is nil, and Lua prints it without complaint. Your program runs, it is just wrong.

When something in Lua is mysteriously nil, check your spelling before you check your logic. It is the typo far more often than it is the reasoning.

Several at once

Lua lets you assign a whole row of variables on one line, and the right-hand side is worked out before anything is stored. That gives you a swap with no temporary variable.

main.lua
local a, b = 1, 2
print(a, b)
a, b = b, a
print(a, b)
Output
1 2
2 1
divider

Build

Open your main.lua and replace what is in it. You are turning last activity's hard-coded readout into one driven by variables.


Task 1: Store the readings

  • Declare three local variables: a name, a crew count, and whether the ship is online.
  • Use a string, a number, and a boolean — one of each.
  • Print all three with a single print call.
main.lua
-- main.lua - Skynest status board
local shipName = "Skynest"
local crew = 4
local online = true
print(shipName, crew, online)
Output
Skynest 4 true

Task 2: Add something you do not know yet

  • Add a variable for the next port and set it to nil. The ship has no orders yet, and that is information worth storing.
  • Print it alongside the others.
  • Add a second print that reports the type() of all four.
main.lua
-- main.lua - Skynest status board
local shipName = "Skynest"
local crew = 4
local online = true
local nextPort = nil
print(shipName, crew, online, nextPort)
print(type(shipName), type(crew), type(online), type(nextPort))

Task 3: Break it on purpose

  • Add a line that prints shipNmae — the misspelling is the point.
  • Run it. Note that you get no error, and write down in a comment what you got instead.
  • This is the failure you will spend the most time hunting later. Meet it now, while you already know the answer.
main.lua
-- main.lua - Skynest status board
local shipName = "Skynest"
local crew = 4
local online = true
local nextPort = nil
print(shipName, crew, online, nextPort)
print(type(shipName), type(crew), type(online), type(nextPort))
print(shipNmae)

Challenge (Optional)

  • Swap the values of two of your variables on one line, with no third variable, and print them before and after.
  • Delete the word local from one declaration. Confirm that the program still runs identically — then explain to yourself, in a comment, why that is a bad thing rather than a convenience.
divider

Check Your Work

Your finished file should produce this.

Output
Skynest 4 true nil
string number boolean nil
nil

Three things to be able to say out loud before moving on: what local does, what nil means, and why a misspelled variable name does not stop the program.