Lua for C++ Developers
Move from C++ value types, STL containers and classes to Lua 5.1 tables, closures and metatable objects. Practice real multi-file modules, build a stock ledger, and port two C++ reference programs.
C++ and Lua side by side
The same program in C++ and in Lua, from the lessons of this course.
Print and Comment
Lua uses print(...) to write values followed by a newline. Unlike std::cout, it inserts separators between multiple arguments, so build one string when the exact spacing matters. Join strings with ..; the + operator is for arithmetic. Quoted strings accept…
C++1std::cout << std::string("Hi") + ", Ada" << "\n";Lua1print("Hi" .. ", Ada")Absence and Missing Fields
Lua nil represents absence. An uninitialized local or missing table field evaluates to nil. Assigning nil to a table field removes that field, so a table cannot store a present entry whose value is nil. This differs from a C++ map containing an empty…
C++1std::map<std::string, std::optional<int>> d;2d["x"] = std::nullopt;3std::cout << std::boolalpha << (d.count("x") != 0) << "\n";Lua1local d = {x = nil}2print(d.x == nil)Counted Loops to Numeric for
Lua numeric loops use for i = start, limit, step do ... end. The step defaults to one, and the limit is inclusive when reached. A negative step walks downward. The loop variable is local to the loop. Start, limit, and step expressions are evaluated before…
C++1int total = 0;2for (int i = 1; i <= 3; ++i) total += i;3std::cout << total << "\n";Lua1local total = 02for i = 1, 3 do total = total + i end3print(total)Functions and Returns
C++ declares parameter and return types, and ordinary calls must satisfy the declared signature. Lua uses local function name(parameters) ... end without type declarations. Functions are values, so they can be stored and passed to other functions. Numeric…
C++1int twice(int x) {2 x *= 2;3 return x;4}Lua1local function twice(x) return x * 2 end2print(twice(4))Vectors as Dense Tables
A C++ vector has a stored size and contiguous elements indexed from zero. Lua uses tables as sequences by convention, keeping integer keys dense from one through the sequence length. Translate index offsets deliberately: the second vector element corresponds…
C++1std::vector<std::string> values{"a", "b", "c"};2values.erase(values.begin() + 1);3std::cout << values[0] << ',' << values[1] << '\n';Lua1local a = {"a", "b", "c"}2table.remove(a, 2)3print(table.concat(a, ","))Sorting and Comparators
C++ std::sort rearranges an iterator range in place. Lua table.sort similarly rearranges a dense sequence and returns no result. Both accept a comparator that reports whether its first argument should precede its second. For descending numbers, return a > b.…
C++1std::vector<int> values{3, 1, 2};2std::sort(values.begin(), values.end());3std::cout << values[0] << '\n';Lua1local a={3,1,2}2table.sort(a)3print(a[1])
Syllabus
10 chapters48 lessons1 project48 challenges99 quiz questionsFirst Lua Programs
6 lessonsValues and Decisions
5 lessonsIteration and Flow
5 lessonsFunctions and Closures
5 lessonsTables as Collections
5 lessonsOrdering, Modules and Errors
5 lessonsObjects and Owned State
5 lessonsComposition and Object Lookup
6 lessonsProject: Stock Ledger
4 lessonsFinal C++ Ports
2 lessonsFrequently asked questions
Who is Lua for C++ Developers for?
For people who already write C++ and want to learn Lua. Each lesson starts from C++ you know and shows how Lua does the same thing, so you skip the beginner basics.
Do I need to know Lua already?
No. The course starts with your first Lua program. You need C++ basics: variables, conditions, loops, functions and collections.
How many lessons does Lua for C++ Developers have?
Lua for C++ Developers contains 48 interactive lessons you can complete at your own pace.
What does Lua for C++ Developers cover?
The course is organized into chapters covering: First Lua Programs, Values and Decisions, Iteration and Flow, Functions and Closures, and more.
Is Lua for C++ Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.