Wiki/Guides/Lua/Learning Lua as a programer

{{Wiki_Page | content=

After reading this you should proceed with Exploring the Stormworks Lua API

=Theory=

In case you did not read Learning Lua as a beginner {{code-block | content= myVariable = true myVariable = 0.1 }} {{code-block | content = myVariableA = 1 local myVariableB = 2 }} {{code-block | content = myTable = {"a", "b"} -- myTable[1] = "a" &#35;myTable -- length of myTable = 2 }}
 * Lua variables have dynamic types:
 * Lua variables do not need to be declared, but can (it influences scope):
 * all index do start at 1, not at 0 (same is valid for strings: the first character is at position 1)!

Scope

 * by default, everything is in the global scope
 * you can declare variables to be local

=Practical Examples= In the Lua IDE you can use the function. It's not supported in Stormworks, but it helps to create and debugs scripts in the Lua IDE.

Basic Math
Copy this code into the Lua IDE and run it. If you scroll down a little bit, you can see the console output, now showing "3".

String formatting / Cutting away digits of numbers
Let's say you want to show a battery % label, from 1.0-100.0%. But the actual battery level is 0.12345 which would result in 12.345%.

Create your own functions
This function simply adds 1 to the argument numb and returns the result.

Advanced table functions
table.insert helps us to insert a new entry to an existing table:

The opposite of table.insert is table.remove, it removes an existing entry of an existing table.

After reading this you should proceed with Exploring the Stormworks Lua API

}}