- Part 1: Variables and Conditional Statements
- Part 2: Functions and Tables
- Part 3: Objects and Looping
Lua is a multi-paradigm scripting language originally designed to be embedded as part of other, existing programs. Often used for this purpose, Lua can be found in everything from photo editing applications to used as an internals scripting language of video games like World of Warcraft.
Lua is not an object-oriented programming language. Code concepts within the language are not made to support that model by default, and Lua does not have the keyword class. However, the idea of objects can still be implemented in Lua through tables.
Some programming languages have a concept of an object literal. In those languages, it is possible to define an object through listing its properties (or attributes, depending on the language) when it is defined. The object then “has” those properties and they can be accessed as part of the object. In Lua, a table, as a set of key-value pairs, can be treated in the same way.
Properties of an object can be used through the dot-notation to mark they are a “part” of an object. When used, their values would be defined.
The Complication of self
Being able to define the equivalent of an object literal using a table is not as helpful as it could be when creating new objects. Creating multiple objects would mean writing new tables per object. There is also another complication.
Like many other programming languages, Lua understands referencing other properties of an object within itself. In some languages, the keyword is this. In Lua, the same functionality uses self.
In Lua, the keyword self (to set values of internal properties) and the function setmetatable() can be used to connect a new object with an exist one.
The setmetatable() function allows for changing the functionality of the table passed to it. Using the name of the object and a new, empty table, the two are bound together. It creates a new table of a type based on the second parameter. In other words, it sets the new object as connected (has the same properties and functions) as the old one. The difference is that because it is a new empty table, it does not have the same values.
Using these, new object-like entries can be created in Lua that follow the same behaviors as objects in other programming languages. The big noticeable difference is the use of the colon instead of the period.
The use of the colon when defining or using a function in Lua tells it that the function is part of an existing table. When that function is called using the colon between it and its object name, it also tells Lua to send an additional, hidden parameter called self of the object on which it was called. This is a syntax shorthand, but also serves to show the difference between a function (uses a colon) and a property (uses a period) for an object.
Local and Global
By default, Lua supports two types of understanding of variables. They are either global (the default) or local to a function. When doing calculations or a complex task, it is often helpful to have variables within a function that might have the same name as those outside of it. In those cases, the keyword local can be used in front of a variable to mark it as only existing inside the function.
It is often very useful to run chunks of code a set number of times. For that task, Lua has a for…do loop.
Because the task of running a loop from a starting number up to some maximum is incredibly common in programming tasks, Lua has a syntax shortcut. When writing a for…do loop, the second, often conditional statement, can be shortened to just its result. A loop that was to run from 0 to 10, for example, would only need to have i = 0, 10 between the for and do keywords.
Tables as Arrays
To move through a table as if it were an array (that is, only containing numerical values), the function ipairs() can be used as part of a more generic for…do loop structure. In these cases, the central variable would have the value of each entry as it moved through them.
Tables as Key-Value Pairs
Similar to the use of the function ipairs() for numbers, the function pairs() can be used for sets of key-value pairs in a table. In a more generic usage of the for…do loop in Lua, the function can be used to move through and show each key-value pair in turn through using the key from the looping through pairs() with its value when used with square brackets and the table’s name.