Stormworks: Build and Rescue Wiki
Advertisement

This article is a stub. You can help Stormworks: Build and Rescue Wiki by expanding it.

Learning Lua as a beginner

Learning Lua as a beginner   [ edit page ]



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


myVariable = nil myVariable = true myVariable = 3.141 myVariable = {"a","b","c"}



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:

(3 == 3.000000001) --true


Comments

comments are not executed:

-- this is a single line comment a = 1 + 2 -- this is also a comment, but the code before "--" will be executed




--[[ this is a multiline comment for long comments it makes sense to use it, instead of prefixing every line with -- still part of the comment ]]-- a = 1 + 2 -- this is also a comment, but the code before "--" will be executed



Math calculations / Arithmetic


myVariable = 1 + 2 myVariable = 2 - 1 myVariable = 2 * 4 -- multiplication, 2*4 = 8 myVariable = 9 / 4 -- division, 9/4 = 2.25 myVariable = 9 % 4 -- rest after a division, 9/4 => 2*4 + 1, so finally 9%4 = 1 myVariable = 2 ^ 3 -- 2 powed by 3, 2^3 = 2*2*2



Strings


myTextA = "Hello:" myTextB = "World" myText = myTextA .. myTextB -- myText = "Hello:World"

myText = "Number: " .. 1 -- myTExt = "Number: 1"



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).

myTableA = {"a","b"} -- myTableA[1] = "a", myTableA[2] = "b" myTableB = {x="a"} -- myTableB["x"] = "a", myTableB.x = "a" myTableC = {"a",x="b"} -- myTableC[1] = "a", myTableC.x = "b"



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

#myTableA -- length of myTableA = 2 #myTableB -- length of myTableB = 0 #myTableC -- length of myTableC = 1



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 < 2 => 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:

function myFunction(argument1) return argument1 + 1 end



Loops

The "while" loop:

myTable = {true, true, true, false}

i=1 while i < 2 do -- as long as the i < 2 the loop will run, if that is not the case, the loop will exit. -- this line is called twice i = i + 1 end -- i is now 2



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

for i=1,2 do -- this line is called twice end



You can manually set a step for the loop:

for i=1,2,0.5 do -- this line is called 4 times end



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

for i=1,-5,-1 do -- this line is called 7 times end



You can also loop over the entries of a table:

myTable = {"a","b","c"} for k,v in ipairs(myTable) do -- this line will be called 3 times: -- 1. k=1, v="a" -- 2. k=2, v="b" -- 3. k=3, v="c" end




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


Advertisement