Lua from the ground up
A tiny language that goes almost everywhere
- Beginner
- 12 chapters
- 194 min read
Learn Lua by running it: values and variables, decisions, loops, text, tables used as lists and as maps, functions, closures, errors, and objects built from metatables, ending with a small program that uses all of it at once.
What You'll Learn
- Print output, write comments, and run a first program
- Hold values in local variables and tell integers from floats
- Branch with if, elseif and else, and know that only nil and false are false
- Repeat work with while and for, and leave a loop early with break
- Join, measure, cut and format text with the string library
- Use a table as a list: insert, remove, walk with ipairs, and sort
- Use a table as a map, and print its keys in a stable sorted order
- Name work as functions, return several values, and take a variable number of arguments
- Capture state in closures and pass functions to functions
- Raise errors on purpose and catch them with pcall and assert
- Build objects from metatables, sharing methods and controlling how they print
- Assemble a small program with a class, a custom sort, and a coroutine
Prerequisites
- No programming experience needed — this course starts at the first line
- Any other language you know will make the early chapters quick
Course Content
- 01Your first LuaNot started
What Lua is, how to print a line, how comments work, and a first program you can run and change.
- 02Values and variablesNot started
Holding values in variables with local, the two kinds of number, text, booleans, nil, and asking a value what it is with type.
- 03DecisionsNot started
Choosing with if, elseif and else, comparing values, and the truth rule that only nil and false are false.
- 04LoopsNot started
Repeating work with while and for, counting up and down, and leaving a loop once you are finished with it.
- 05Text in depthNot started
Joining and measuring text, pulling pieces out with sub and find, and laying values out neatly with string.format.
- 06Tables as arraysNot started
Using a table as a list, growing and trimming it with insert and remove, walking it with ipairs, and sorting it in place.
- 07Tables as mapsNot started
Using a table as a set of named fields, reading and writing keys, walking the pairs, and printing them in a stable order.
- 08FunctionsNot started
Naming a block of work with local function, giving values back, returning more than one thing, taking a variable number of arguments, and defaulting a missing one.
- 09Closures and higher-order functionsNot started
Functions as values you can pass and return, and closures that remember the variables they were created beside.
- 10Errors and pcallNot started
Raising an error on purpose, protecting a call with pcall, and promising that something is true with assert.
- 11Objects with metatablesNot started
Adding behaviour to a table with a metatable, sharing methods through the index hook, and controlling how an object prints.
- 12A small program, end to endNot started
Putting the course together: a metatable class, a table of objects, a custom sort, and one more tool — the coroutine.
