Wiki/Guides/Lua/Learning Lua as a beginner

{{Wiki_Page | content=

After reading this you should proceed with Learning Lua as a programer

=Theory= Here we tell you the basics of Lua.

Variables
The type of a variable is set automatically depending on the value. Possible types are:
 * nil
 * boolean
 * number
 * string
 * function
 * table

Numbers are always saved as floats (double precision). So "3" is actually saved as something very similar to "3.00000001". Comparison will of course work:

Comments
comments are not executed:

Tables
Each entry of a table is a combination of a key and a value. The key can be a number or a string. If you do not specify a key for a value, lua will automatically use numbers as keys. If you specify a key, this key will be not be visible in a "for" loop (see Loops) and it will not count in #table (length of table).

Lua calculates the length of a table by starting at table[1] and then counting up until the last table[i] that is not nil. So table={"a","b",nil,"c"} -- #table = 4 but table={"a","b",nil} -- #table = 2

if / else
This is used to execute different code depending on a condition. Any variable or expression can be used as a condition:

true => true

1 > 2 => false  -- greater than

1 true  -- less than

2 == 2 => true  -- equal

3 >= 2 => true  -- greater or equal

3 <= 2 => false  -- less or equal

nil => false  -- nil equals to false, anything else (tables, numbers, string) ALWAYS equal true

{} => true

-1 => true

"" => true

you can combine expressions with the keywords "not", "and", "or"

false or true => true false and true => false not true = false

(false or true) and true => false (false and true) or (true and true) => true

Functions
functions do some stuff and maybe return a value. They can accept input values, so called arguments:

Loops
The "while" loop:

Similar to the while loop is the "for" loop. It can do exactly the same (increment a number):

You can manually set a step for the loop:

And you can choose a different max, even negative (but dont forget to choose a negative step too).

You can also loop over the entries of a table:

After reading this you should proceed with Learning Lua as a programer

}}