LUA Scripting

**LUA** is a scripting language used in Modding Hegemony III.

Lua ( Portuguese for moon ) is an imperative and extensible scripting language for integration into programs so that they can be further developed and maintained more easily. One of the special features of Lua is the small size of the compiled script interpreter. Lua programs are mostly platform-independent and are translated into bytecode before they are executed. Although Lua can also be used to write standalone programs, it is primarily designed as an embedded scripting language for other programs.

# Conventions

## Basics

* Lua is a case sensitive language. "and", "And" or "AND" are not the same.
* Literal strings can be delimited by matching single or double quotes (e.g. 'hello' or "hello")
* A comment starts with a double hyphen (--) anywhere outside of a string. e.g.: `a=4 -- variable a is now 4!`
* Commands in Lua can be terminated with a semicolon . If several commands are in one line, this makes the code easier to read.

* Following strings denote other tokens:

```
+     -     *     /     %     ^     #
==    ~=    <=    >=    <     >     =
(     )     {     }     [     ]
;     :     ,     .     ..    ...

```

## Reserved Keywords

* Following are Lua keywords `and - break - do - else - elseif - end - false - for - function - goto - if - in - local - nil - not - or - repeat - return - then - true - until - while`

```
and       break     do        else      elseif
end       false     for       function  if
in        local     nil       not       or
repeat    return    then      true      until     while

```

* The keyword nil stands for false and generally for uninitialized variables or non-interpretable values.

# Variables and Data Types

## Variables

* There are 3 kinds of variables: global variables, local variables and table fields. Any variable is assumed to be global unless explicitely declared as local
* Before the first assignment to a variable, its value is nil
* Square brackets are used to index a table (e.g. `value=table[x]`). The first value in a table is at position 1 (and not 0 as for C arrays)

## Data types

* In addition to the data types nil , boolean , number (with the internal subtypes integer and float ), string , function, userdata and thread , Lua recognizes the only structured data type table (table).
* Lua is a dynamically typed language which means that variables do not have types; only values do. There are 8 basic types in Lua:

```
nil       type of the value nil whose main property is to be different from any other value. It usually represents the absence of a useful value
boolean   values false and true (both nil and false make a condition false; any other value makes it true)
number    real numbers
string    arrays of characters (strings may contain any 8-bit character, including embedded zeros)
function  Lua functions
userdata  can hold arbitrary C data (corresponds to a block of raw memory)
thread    independent threads of execution used to implement coroutines
table     arrays that can hold values of any type except nil

```

# Functions and Statements

* Functions can be created with the keyword function. Functions do not have a fixed number of parameters and return values.

* Lua allows multiple assignments. The syntax for assignments defines a list of variables on the left side and a list of expressions on the right side. The elements in both lists are separated by commas:

`x,y,z = myTable[1],myTable[2],myTable[3]`

## Operators

* The syntax is based on that of Pascal , which makes it easier for beginners to get started with Lua. In contrast to languages ​​derived from Pascal, Lua uses "==" and not "=" as a comparison operator, "=" as an assignment operator (instead of ": =" in Pascal) and "~ =" for unequal (instead of "<>." ").
* Relational operators (always result in false or true)

```
==     equality
~=     negation of equality
<      smaller than
>      bigger than
<=     smaller or equal than
>=     bigger or equal than

```

## Control Structures

* If control structure (by example):

```
if value1==value2 then
   print('value1 and value2 are same!')
end

```

* If-else control structure (by example):

```
if value1==value2 then
   print('value1 and value2 are same!')
elseif value1>value2 then
   print('value1 is greater value2!')
else
   print('value1 is smaller value2!')
end

```

* For control structure (by example):

```
for i=1,4,1 do -- count from 1 to 4 with increments of 1
   print(i)
end

```

* While control structure (by example):

```
i=0
while i~=4 do
   i=i+1
end

```

* Repeat control structure (by example):

```
i=0
repeat
   i=i+1
until i==4

```

## Other

* Table operations (by example):

```
myTable={'firstValue',2,3} -- builds a table with 3 values
print(myTable[1]) -- prints the first element in the table
table.insert(myTable,4) -- appends the number 4 to the table

```

* Concatenation (by example):

```
a=' hello'
b=' world'
c=a..b -- c contains 'hello world'

```

* Length operator #:

```
stringLength=#'hello world'
tableSize=#{1,2,3,4,5}

```

* String manipulation

```
a = "one string"
b = string.gsub(a, "one", "another")  -- change string parts
print(a)       --> one string
print(b)       --> another string

```

# Links

The Following sites provide information on how to script in LUA.

* Introductory Wikipedia Page for LUA
* Programming in Lua (online book)
* GitHub LUA Repostitory "Awesome LUA"
* Official LUA FAQ
* Inofficial LUA FAQ