The name 'Lua' comes from the Portuguese word for “moon”. This programming language can be extended independently 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 introduction to the scripting language Lua. You can also find a detailed Lua tutorial in the Digital Guide to get started with this programming language.
What is Lua?
The world of internet programming languages seems endless. Lua manages to stand out from the crowd, but it is still relatively unknown next to Python or Perl. This language has been used for well over two decades. However, it’s rarely used as a standalone programming language. Instead, developers usually use it as an embedded scripting language for individual programs. Lua has mainly been used in video games and game engines, but it’s also used as a programming language in many network and system programs.
There is a reason behind its Portuguese name. Lua was developed at the Catholic University of Rio de Janeiro by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, and Waldemar Celes. Brazil was subject to strict import regulations for hardware and software until 1992, so out of sheer necessity, these three programmers developed their own scripting language called Lua. Today, it is one of the most important and popular programming languages in many sectors.
So, what exactly is Lua? Lua is essentially a library that programmers can integrate into their software to make it scriptable.Lua is supported by every standard operating system. It contains a highly compact interpreter which only requires a few hundred lines of code and can also access the library. Despite its minimalist design, the interpreter offers useful features such as automatically cleaning up data structures that are no longer used in order to free up valuable memory. The following is a summary of Lua’s most important characteristics:
- Fast in execution
- Highly extendable
- Easy to learn
- Free to use
How is the programming language Lua structured and what is it used for?
The scripting language Lua is used for general procedural programming. As such, it’s considered to be an imperative scripting language among the programming paradigms. It is implemented as a library in C and offers functions such as Lua code and a sample host program called Lua, which provides a standalone Lua interpreter.
As a scripting language, Lua does not have its own main program. It works exclusively as an embedded part of a host application. The application can then call the previously mentioned functions via the Lua code. Thanks to the C function, Lua can be extended with numerous functions to cover the equally numerous requirements in programming. This programming language can be easily adapted to ensure that there is a common syntactical web framework.
If you have experience with Pascal, you will have no problem with Lua’s syntax, since these two programming languages are quite similar in this regard. In its syntax, multiple commands can be entered into a single line of script separated by semicolons. 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 structured data type. Variable types are determined during script execution but can be converted at any time if needed.
When is Lua used?
Due to the lack of limitations placed on developers, the scope of applications for the scripting language Lua is so vast that the sky is the limit. Lua can be extended virtually endlessly through user-defined modules.Applications range from web servers programmed in Lua to programming your own apps and video game development.
In the video game industry, Lua is the programming language used most often. Numerous games use Lua as their scripting language to keep configurations and game characters 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 successful puzzle game Enigma.
Another major application of Lua is network and system programming. Lua is used in programs for configuration and automation. 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 programming language with major strengths and minor weaknesses
Conceptually, Lua offers few ground-breaking features as a programming language. Its syntax is quite conventional and rather simple using the 20 dedicated keywords previously mentioned.
Lua is dynamically typed. The variable’s type is determined when the script is executed, but it can be converted to another type. This is one of Lua’s major strengths, especially in comparison to CSS or PHP in which, to give an example, the assignment 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 assignment is done in a self-contained block at the beginning of the module. The subsequent functions only access the variables. If the environment changes, only the values in the block need to be adjusted accordingly. This is the simplest method of programming for the initial creation.
It also has some disadvantages.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.
The scripting language Lua can be used to write standalone programs, but it can also be used as an embedded language. Lua may not be as popular as other programming languages, but that’s certainly not due to a lack of programming 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 (characters, 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