A brief introduction to Lua


1 Basics of Lua

The Lua scripting language was originally designed for extending the functionality of existing applications, but it is often used as a standalone language. It is very similar to Pascal.

The language has the objectives of simplicity, efficiency and portability of code. The full Lua language documentation can be found at http://www.lua.org/manual/5.1/. Below are some basic elements that you need to get started with writing scripts in Lua.


1.1 Structure of the code

In Lua, commands are typed, as in other programming languages ​​in lines one under the other. There is no need for a termination character at the end of each line (however Lua ignores a semicolon at the end of the line, so you do not have to get rid of habits acquired from other programming languages which ​​ require that symbol).


1.2 Declaring local variables

If you want to declare a local variable, type:

local <variable_name>

Alternatively, we can assign an initial value when we declare the variable:

local <variable_name> = <value>

For example:

local var1
local var2 = "aaa"
local var3 = 17


1.3 Conditional Statements

Conditional statements have the form:

if (<conditions>) then
    <commands>
elseif (<conditions>) then
    <commands>
elseif (<conditions>) then
    <commands>
else
    <commands>
end

For example:

if (a == 1) then
    b = 2
    c = 3
elseif (a == 2) then
    b = 3
    c = 4
else
    b = 4
    c = 5
end

This example means:

“If the variable a has a value of 1, then assign the value 2 to the variable b and the value 3 to the variable c,
otherwise, if the variable a has a value of 2, then assign the value 3 to variable b and the value 4 to the variable c,
otherwise assign the value of 4 to variable b and the value of 5 to the variable c


1.3 Operators

Comparison operators:

== equal
~ = different (not equal)
> greater than
< less than
> = greater than or equal
<= Less than or equal

local a = 2
local b = 3
local c = 5
 
-- Displays "a less than or equal to b"
if (a> b) then
    fibaro:debug('a greater than b');
else
    fibaro:debug('a less than or equal to b');
end
 
-- Display: "c equal to 5"
if (c ~= 5) then
    fibaro:debug('c not equal to 5');
else
    fibaro:debug('c equal to 5');
end


Logical operators:

and conjunction
or alternative
not negation

local a = 2
local b = 3
local c = 5
 
-- Displays "true"
if (a == 2 and b == 3) then
    fibaro:debug('true')
else
    fibaro:debug('false')
end
 
-- Displays "true" (because c is equal to 5)
if (a == 5 or b == 5 or c == 5) then
    fibaro:debug('true')
else
    fibaro:debug('false')
end
 
-- Displays "false"
-- (because c is equal to 5 but we want to know if it's NOT the case)
if ( not (a == 5 or b == 5 or c == 5)) then
    fibaro:debug('true')
else
    fibaro:debug('false')
end

The concatenation operator:

To combining strings (text variables) in Lua, we use two dots “..”

For example:

local text1 = "I "
local text2 = "love "
local text3 = "Home Center 2!"
local text4 = "My name is "
 
local result = text1 .. text2 .. text3
local result2 = text4 .. 'Lili.'
 
-- Displays "I love Home Center 2!"
fibaro:debug(result)
 
-- Displays "My name is Lili."
fibaro:debug(result2)


1.4 Types of variables (number vs string)

Lua does not explicitly declare the type of a variable (as is the case, for example, in C / C++).

The type is dynamically determined based on the value that is assigned to the variable:

stringVariable = "3"
numberVariable = 2

stringVariable will create a variable of type string and numberVariable will create a variable of type number.

The coercion mechanism in Lua provides automatic type conversion if possible. This means that if you give the variable stringVariable to a function which expects a number (not text), it will be automatically converted.

However, problems may arise when comparing variables of type number with variables of type string. In such cases, you should explicitly convert the string to a number value using the tonumber function:

-- Not valid
if (stringVariable > numberVariable) then
    fibaro:debug('stringVariable is greater!')
end
-- Not valid
 
-- Assign the numerical value of the variable stringVariable
-- to the variable a
local a = tonumber(stringVariable)
 
-- Now you can easily make a comparison
if (a > numberVariable) then
    fibaro:debug('stringVariable is greater!')
end
 
-- Alternatively, we can do it without an additional variable
if (tonumber(stringValue) > numberVariable) then
    fibaro:debug('stringVariable is greater!')
end

The code on lines 1-5 will generate the following error:

(line 2): attempt to compare number with string