Lua is a scripting programming language. It is dynamically typed, meaning that there are no types of data, merely different values. All values are also passed by value instead of by reference.
Simple value assignment is the most basic of all operations in Lua. Identifiers (named variables) start with letters and can end with numbers or other letters.
Vital White Space
Like other programming languages like Python and Ruby, white space and line endings are important and serve as delimiters. While semicolons can be used at the end of single statements, the preference is not to use them. Object, function declarations, and conditional statements, however, are defined in part by their white space and tabbing.
Ending With “end”
Conditional statements start with “if” have a “then” and end with an “end”. This pattern of ending with an “end” is common across the language.
Functions follow the “end” pattern as well.
The only data structure in Lua is a table, a combination of object-literal and associative arrays. Key-value pairs can be added through using a reference to the original name. (Values can also be accessed through brackets and the name of the key like an associative arrays.)
Tables are a core functionality of Lua, often used to store and retrieve data. Because of the nature of identifiers in Lua, functions can also be stored as a key-value pair.
Note: String concatenation in Lua uses “..”. In the above example, the initial string “The value is” is concatenated to the value of the variable testTable.newKey.
While functions can be added using the same reference as any other key-value, in order to be called, the “:” (colon) must be used. The reason for this is scope: the use of the colon indicates that it is passing itself. It is called on instead of from the table.
Global and Local
By default, all identifiers are global. Once defined, they can be access in any other lower scope.
However, to limit the scope of an identifier, the keyword “local” can be used.
The keywords of “for” and “repeat” are used to loop.
Like “if . . . then”, for loops use the “for . . . do” pattern.
The keyword “repeat” is paired with “until”.
Note: The character “#” is used in Lua to indicate the length of a table. Acting like an array, both loops proceed until the variable matches the length of the table.
Note: Arrays with at index 1 in Lua! In both examples the variable used in the looping starts with 1 and not 0.
Existing functionality in Lua can be augmented through the use of modules. These are added through the function require().
Modules have their own scope. As in the example, the local module table and printText() function are called and used in their scope. Using the require() function runs and stores the returned “module” in the assignment.
Classes (Sort of)
Lua is not an object-oriented programming language. However, some parts of object-oriented functionality can be emulated through using metatables.
In the example, newObj is used as an internal table. Keys are saved and then re-mapped to the Person object.
Note: In Lua, internal table references start with “self” (it is similar to the keyword “this” in other programming languages).