Lua
- Pronunciation
- LOO-uh
In short
Lua is a small, fast scripting language designed to be embedded in other applications, making it popular for game scripting, plugins and configuration.
What is Lua?
Lua is a lightweight scripting language created in 1993 at the Pontifical Catholic University of Rio de Janeiro in Brazil; its name means "moon" in Portuguese. It was designed to be embedded inside other programs: the whole interpreter is a small C library that an application can include, so users can write Lua scripts to extend or configure that application without recompiling it.
Lua is dynamically typed and deliberately minimal. Its only built-in data structure is the table, which serves as array, dictionary, object and module all at once, and metatables let you customize how tables behave to build classes or overload operators. Lua counts array positions from 1, supports first-class functions and closures, and has coroutines for cooperative multitasking. Memory is managed by a garbage collector, and the separate LuaJIT project provides a very fast just-in-time compiler.
Lua is widely used for scripting in video games and game engines, for plugins and configuration in text editors, for scripts that run inside web servers, proxies and databases, and in embedded devices. Embedding Lua is like giving an appliance a slot for recipe cards: the machine does the heavy work in fast native code, and the scripts decide what it should do.
Lua is often compared with Python and JavaScript, which are also dynamic scripting languages. The main difference is purpose: Python and JavaScript ship large standard libraries and are mostly used to write whole applications, while Lua is tiny, easy to embed in a C or C++ host and usually runs inside another program. Some platforms maintain their own Lua dialects that add features such as gradual typing on top of the core language.
Key takeaways
- Lua is a tiny scripting language designed to be embedded inside other applications.
- Tables are its single, flexible data structure for arrays, maps and objects.
- Arrays in Lua start at index 1 by convention.
- It is widely used for game scripting, editor plugins and server-side extensions.
Example
-- A table works as both an array and a dictionary
local player = { name = "Ada", score = 0, items = { "sword", "map" } }
function player:addPoints(points) -- ':' passes the table as self
self.score = self.score + points
end
player:addPoints(10)
print(player.name .. " has " .. player.score .. " points") -- Ada has 10 points
for i, item in ipairs(player.items) do -- arrays start at 1
print(i, item) -- 1 sword, then 2 map
endReaders ask
Why do Lua arrays start at 1?
Lua was designed with engineers and non-programmers in mind, who were used to counting from 1, so its standard library and length operator treat 1 as the first index. You can store a value at index 0, but functions like ipairs start at 1.
Is Lua hard to learn?
No. Lua has a small syntax and only a handful of concepts, so many people learn the basics in a day, which is one reason games and tools choose it for user scripts.
Is Lua faster than Python?
The standard Lua interpreter is often faster than CPython for simple scripts, and LuaJIT can be much faster still. Real performance depends on the workload and on how much work happens in native libraries.
See also
- InterpreterProgramming Fundamentals, p. 30An interpreter is a program that runs source code directly, step by step, instead of first translating the whole program into a separate executable file.
- PythonProgramming Languages, p. 26Python is a general-purpose, dynamically typed programming language known for readable syntax and wide use in data science, automation, and web backends.
- JavaScriptWeb Development, p. 24JavaScript is the programming language that runs in web browsers to make pages interactive, and it also runs on servers through runtimes like Node.js.
- CProgramming Languages, p. 3C is a compiled, low-level programming language that gives direct control over memory and is used to build operating systems, drivers, and embedded software.
- Garbage CollectionProgramming Fundamentals, p. 23Garbage collection is automatic memory management in which the language runtime finds data a program can no longer use and frees that memory for reuse.
- Hash TableData Structures, p. 18A hash table is a data structure that stores key-value pairs and uses a hash function to find the value for any key in constant time on average.
Spotted a mistake or something missing on this page?Suggest an edit