What is Lua?
Lua is a powerful, efficient, lightweight, embeddable scripting language. It supports procedural programming, object-oriented programming, functional programming, data-driven programming, and data description.
Developed in Brazil in 1993, Lua is known for its simplicity, speed, and ease of integration with applications written in other languages like C and C++. This makes it a popular choice for game development (e.g., Roblox, World of Warcraft addons), embedded systems, and configuration files.
Key Features of Lua
- Lightweight: The core Lua interpreter is very small, making it ideal for embedding.
- Embeddable: Designed from the ground up to be easily integrated into host applications.
- Fast: Often benchmarks among the fastest scripting languages.
- Portable: Written in clean C, Lua runs on a wide variety of platforms.
- Simple Syntax: Easy to learn and read, with a concise grammar.
- Extensible: Allows easy extension through Lua code or external C libraries.
- Garbage Collection: Automatic memory management simplifies development.
Example Lua Code
-- Simple Lua example demonstrating functions and tables
local player = {
name = "Hero",
level = 1,
inventory = {"sword", "potion"}
}
function greet(name)
print("Hello, " .. name .. "!") -- String concatenation
end
function levelUp(character)
character.level = character.level + 1
print(character.name .. " reached level " .. character.level .. "!")
end
greet("World") -- Output: Hello, World!
levelUp(player) -- Output: Hero reached level 2!
print("Inventory size: " .. #player.inventory) -- Output: Inventory size: 2