Home / Lua
Lua
Lua beyond the basics: metatables in depth, closures and iterators, object patterns, coroutine pipelines, string patterns, files, and garbage collection. Every example is verified with the real interpreter.
Lua Intermediate is the deeper level of Lua. It assumes Lua Beginner is complete and turns its first looks into working tools. Across 52 lessons you learn to bend tables to your will with metatables, build objects, run coroutine pipelines, and parse text with patterns.
Course contents
Work top to bottom — the sidebar is the course. Topics later in the list assume the ones before them.
Lua Intermediate Introduction
Intermediate · ⏱ 3 min · standalone lesson
Metatables in Depth
- Lua Metamethods: Arithmetic
- Lua Metamethods: Comparison
- Lua Metamethod __index
- Lua Metamethod __newindex
- Lua Metamethod __call
- Lua Metamethods: Tostring and Concat
- Lua Metatable Chains
Closures and Iterators
- Lua Closures with State
- Lua Closures for Privacy
- Lua Stateful Iterators
- Lua Stateless Iterators
- Lua Chaining Iterators
- Lua Iterators over Trees
Object-Oriented Lua
- Lua Classes with Tables
- Lua Inheritance
- Lua Encapsulation with Closures
- Lua Polymorphism
- Lua Mixins
- Lua Class Factories
Coroutines in Depth
- Lua Coroutine Status
- Lua Producer Consumer Coroutines
- Lua Coroutine Pipelines
- Lua Coroutines with Wrap
- Lua Coroutine Scheduler
- Lua Coroutine Errors
String Patterns in Depth
- Lua Pattern Captures
- Lua Pattern Character Classes
- Lua Pattern Frontiers
- Lua Gsub with Functions
- Lua Pattern Quantifiers
- Lua Parsing with Patterns
Tables in Depth
- Lua Sorting Tables
- Lua Serializing Tables
- Lua Tables as Sets
- Lua Queues and Stacks
- Lua Copying Tables
Errors and Modules
Start: Lua Pcall Patterns ❯Files and I/O
- Lua Writing Files
- Lua Reading Files
- Lua Reading Files Line by Line
- Lua Formatted File Output
- Lua Processing Files
Garbage Collection
Start: Lua Garbage Collection ❯Lua Project: Text Adventure
Intermediate · ⏱ 7 min · standalone lesson
Lua Project: Data Tool
Intermediate · ⏱ 6 min · standalone lesson
Learn Lua Intermediate
Lua is a small language that lives inside bigger programs. Games, tools, and editors embed it because it is light and quick to learn. At the intermediate level you stop using only the surface of Lua and start shaping how it behaves.
Metatables sit at the centre of this level. They let a table answer arithmetic, comparisons, missing keys, and calls in its own way. Once you can write them, tables stop being plain containers and become vectors, accounts, settings stores, and game objects.
The rest of the course grows from the same idea of control. Coroutines turn into pipelines and small schedulers. String patterns turn into real parsers. Closures keep private state. Files, errors, and garbage collection round out the toolkit for programs that run longer than a single script.
What you will learn
Work through the course and you will be able to do each of these things in Lua:
- Give tables custom behaviour with arithmetic, comparison, index, newindex, call, and tostring metamethods.
- Chain fallbacks through several tables, and step outside them with rawget and rawset.
- Keep state in closures, hide it for privacy, and write iterators that walk arrays, chains, and trees.
- Build classes with tables, inherit from a parent class, share behaviour with mixins, and generate classes from factories.
- Run coroutines as producers, pipelines, and a round-robin scheduler, and handle errors inside them.
- Capture parts of a string, match character classes and word frontiers, rewrite with gsub functions, and parse logs and key-value lines.
- Sort tables with your own comparison, turn tables into strings and back, and use tables as sets, queues, stacks, and copies.
- Catch failures with pcall and xpcall, raise errors that carry their own data, and organise code into modules.
- Read and write files, walk them line by line, format records as text, and summarise a whole file.
- Explain how garbage collection frees unreachable values, use weak tables for caches, and write faster code with locals and table.concat.
- Finish by building a text adventure engine and a data tool that reads, parses, and reports on a file.
Who this Lua Intermediate course is for
This course is for readers who finished Lua Beginner and want the next layer. If metatables, coroutines, and modules still feel like sketches, this level fills them in properly.
It suits people who embed Lua in games or tools. Much of the level is about shaping tables into the objects a host program needs: settings, entities, inventories, and plug-ins.
It also suits programmers coming from another language who already know loops, functions, and tables. The course does not reteach those ideas. It teaches what makes Lua different.
How this course works
Work through the lessons in order. Later topics use earlier ones: object patterns lean on closures, coroutine pipelines lean on iterators, and the projects lean on everything before them.
The 52 lessons add up to 218 minutes of reading time, just short of three hours and forty minutes, before practice time. The two projects ask for more time than their pages take to read, since reshaping them around your own ideas is where the learning sticks.
Every example was run twice with Lua 5.4.7, and the stated output matches the interpreter byte for byte. Each lesson ends with a short exercise, and the answer gives the reasoning behind it.
As you finish lessons, the browser remembers your place. The course home tracks your spot across the 52 lessons, so you can pick up where you stopped.
Frequently asked questions
Do I need to finish Lua Beginner before this course?
Yes. This course treats the Beginner course as done: tables, functions, loops, and the first looks at metatables, modules, and coroutines are all used from the first lessons. If any of those feel unfamiliar, start with Lua Beginner and return here after it.
Is this Lua Intermediate course free?
Nothing here is locked: all 52 lessons, every exercise, and both projects are free to read and try, with no sign-up and no charge anywhere.
Do I need to install Lua to take this course?
No. Every lesson shows its code with the exact output the real interpreter produced, so you can learn by reading. To run the code yourself, install Lua 5.4: the course was verified with version 5.4.7.
How long does the course take?
Adding up the per-lesson estimates gives 218 minutes of reading, just short of three hours and forty minutes, before practice time. The projects deserve extra time beyond that: adapting them is where the skills settle.
What is a metatable?
A metatable is a table that holds special behaviour for another table. When Lua adds, compares, indexes, or calls a table, it looks in that table's metatable for a field that says what to do.
What can I build after this course?
You can build programs that organise their own data: a small game engine with rooms and items, a tool that reads and summarises files, modules with private state, and coroutine pipelines that process data in stages. The two projects in this course are working starts for all four.
Next steps
- Lua ❯ — The prerequisite course: first scripts, tables, functions, and first looks at metatables, modules, and coroutines.
- Python Beginner ❯ — Another scripting language on this platform, for general-purpose programs and automation.