Introduction
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.
Assignment
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
variableName = "new value" | |
variableName = 1 | |
variableName = 'new value' | |
variableName = [] |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
variableName = 1 | |
if variableName == 1 then | |
print("The value is 1") | |
end |
Functions follow the “end” pattern as well.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
variableName = 1 | |
function newFunction() | |
print("The value is 1") | |
end | |
newFunction() |
Tables
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.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
testTable = {} | |
testTable.newKey = "new value" |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
testTable = {} | |
testTable.newKey = "new value" | |
function testTable.newFunction() | |
print("The value is " .. testTable.newKey) | |
end | |
testTable:newFunction() |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
testValue = 1 | |
function newFunction() | |
print("The value is " .. testValue) | |
end | |
newFunction() | |
However, to limit the scope of an identifier, the keyword “local” can be used.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
testValue = 1 | |
function newFunction() | |
— This will work | |
print("The value is " .. testValue) | |
local newValue = 2; | |
end | |
newFunction() | |
— This will NOT work. | |
— newValue is only defined within the scope of newFunction() | |
print("What is newValue?" .. newValue) | |
Loops
The keywords of “for” and “repeat” are used to loop.
Like “if . . . then”, for loops use the “for . . . do” pattern.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
newTable = {1, 2, 3, 4} | |
for i = 1, #newTable do | |
print(newTable[i]) | |
end | |
The keyword “repeat” is paired with “until”.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
newTable = {1, 2, 3, 4} | |
i = 1 | |
repeat | |
print(newTable[i]) | |
i = i + 1 | |
until i == #newTable | |
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.
Modules
Existing functionality in Lua can be augmented through the use of modules. These are added through the function require().
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local module = {} | |
local function printText() | |
print('And this text!') | |
end | |
function module.printText() | |
print('This text!') | |
printText() | |
end | |
return module | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module = require("module") | |
module.printText() | |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Person = {} | |
function Person:new() | |
newObj = {name = 'Dan'} | |
self.__index = self | |
return setmetatable(newObj, self) | |
end | |
function Person:sayName() | |
print('My name is ' .. self.name) | |
end | |
newPerson = Person:new() | |
newPerson:sayName() | |
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).