Why you should try programming in Lua

illustrations illustrations illustrations illustrations illustrations illustrations illustrations
post-thumb

Published on 21 December 2023 by Andrew Owen (4 minutes)

Let me start by saying that I don’t have anything against Python. It’s the number one programming language for a reason. But I learned Perl before Python was invented, and I’ve never had a compelling reason to learn it. On the other hand, when I worked in video games, I needed to get familiar with Lua and I found it very much to my liking.

Lua (meaning moon in Portuguese) is 30 years old this year. It was created by Waldemar Celes, Luiz Henrique de Figueiredo and Roberto Ierusalimschy at the Computer Graphics Technology Group of the Pontifical Catholic University of Rio de Janeiro. Brazil’s trade barriers for computer software and hardware meant that it was natural for the department to create its own efficient, lightweight, embeddable scripting language.

Because many potential users of the language were not professional programmers, the language should avoid cryptic syntax and semantics. The implementation of the new language should be highly portable, because Tecgraf’s clients had a very diverse collection of computer platforms. Finally, since we expected that other Tecgraf products would also need to embed a scripting language, the new language should follow the example of SOL and be provided as a library with a C API.—“The Evolutino of Lua”

In its current version, Lua supports data-driven, functional, object-oriented and procedural programming. It combines simple procedural syntax with data description constructs based on associative arrays and extensible semantics. It’s dynamically typed, runs by interpreting byte code with a register-based virtual machine and has automatic memory management with incremental garbage collection.

During the seventh console generation (PlayStation 3, Wii, Xbox 360), one games industry survey found that more than half its respondents were using Lua for scripting. This makes a lot of sense when you consider that the leading game engine at the time was Unreal, and the leading game development language was C/C++. More than a fifth of respondents were also using Lua for rapid prototyping. Not only is Lua fast, portable, embeddable, simple but powerful, it’s also small at around 30000 lines of code. And it’s distributed under the MIT License.

When parents decided to teach their kids programming, they will often start with block-based languages like Scratch. These can teach programming principles and generate an enthusiasm for programming. But prolonged exposure could make it harder to transition to line-based programming. Python is a popular choice of teaching language, except with mean Gen X parents who make their kids use BASIC. Lua is a little more niche, but it has some cool add-ons like the Lapis web framework and the LÖVE 2D-games framework, and it’s used in some interesting places:

There are two commercial projects I’m particularly fond of. For iPad owners, Codea gives you a way to develop apps directly on the device. It was created by University of Adelaide PhD students John Millard, Simeon Saëns and Dylan Sale. It’s mainly aimed at games development, but it can be used for general purpose apps too. Complete projects can be exported directly into Xcode. It supports 3D through voxels and models in Wavefront format (as exported by Blender). Its frameworks include animation, graphics, location, motion, multitouch, network, physics, sound and storage. The first ever app store app made entirely on the iPad was made with Codea. Although currently, the only featured game still available is Cargo-Bot. It has comprehensive documentation and comes with sample projects to get you started.

For people who like the retro aesthetic but don’t want to learn 6502 or Z80 assembly language, the Pico-8 is a fantasy console that runs in a web browser or on Linux, macOS and Windows. It has a 128×128 pixels display with 16 colors from a palette of 256 colors. There are 256 8×8 pixel sprites and a 128×32 tile map. It has a 4-channel programmable sound generator. There are editors for code, music, sound, sprites and maps built into the console. Cartridges are stored as PNG files. Pico-8 also includes licensing for educators.

Afterword

As is often the case, I learned about another project after publishing. The TIC-80 is an open source alternative to the Pico-8 that also ssupports Lua. It too has built-in editors for code, sprites, maps, sound and so on. It has a 240×136 pixel display with a fixed 16 color palette that is quite similar to the DawnBringer 16 palette. The TIC-80 has proved particularly popular at in the computer demoscene.