Basic notions of Lua programming

By Samir Tine, published on December 2021

With Luart, you can easily program in Windows using the Lua language. Still, you'll need to know coding in Lua! During this tutorial, you will be able to learn the fundamentals of programming with Lua, which will then enable you to then become familiar with Luart and start creating your own applications. Let's go !

Let's start with some prehistory...

Programming has always been about solving problems. As the Cro-Magnon man used stones to make fire, modern man uses a computer to facilitate his daily activities. Using tools by humans always follows the same pattern, the same sequence of steps to achieve (or not achieve, if there is an error!) the desired result.

To cook meat, prehistoric people prepared leaves and twigs first, then rubbed flints together to make a spark and blew on them to make embers. Today, to write an email, the modern man starts by turning on his computer, logging into his email account, and then writes it, before sending it.

Here are two diagrams, one representing cooking meat and one representing sending an email (can we speak of humanity's progress here?). Essentially, these are instruction sequences since the instructions must be followed one after another. That's what programming is all about!

Sticky note

A program consists of a series of instructions executed one after another.

A modern approach to complexity

Using voice assistants nowadays makes it easier to program your smartphone or computer to do certain tasks: "send Lisa an email" or "wake me up at 6 am tomorrow". It's sort of like programming, but you cannot express the inventiveness that makes you a man (or woman).

With the voice assistant, you can only program what was previously defined as being programmable (send an email, set an alarm clock, etc.).

Programming languages, such as Lua, provide more flexibility by allowing you to define instruction sequences in much more precise manners, while also being particularly simple. Just as voice assistants translate your words into actions, Lua translates your instructions into actions our computers can understand (don't tell me you thought you were really talking with your phone!). To accomplish this, Luart uses an interpreter.

Sticky note

An interpreter translates your instructions into actions that are executed by the computer.
Luart uses an interpreter named luart.exe. In its hands, your instructions will be executed one after another. Wait a minute, how am I going to give it my instructions ? A voice assistant speaks with the voice of his master (hence modern slavery), a Luart interpreter will use a text file in which you will specify the instructions he must perform.


You can run your program with Luart by entering luart.exe myprogram.lua in a console window or by double-clicking the file in Windows Explorer.

Your first program

Come on, you will finally be able to write your first program !

"Hey Google, how do we write a program" ?
You guess right, not like that ! Ok ok I will not use the voice assistant example in my programming tutorials anymore...

The first step involves creating a text file (for example, using the famous Notepad) and saving it as "myprogram.lua". Write the following instructions in this file :

print("Listen !") sys.beep()

Go to the directory where you saved your program, then run it in a console window with the following command :

luart.exe myprogram.lua

You should see a message "Listen !" on the screen and then hear a beep. Great, your program was executed sequentially as we requested !

The first line
print ("Listen !")

In the first line of the program, the computer is asked to perform an action called print on the screen. Why are there parentheses around the command? Through parentheses, the interpreter is instructed not to print just anything, but rather to print the word inside the double quotes"Listen!".

Just like when you go shopping, you must open your wallet in order to pay. You can then choose whichever method of payment you want, cash or credit card. Well, in Lua it is the same: opening parentheses tells what the action will be working with.

With the same analogy, Lua calls the means of payments arguments. The action with parentheses will then be executed depending on these arguments. The action in question is actually called a function in Lua (reminds us of our math lessons without a doubt !).

Sticky note

A function allows to perform an action based on arguments enclosed in parentheses.
In summary, your program's first line instructs the interpreter:
printHere is the name of a function
(Execute the function with the following arguments
"Listen !"The first argument is a quoted sentence
)No more arguments, execute the function now !

Programming languages have a very specific grammar with words and expressions that are different from our own language, and this is the reason why we need to learn them in order to program. The language we are describing is Lua. Regardless of what language is used, it cannot be translated to other languages like javascript, C, or assembler, whose syntax has to be learned before using them. Hopefully, the concepts remain the same, such as the concept of function.

One can ask at this point: where is the function print ? Indeed your program does not indicate to the interpreter where the function is, to be able to run it. It is actually a basic function available throughout your program (it is called a library function part of a whole arsenal of functions already integrated into the interpreter and therefore immediately available to make your life easier. This arsenal is actually called the runtime library). This library does not store books but functions ready to be executed by your programs.

Sticky note

Luart provides a much larger library of functions than the standard version of Lua, allowing users to do many more actions in your programs without additional effort.

Second line

Good! The first line of the program covered so many concepts! I assure you, the few concepts you have just learned will help you understand the rest better. According to the first instruction's syntax, and by analogy, we can describe this second line as follows:

sys.beepHere is the name of a function
(Execute the function with the following arguments
)No arguments, execute the function now !

I think this is rather clear if you have followed along. Not quite yet, though.

Perhaps you are wondering why there is a period . in the middle of a function name. The period is usually used to separate two sentences in human language. Good point! In Lua language, a dot is also used to separate instructions, not sentences. What are sys and beep in this case? Since it is followed by parentheses, beep is the function that the interpreter will execute. But then what is sys?

Let's answer this question! Change the second line in your program that way :
print("Listen !") beep()

Run your program by typing luart.exe myprogram.lua in the console window ... and a disaster occurs! Rather than hearing a beep, you just saw a strange error message instead!

myprogram.lua:2: attempt to call a nil value (global 'beep')

Let's see. The message indicates that the interpreter tried to execute the beep function on line 2 but ended up executing a nil value instead! That is to say, the interpreter found nothing but absolute nothing, the interstellar void !

Do you remember the print function? It has been executed, however, because the interpreter stores it in the overall library space (a sort of central aisle), which makes it accessible anywhere in your program (In Lua we call it a global function). In contrast, there is a beep function in the interpreter, but it is stored in a separate library shelf.

Yes, it is in the sys aisle, as you probably guessed. Without this primordial indication, the interpreter couldn't find the beep function.

The dot in sys.beep tells the interpreter that the beep function is located in the sys aisle of the Luart runtime library.

Sticky note

The point, in Lua, is used to classify elements, for example functions, in the standard library.

Keeping all this in mind, here is how the interpreter interprets your first program:

print(I order you to execute that function from the central aisle of the library, using the following arguments
"Listen !"The first argument is a quoted sentence
)No more arguments, execute the function now !
sys.Here is the name of a shelf in the library
sys.beep(Here is the name of a function from this shelf that I order you to execute, using the following arguments
)No arguments, execute the function now !

A punctuation symbol, which follows a word in Lua, offers information to the interpreter: an open parenthesis indicates a function to be executed. The dot indicates that the word preceding the dot is a shelf, a storage area.

In conclusion

What a wide range of concepts this tutorial covers ! If you have followed along correctly, you have already grasped some important Lua concepts:

  • A Lua program is sequence of instructions executed by an interpreter
  • A function is an action that is executed (or we prefer the term 'called') by providing arguments in parentheses
  • The Luart interpreter provides plenty of ready-to-use functions, stored in a runtime library
  • This library has accessible storage shelves when using a dot
  • This library has a central aisle where the functions are accessible without the need to use the dot (they are global functions)
  • If a function is not found in the library, it does not exists, it's nothing
  • Nothing is called nil in Lua
  • A text sentence is placed in double quotes in Lua
  • The print function is used to display text on the screen
  • The sys.beep function is used to emit a beep

After reading the first part of this tutorial, it is time to take a short break and go back to your real life, to enjoy your loved ones for example, before diving into the second part with curiosity !