Lua is a programming language designed for simplicity and performance, used by video game and multimedia companies as a front-end scripting language. It's also used by the Awesome window manager, the Far file manager, the Howl text editor, and many more open source projects for its clarity and clean design. Lua is embeddable, too, so you can include Lua code in codebases of another language (such as Java, C, and C++), as well as interact with a rich C API. Whether you want to want to learn Lua to get into the gaming and media industry, or you're just interested in an easy scripting language with no upper limit, Lua is an approachable and powerful programming language.
On Linux, you can install Lua using your package manager. Your distribution may offer old versions of Lua in the interest of backward-compatibility. Install the latest version unless you have a reason to install an old version.
The Luau (with a trailing "u") programming language is a popular fork of Lua 5.1 created by Roblox. If you're using Luau or Lua for Roblox programming, install Lua 5.1.
Getting started with Lua
You write Lua code in a text editor. Don't use a word processor or office application. Use a plain old text editor or IDE, because those save text as literal plain text without any special formatting characters that tend to confuse the programs that need to interpret your code.
Most programs, Lua code included, can be broadly organized into these three sections:
Set up the environment: In this section, you identify the components you want your program to have available. In some programs, the components are just letters and numbers, while in advanced coding they may also include graphics and sound.
Build the engine: In this section, you tell Lua what to do with the components you've provided. In rudimentary programs, data is processed and an answer is produced. In advanced coding, this section runs in a loop while the user interacts with the application.
Go: Your code does nothing unless you tell it to start.
Suppose you want to write a simple Lua script that calculates the chance of a zombie apocalypse (or at least, that produces an arbitrary number you can claim is the chance of a zombie apocalypse). Getting an arbitrary number from a computer is difficult:
-- setup math.randomseed(os.time()) myNumber = math.random(0,100) -- engine function calculate() print("There is a " .. myNumber .. "% chance of a zombie apocalypse today.") end -- go calculate()
setup section, you start a random number generator with
math.randomseed. As the source of randomness, you use the time as reported by your operating system. These are built-in functions of Lua. Nobody starts out knowing all the built-in functions of a programming language, so you find out about them from articles like this one, through task-based Internet searches, and by reading the Lua documentation.
Next, you create a variable. I often prefix variable names with "my" as a reminder to myself that it's something that I created for this application, not something built into Lua. A variable is a changeable (or "mutable," in programming terminology) value. You're claiming space in your computer's RAM and storing data there so you can use it later. In this case, the variable you create uses the
math.random function of Lua to select a number between 0 and 100. Once again, this is a built-in function.
engine section, you create your own function, which I call
calculate. There's nothing special about the term
calculate except that it makes sense in the context of this application. You can name your functions practically anything. Whatever you call it, this is the part of your code that actually does something. Most computer programs have several functions, but this is a simple application, so it has just the one. The
calculate function uses the built-in Lua
..) in Lua indicate a continuation of a statement, similar to what an ellipsis does in English.
go section runs the
calculate function. If you forget to execute a function, then your application never runs. It would be like sitting in a car without turning the ignition.
The section labels
go are comments. Comments in Lua are indicated by two leading dashes (
--), and it's a way for you to expressly tell Lua to ignore that line. In other words,
go aren't Lua keywords, and you don't need them in your code. They're just a useful way to remember how to structure your code.
Try running your application a few times:
$ lua ./zombie.lua There is a 78% chance of a zombie apocalypse today. $ lua ./zombie.lua There is a 10% chance of a zombie apocalypse today.
At a very low level, computers operate according to binary conditions. An electrical signal is either present (1) or not (0). This manifests in code, too, and one of the classic methods modern programming languages provide to express that binary condition is the if-then statement.
An if-then statement causes the computer to analyze the data it has gathered so far, compare it to some arbitrary condition you define, and then take some action based on what it discovers. To make your first Lua application more dynamic, you can add an if-then statement to your function:
function calculate() print("There is a " .. myNumber .. "% chance of a zombie apocalypse today.") if myNumber > 50 then print("Take an umbrella!") else print("It's a good day for gardening!") end end
This if-then statement uses a little basic math to determine what the current run of the application is reporting. If there's more than a 50% chance of a zombie apocalypse, then some helpful advice is provided to the user. Else, the chance must be 50 or less, so different advice is provided. It's a simple condition that has the computer analyze the random number it has generated, and then take one or another action based on the result.
Run the code to see the results:
$ lua ./zombie.lua There is a 71% chance of a zombie apocalypse today. Take an umbrella! $ lua ./zombie.lua There is a 65% chance of a zombie apocalypse today. Take an umbrella! $ lua ./zombie.lua There is a 12% chance of a zombie apocalypse today. It's a good day for gardening!
There are many other kinds of conditional controls you can use in Lua, including repeat-until, while, for, and more.
Nobody starts programming already knowing how to program. If you're interested in learning how to write code, Lua is a great way to start because it's a small language consisting mostly of conveniences to make your fledgling career as a programmer easier. It's not muddled up with lots of edge-case functions that you're not likely to use. Instead, Lua provides the building blocks you need to create your own unique functions that do exactly what you want them to do. Everything you need to know about Lua is available in the language's documentation, but if you want to continue a walk-through of the language, you can also download our eBook. By the end of the book, you'll know all the Lua basics, and where to find more Lua functions so you can move on to advanced Lua programming.