Skip to content

Syntax

The Lua syntax is very easy to learn and understand. If you are new to Lua, you can clearly see that it's very much like english text. Let's take the construction of an if clause as an example:

1
2
3
if EXPRESSION then
    -- do something
end

Very simple to understand, huh?

Let's finally take a look at the syntax.

Operators

Name Symbol(s) Description Example
Addition + Used to add one thing to another 5 + 5
Substraction - Used to substract one thing from another 10 - 6
Multiplication * Used to multiply one thing by another 6 * 9
Division / Used to divide one thing by another 18 / 9
Modulus % Used to get remainder of division 5 % 2
Float-free division // Used to divide one thing by another but without floating point 5 // 2
Exponence ^ Used to power one thing by another 10 ^ 2
Equal == Used to check if one thing equals another a == b
Not equal ~= Used to check if one thing is not equal to another a ~= b
Greater than > Used to check if one thing is greater than another a > b
Less than < Used to check if one thing is less than another a < b
Greater than or equals >= Used to check if one thing is greater or equals to another a >= b
Less than or equals <= Used to check if one thing is less or equals to another a <= b
Logical and and - X and Y
Logical or or - X or Y
Logical not not - not X
Concatenation .. Used to concatenate 2 things 'str1' .. 'str2'
Length # Used to count elements in array or characters in string #table

Commentaries

There are 2 types of comments in Lua - inline and multiline. Inline comments are defined like this:

1
-- this is comment

Everything after -- is counting as comment.

Multiline commends are defined like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
--[[
    This is long and informative
    MU
      LT
        I 
          LI
            NE
              CO
                MM
                  EN
                    T
]]

Variables

In Lua, variables have dynamic types so you don't have to specify them at all. Here are all basic types (which are defined by the Lua engine):

  • nil - type, that defines nothing
  • number - defines all numbers. Floats, integers, doubles - all are of type number
  • boolean - defines a bit. Can be true or false (1 and 0)
  • string - defines a string. Strings are defined by single quotes ' or double quotes "
  • function - defines a function. Can have infinite amount of arguments
  • table - hardest part of Lua. Can work as array, dictionary, class, struct, etc.

Warning

Lua can experience problems with huge numbers on x86 systems. If application is not x64 and 64bit number is being used, last 4 bytes will be truncated or converted to wrong type.

Type of variable is defined automatically by value.

1
2
3
4
5
6
7
8
var = nil               -- type: nil
var = 123               -- type: number
var = -123.665          -- type: number
var = false             -- type: boolean
var = 'hi'              -- type: string
var = "hello"           -- type: string
var = function() end    -- type: function
var = {}                -- type: table

Scopes

A scope is a block of code where something can be accessed. In Lua, a scope can be global or local. Variables/functions that should be only accessable in a local scope must have a local keyword before their name:

1
2
3
4
5
local var = 123                 -- local scope
var = 555                       -- global scope

local function my_func() end    -- local scope
function my_func() end          -- global scope

if, elseif and else

if is a keyword that is being used to compare stuff. It has the following structure:

1
2
3
if X then
    -- Y
end

If you want to add an action that should be executed if the expression is not met, you can use else as keyword:

1
2
3
4
5
if X then
    -- Y
else
    -- Z
end

Note, that you don't have to add then after else!

If you want to add multiple cases, you can use elseif as keyword:

1
2
3
4
5
6
7
if X then
    -- Y
elseif Z then
    -- A
elseif C then
    -- B
end

for

for is being used to loop through an array (usually). It has the following structure:

1
2
3
for VAR = INDEX, COUNT do
    -- ...
end

If you want to loop through an iterable array, it can be done like this:

1
2
3
for VAR in ARRAY do
    -- ...
end

Even if array is not iterable, it can be turned into using pairs (for dictionary) and ipairs functions (for both arrays and dictionaries).

while

while is a loop, that works while its expression is true. The structure is like this:

1
2
3
while X do
    -- ...
end

repeat until

repeat X until Y is a loop that is being used to repeat some action (X) until expression (Y) is met (basically inverted while). The structure is like this:

1
2
3
repeat
    -- ...
until X

Functions

Functions are code blocks that can be called with several arguments. The structure for this part is like this:

1
2
3
function NAME (ARG_1, ARG_2, ARG_N)
    -- ...
end

To call a function, you must put it's name, open brackets, put arguments between and close them. You can do it like this:

1
NAME(0, 'Arg 2', true, {})

This will execute the function (to be correct it's body) with the given arguments