TExcelTAD
Home › Lua › Lua Intro

Lua Intro

Lua (Portuguese for "moon") is a small, fast scripting language created in 1993 at PUC-Rio in Brazil. Its whole interpreter fits in a few hundred kilobytes of portable C, which makes it the favourite language for embedding inside bigger programs: the host application is written in C, C++ or Rust, and Lua is the language its users write scripts and plugins in.

This course teaches Lua 5.4, the current version. Every lua run example on these pages runs the real Lua 5.4 interpreter, so you can edit the code and press ▶ Run.

lua · try it yourself
print("Hello from Lua!")
print("This is " .. _VERSION)
Hello from Lua!
This is Lua 5.4

print writes its arguments followed by a newline, .. joins two strings, and _VERSION is a global variable that holds the interpreter's version.

Where Lua is used

You have probably used software scripted in Lua without knowing it:

Area Examples
Games World of Warcraft add-ons, Roblox (its dialect Luau), the LÖVE 2D engine, Defold, many console titles
Editors and tools Neovim configuration and plugins, Wireshark dissectors, Adobe Lightroom plug-ins
Servers and networking Redis scripts (EVAL), nginx through OpenResty, the Kong API gateway, HAProxy
Embedded devices Router firmware (OpenWrt's LuCI web UI), the NodeMCU board for ESP8266 chips

The common theme: an application wants users to customise its behaviour without recompiling it, and Lua is easy to embed, safe to sandbox and quick to learn.

What makes Lua different

  • One data structure. The table is Lua's only compound type. Arrays, dictionaries, objects, modules and namespaces are all tables.
  • Small and regular. The reference manual is about 100 pages. There are only 22 reserved words.
  • Functions are values. You can store them in variables, pass them around and return them, and they capture the variables around them (closures).
  • Metatables. You can change how a table behaves with operators such as +, == or indexing, which is how Lua does object-oriented programming.
  • Coroutines. Cooperative "threads" that pause and resume, often used for game logic and asynchronous code.
lua · try it yourself
-- A table used as a record
local planet = { name = "Mars", moons = 2 }

-- A table used as a list
local moons = { "Phobos", "Deimos" }

print(planet.name .. " has " .. planet.moons .. " moons:")
for i, moon in ipairs(moons) do
  print(i, moon)
end
Mars has 2 moons:
1	Phobos
2	Deimos

Notice that print separates several arguments with a tab character, and that Lua lists start at index 1, not 0. Both surprise newcomers coming from other languages.

Lua compared with other scripting languages

Lua Python JavaScript
Typing dynamic dynamic dynamic
Collections tables only list, dict, set, tuple Array, Object, Map, Set
First index 1 0 0
Block syntax then … end, do … end indentation { … }
Not-equal operator ~= != !==
Main use embedding, games, config general purpose the web

How this course is organised

  1. Basics: syntax, variables, types, strings, operators, control flow.
  2. Functions: multiple return values, varargs, closures, recursion.
  3. Tables: arrays, dictionaries, the table library and iterators.
  4. Metatables and objects: operator overloading, classes and inheritance.
  5. Advanced topics: modules, error handling, coroutines, patterns, UTF-8, garbage collection, and how Lua is embedded in C programs and games.
  6. Practice: exercises and a mini project.

Note: The playground runs plain Lua 5.4 with its standard library. Libraries from LuaRocks (Lua's package manager) and engine APIs such as Roblox's or LÖVE's are shown as non-runnable code.

Test yourself

What is Lua's only built-in compound data structure?

  1. The array
  2. The list
  3. The table
  4. The object

At which index does a Lua list start by convention?

  1. 0
  2. 1
  3. -1
  4. Any index

Why is Lua so popular inside games and tools?

  1. It compiles to machine code only
  2. It is small, fast and easy to embed in a host program
  3. It only runs in browsers
  4. It has a huge standard library