Intro to Lua
Tiny, fast, and powerful. The secret weapon behind your favorite games.
Tiny, fast, and powerful. The secret weapon behind your favorite games.
In 1993, three computer scientists at PUC-Rio university in Brazil created a language that would quietly become one of the most widely embedded languages in the world. They called it "Lua," the Portuguese word for "moon."
Lua was not Brazil's first homegrown language. The same team created two earlier languages called SOL ("Simple Object Language") and DEL ("Data Entry Language"). SOL is also Portuguese for "sun." From the sun came the moon: Lua.
Lua's syntax is clean and readable. No semicolons, no curly braces, no type declarations. You just write what you mean.
That is the entire program. One line. print() sends text to the output.
In Lua, you do not need a keyword like var or let. Just assign a value to a name.
Lua uses .. to join strings (not +).
Type lua in the terminal to open the interactive prompt. Type code, press Enter, see the result.
Most languages have arrays, dictionaries, objects, and classes as separate things. Lua has one data structure that does all of it: the table. If you understand tables, you understand Lua.
Use curly braces to create a table. Lua arrays start at index 1, not 0.
Add named keys and the table becomes a key-value store, like a dictionary or object.
Tables inside tables. You can build any data structure you need.
In Lua, everything is a table. Arrays are tables with integer keys. Objects are tables with string keys. Modules are tables. Even the global scope (_G) is a table. This simplicity is by design: one powerful concept instead of a dozen specialized ones.
Lua functions are first-class values. You can store them in variables, pass them as arguments, and return them from other functions, just like numbers or strings.
Most languages only let a function return one thing. Lua functions can return as many values as you want.
Functions are values in Lua, just like strings or numbers. Store them in variables or tables.
A closure is a function that remembers values from the scope where it was created, even after that scope is gone.
Plenty of scripting languages exist. Here is why Lua keeps showing up in places that matter.
The entire Lua interpreter is roughly 200KB. That is smaller than most image files. You can embed it in a microcontroller, a game console, or a router.
LuaJIT, the just-in-time compiled version, is one of the fastest dynamic language runtimes in existence. It regularly outperforms Python, Ruby, and JavaScript in benchmarks.
Lua was built to live inside other programs. Its C API makes it trivial to add scripting to any C or C++ application. That is why game engines love it.
Tables handle arrays, dictionaries, objects, classes, and modules. Instead of learning five different container types, you learn one flexible concept.
Lua has only 21 reserved keywords. Compare that to Java (50+), C++ (80+), or even Python (35). The entire language reference fits in a small booklet.
Roblox, World of Warcraft, Adobe Lightroom, Angry Birds, Nginx (OpenResty), Redis, Wireshark, and hundreds of game engines all depend on Lua in production.
Lua's entire source code is about 30,000 lines of C. Python's is over 400,000. JavaScript (V8 engine) is over 1,000,000. Lua proves that a language does not need to be large to be powerful.
Open the terminal and start writing Lua right now. Here is everything you need.
apt install luanano game.lualua game.luaCopy this into game.lua and run it. Then modify it to add your own rooms and choices.
Challenge: Add more rooms. Use tables to store room descriptions and connections. Add an inventory system with a table. Make the dragon fight winnable if the player has a sword.
You can also use the Lua REPL for quick experiments. Just type lua with no arguments to start it. Try print(type({})) to see what type a table is, or print(type(print)) to confirm that functions are values.
You've explored one of the most elegant scripting languages ever created. From Roblox to game engines to embedded systems, Lua is everywhere. Now you know why.
Lua was created in 1993 at PUC-Rio in Brazil. "Lua" means "moon" in Portuguese. It was built because trade restrictions made importing software impossible.
Lua has one data structure: the table. It works as an array, dictionary, object, module, and namespace. Simple by design, powerful in practice.
At roughly 200KB, Lua is designed to live inside other programs. Game engines, routers, Adobe Lightroom, and Redis all use Lua as their scripting layer.
Lua treats functions like any other value. You can store them in variables, pass them as arguments, and return them from other functions.
Put your new knowledge into practice!