The name 'Lua' comes from the Por­tuguese word for “moon”. This pro­gram­ming language can be extended in­de­pend­ently of the platform. Its small size of just 120 kilobytes and high speed set it apart, which is reason enough to take a closer look at this scripting language.

This article will provide a general in­tro­duc­tion to the scripting language Lua. You can also find a detailed Lua tutorial in the Digital Guide to get started with this pro­gram­ming language.

What is Lua?

The world of internet pro­gram­ming languages seems endless. Lua manages to stand out from the crowd, but it is still re­l­at­ively unknown next to Python or Perl. This language has been used for well over two decades. However, it’s rarely used as a stan­dalone pro­gram­ming language. Instead, de­velopers usually use it as an embedded scripting language for in­di­vidu­al programs. Lua has mainly been used in video games and game engines, but it’s also used as a pro­gram­ming language in many network and system programs.

There is a reason behind its Por­tuguese name. Lua was developed at the Catholic Uni­ver­sity of Rio de Janeiro by Roberto Ier­us­alim­schy, Luiz Henrique de Figueiredo, and Waldemar Celes. Brazil was subject to strict import reg­u­la­tions for hardware and software until 1992, so out of sheer necessity, these three pro­gram­mers developed their own scripting language called Lua. Today, it is one of the most important and popular pro­gram­ming languages in many sectors.

So, what exactly is Lua? Lua is es­sen­tially a library that pro­gram­mers can integrate into their software to make it script­able. Lua is supported by every standard operating system. It contains a highly compact in­ter­pret­er which only requires a few hundred lines of code and can also access the library. Despite its min­im­al­ist design, the in­ter­pret­er offers useful features such as auto­mat­ic­ally cleaning up data struc­tures that are no longer used in order to free up valuable memory. The following is a summary of Lua’s most important char­ac­ter­ist­ics:

  • Compact
  • Fast in execution
  • Highly ex­tend­able
  • Easy to learn
  • Free to use

How is the pro­gram­ming language Lua struc­tured and what is it used for?

The scripting language Lua is used for general pro­ced­ur­al pro­gram­ming. As such, it’s con­sidered to be an im­per­at­ive scripting language among the pro­gram­ming paradigms. It is im­ple­men­ted as a library in C and offers functions such as Lua code and a sample host program called Lua, which provides a stan­dalone Lua in­ter­pret­er.

As a scripting language, Lua does not have its own main program. It works ex­clus­ively as an embedded part of a host ap­plic­a­tion. The ap­plic­a­tion can then call the pre­vi­ously mentioned functions via the Lua code. Thanks to the C function, Lua can be extended with numerous functions to cover the equally numerous re­quire­ments in pro­gram­ming. This pro­gram­ming language can be easily adapted to ensure that there is a common syn­tactic­al web framework.

If you have ex­per­i­ence with Pascal, you will have no problem with Lua’s syntax, since these two pro­gram­ming languages are quite similar in this regard. In its syntax, multiple commands can be entered into a single line of script separated by semi­colons. Lua has a total of 20 defined keywords which are easy to learn. For example, Lua generally uses function blocks that start with keywords like goto and end with end, elseif, or else. The keyword local indicates local variables that are only valid for the function block in which the keyword occurs. Function blocks can also be nested within one another.

Lua can work with the following data types: table, userdata, function, thread, nil, boolean, string, and number. Out of this list, table is the only struc­tured data type. Variable types are de­term­ined during script execution but can be converted at any time if needed.

When is Lua used?

Due to the lack of lim­it­a­tions placed on de­velopers, the scope of ap­plic­a­tions for the scripting language Lua is so vast that the sky is the limit. Lua can be extended virtually endlessly through user-defined modules. Ap­plic­a­tions range from web servers pro­grammed in Lua to pro­gram­ming your own apps and video game de­vel­op­ment.

In the video game industry, Lua is the pro­gram­ming language used most often. Numerous games use Lua as their scripting language to keep con­fig­ur­a­tions and game char­ac­ters separate. This makes a game engine more flexible, enabling multiple games to use the same engine. Popular examples include the classic MMORPG World of Warcraft and the equally suc­cess­ful puzzle game Enigma.

Another major ap­plic­a­tion of Lua is network and system pro­gram­ming. Lua is used in programs for con­fig­ur­a­tion and auto­ma­tion. Well-known examples include the VLC Media Player and Adobe Photoshop Lightroom. As for networks, network analysers like Wireshark use Lua functions. In addition, the MySQL proxy, the Redis NoSQL database, and the Apache and Nginx web servers also use this scripting language.

Lua – a pro­gram­ming language with major strengths and minor weak­nesses

Con­cep­tu­ally, Lua offers few ground-breaking features as a pro­gram­ming language. Its syntax is quite con­ven­tion­al and rather simple using the 20 dedicated keywords pre­vi­ously mentioned.

Lua is dy­nam­ic­ally typed. The variable’s type is de­term­ined when the script is executed, but it can be converted to another type. This is one of Lua’s major strengths, es­pe­cially in com­par­is­on to CSS or PHP in which, to give an example, the as­sign­ment of any types to 'boolean' (i.e. the logical values) does not follow a specific method.

Lua’s greatest advantage is that all editable text and numbers are written directly as variables. The as­sign­ment is done in a self-contained block at the beginning of the module. The sub­sequent functions only access the variables. If the en­vir­on­ment changes, only the values in the block need to be adjusted ac­cord­ingly. This is the simplest method of pro­gram­ming for the initial creation.

It also has some dis­ad­vant­ages. If a function is changed by an update, the source code will need to be re-written in every project. This means that all pages involved will need to be updated.

Summary

The scripting language Lua can be used to write stan­dalone programs, but it can also be used as an embedded language. Lua may not be as popular as other pro­gram­ming languages, but that’s certainly not due to a lack of pro­gram­ming tools.

A quick look at the scripting language Lua (with code snippets)

Lastly, let us take a quick look at the scripting language Lua by examining two simple examples. These should give you a rough idea of how Lua works.

Example 1: line breaks in Lua

In Lua, you can insert line breaks using ‘\’ or ‘\n’ and a real line break:

print ('1 : Hello\
	World')
print ("2: Hello\
	World")

The resulting output is as follows:

1: Hello
World
2: Hello
World

Example 2: variables in Lua

Variables do not have to be declared in Lua. They are created as needed. As a quick reminder, values can be of the following data types: nil (also the value of variables that are not created), number, string (char­ac­ters, letters, words, etc.), boolean (true/false), table, and function. The following is a simple example of code:

print ( var )
var = "Hello World"
print ( var )

Since undefined variables result in ‘nil’, the output will look as follows:

nil
Hello World
Go to Main Menu