What is Lua?
Lesson 2.1
Lua is a small programming language made for being built into other programs. Games use it a lot, and Celestial uses it for mods.
A language for scripting
Lua was created in 1993 at a university in Rio de Janeiro, Brazil. “Lua” means “moon” in Portuguese. It is not an acronym, so write it as Lua, not LUA.
Lua is designed to live inside another program, called the host. The host does the heavy work and gives Lua a set of functions to call. Many games work this way: the engine is written in C or C++, and the game logic, menus or mods are written in Lua.
Celestial works the same way. The game is the host. Your mod is a Lua script, and the game gives it the cl table with functions for items, blocks, players, screens and more. You will meet cl in part 2. Part 1 is about the language itself.
Why Lua is a good first language
- It is small. There are only 22 keywords, such as
if,for,functionandend. You will know all of them by the end of this course. - It reads like English. Blocks start with a keyword and finish with
end. There are no curly braces and no semicolons to forget. - It has one data structure. Lists, dictionaries, objects and modules are all built from tables.
- It is fast. Lua is one of the fastest scripting languages, which matters when your code runs every game tick.
What Lua code looks like
Here is a complete Lua program. Don’t worry about the details yet; just notice how readable it is.
local items = { "Stone", "Torch", "Diamond" }
for i, name in ipairs(items) do
print(i, name)
end
It prints:
1 Stone
2 Torch
3 Diamond
Which version?
This course teaches Lua 5.4, the current version and the one Celestial uses. Most Lua code you find online is for 5.1 to 5.4 and works the same way. Where 5.4 differs (for example, it has real integers), the lesson says so.
Try it
Look at the program above. Without running it, guess what it prints if you add "Bread" to the end of the list.
Show the answer
One more line: 4 Bread. The loop goes over every entry in the list, so a fourth item gives a fourth line.
Recap: Lua is a small, fast language that runs inside a host program, and Celestial uses it for mods.