Learning Lua (5.3.4)

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.

variableName = "new value"
variableName = 1
variableName = 'new value'
variableName = []

view raw
identifiers.lua
hosted with ❤ by GitHub

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.

variableName = 1
if variableName == 1 then
print("The value is 1")
end

view raw
conditional.lua
hosted with ❤ by GitHub

Functions follow the “end” pattern as well.

variableName = 1
function newFunction()
print("The value is 1")
end
newFunction()

view raw
function.lua
hosted with ❤ by GitHub

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

testTable = {}
testTable.newKey = "new value"

view raw
table.lua
hosted with ❤ by GitHub

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.

testTable = {}
testTable.newKey = "new value"
function testTable.newFunction()
print("The value is " .. testTable.newKey)
end
testTable:newFunction()

view raw
tablefunction.lua
hosted with ❤ by GitHub

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.

testValue = 1
function newFunction()
print("The value is " .. testValue)
end
newFunction()

view raw
functionscope.lua
hosted with ❤ by GitHub

However, to limit the scope of an identifier, the keyword “local” can be used.

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)

view raw
scopeproblem.lua
hosted with ❤ by GitHub

Loops

The keywords of “for” and “repeat” are used to loop.

Like “if . . . then”, for loops use the “for . . . do” pattern.

newTable = {1, 2, 3, 4}
for i = 1, #newTable do
print(newTable[i])
end

view raw
for.lua
hosted with ❤ by GitHub

The keyword “repeat” is paired with “until”.

newTable = {1, 2, 3, 4}
i = 1
repeat
print(newTable[i])
i = i + 1
until i == #newTable

view raw
repeat.lua
hosted with ❤ by GitHub

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

local module = {}
local function printText()
print('And this text!')
end
function module.printText()
print('This text!')
printText()
end
return module

view raw
module.lua
hosted with ❤ by GitHub

module = require("module")
module.printText()

view raw
main.lua
hosted with ❤ by GitHub

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.

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

view raw
person.lua
hosted with ❤ by GitHub

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