Lua was designed by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, and Waldemar Celes Filho. The language was designed to be embedded easily into other applications. It is multi-paradigm, lightweight, portable (UNIX, Windows, iOS, Android, game consoles), and is commonly used as a scripting language.
Can there really be another programming language? Lua is a new language, but not entirely new. In fact, the Lua (meaning moon in Portuguese) language has been around since 1993. (In case you are wondering why there’s a need for so many programming languages, Matt Sherman provides the answer in this post.)
Lua is, by far, the best and most popular scripting language choice when it comes to games. The language is simpler than others like C++. It is dynamically typed, supports garbage collection, and is extremely fast, making it very attractive to game developers. Here are some games that use Lua in one way or another: Far Cry, SimCity (for generating help texts), UFO, and World of Warcraft. Apart from games, other applications have been developed partly or fully in Lua, or embed Lua for scripting support: VLC, MySQL Workbench, Nginx, Geany, Awesome window manager, CRYENGINE, and conky.
Conky displaying system information on the desktop. Conky scripts written with Lua.
Language Basics
Following the tradition, we will begin with the famous Hello World program. Download Lua, if you haven’t already. Then run the Lua REPL via the lua (or lua5.x) command.
print("Hello World") --prints Hello World
This displays the message “Hello World” as expected. This is exactly what a Python 3.x Hello-World program looks like—This time, it’s Lua. A lua script has a .lua extension. The interpreter can execute a script by feeding it the file name like this: lua5.3 first.lua
Lua is interpreted, but it precompiles code into an intermediary form before running it. The precompiled form is generated using the luac command.
The last statement is a one-line comment (begins with two dashes). Comments that span multiple lines start with –[[ and end in –]] . Similar to JavaScript, Lua doesn’t require a semicolon after every statement.
Types
Lua has eight basic types: boolean, number, nil, string, function, userdata, table, and thread. As a dynamically typed language, variable names need no type qualification. Many of the types are common types in other languages. The nil type represents the absence of value (just like null in other languages). Tables are a powerful feature, and the only data structure of Lua. Userdata represents new types created by C library or an application program. Note that strings in Lua are surrounded either by double or single quotes.
In Lua, tables are objects which represent associative arrays, packages, or other data structures like sets. The concept of arrays has a subtle difference from arrays in many other languages. Array indices can be a number, or any other value (string, etc.) Nil, however, is not accepted. A big difference is that tables are not fixed-sized and do not store only one kind of data.
To find out the type of a variable, use the type function.
my_array = {} --empty table my_array['first'] = 1 my_array['second'] = false type(44.3) --number type(false) --boolean type("Lua") --string type(my_array) --table
Loops & Functions
Before anything, let me establish this: Every variable in Lua is global unless qualified with local, which forces the variable to become local to a chunk.
Syntax for control structures is very close to pure English. The first is the if-then-else statement.
if wait_time > 10 then patience = 0 end if wait_time == 10 then -- do something else -- do another thing end
Note the end terminator. It is required.
Another control structure is repeat-until. It runs a section of code until a condition is satisfied. Like the do-while loop in other languages, the check is performed after executing the body.
a = 0 repeat a = a + 1 print(a) until a == 10
Not equal looks a little weird. It is written as ~=
While loop executes the body as long as a condition holds.
while wait_time < 10 do print("We're on it") wait_time = wait_time + 1 end
Functions
We’ve already met one function from the start: the print function. Functions in Lua have some quite interesting features. The most outstanding one is probably the ability to return multiple values from a function. Function definitions are simple. We’ll define one which utilizes loops.
function say(msg) if msg == nil then msg = "Hello!" end return msg, os.date() end -- required!
This trivial example brings up some key points to discuss.
First, when the function is called without an argument, it executes and returns the string “Hello!” plus the current date without errors. When arguments are omitted, nil is used for the parameter values. The function returns the value of arguments passed, and the date, when called with arguments. Notice how the function returns multiple values. It is indeed a unique feature which you may want to take advantage of.
As a multi-paradigm language, there are many aspects of the language worth exploring; however, not all can be covered here. Lua is very easy. The syntax is extremely simple. With the concepts covered here, you should be able to write some useful functions in Lua. The official site provides a free book and some good information to get you rooted in the language. I hope Lua becomes another language in your toolbox, if it is not one already.