Types
Quick Description
nil: A type to denote a value that is nothing, nonexistent, null.
string: A sequence of characters (letters, symbols, numbers).
number: Any kind of number.
boolean: Two types of values, true or false.
function: A chunk of redistributable code to be executed at a later time.
table: The only data structure in the language, holds key, value pairs.
userdata: A chunk of memory which holds C data (Lua's parent language).
thread: A reference to a coroutine.
More Information & Examples
Since userdata and threads are advanced and are rarely used, they will not be covered here.
nil
string
Escape Sequences
Escape sequences are a way to translate its sequence into a different character that may be difficult or impossible to respresent in a string. In Lua, the escape sequence is denoted by a backslash followed by a special character.
\": double quote
\': single quote
\\: backslash
: new line
The same can be done with single quotes:
Inserting literal backslashes into a string:
And finally, newline:
Here is a string that cannot have any escape sequences in it, everything is interpreted as raw characters.
number
The main thing that might not be obvious is the use of e
, which means the following number is an exponent.
boolean
table
Tables are (in my opinion) the most fun & complex data type Lua has to offer. They hold key, value pairs of data which can let you hold any kind of data you want in any order or way you'd like. The easiest way to learn how it works, is by thinking of tables as a ton of storage lockers. With a storage locker, you need a key to access what's inside of it, the same concept applies to tables. The keys for tables can be any type of value (except nil
), and the value can be any type of value. All values must be separated by a comma.
Keys are denoted by either [key]
or key
. If square brackets are used, the key can be of any type, but writing the key plainly is a shortcut for a key with a string
type. Here's a simple example of an array of numbers:
You can see here that no keys are explicitly defined. If you don't explicitly define keys, they are automatically ordered starting from 1. Here's the exact same array but written with keys to show what it looks like:
Both are equivalent, however, the first one is much shorter and easier to look at if you understand how it works internally.
Here is an example of what a player might look like for a game:
Each key in this example is a string
type, since no brackets are used. The exact same can be written with square brackets like so:
Tables can also be nested (a table inside of a table), here's an example:
Again, since there's no explicit definition of the keys, they are automatically ordered starting from 1. To access tables, you can use either the table[key]
syntax, or table.key
. The difference between both is the same concept as before: with square brackets the key can be of any type, with the period the key must be a string
. Here's an example:
function
Functions are extremely powerful, they allow you to reuse code at any time of your choosing instead of having to copy/paste a set of code over and over again, which results in cleaner and more organized code.
Take this function for example, this defines a new function called average
which calculates the average of the parameters passed (a & b), and uses the return
keyword to give back a value which we use to show its output to the console.
You will see the word "call" many times throughout this book, calling means to invoke or execute a function. To call a function, use parentheses + any parameters you'd like to pass after writing the name of the function, like so: function_name(param1, param2, etc)
When passing an argument to a function, it is first evaluated before the value is inserted as the parameter. In the example above, print
is called and the result of average(5, 7)
is inserted as the first parameter. However, if you don't call the function, it would give an output like this:
The reason it outputs the function itself, is because it isn't called. We're simply passing the function itself to the first parameter of print instead of calling it to get a result from it.
Functions can also be a part of tables, and can be explicitly defined inside of said table within the function name using the .
syntax used for tables normally:
This will be prudent in the future since Lua's standard libraries include functions inside of tables, such as table.insert
, which is part of the table
library.
Now to introduce variadic functions. These work the same as normal functions, except they have a special parameter (which is ...
) that holds any number of values passed to it. The best example of this is the print
function, it holds any number of parameters and outputs all of them in sequence to the console. The print function is basically written as this:
The variadic parameter doesn't have to be the only one in the parameter list, it can be used to hold any values after a specific set of arguments you'd like to take in first, like so:
Parameters a & b hold 1 & 2 respectively, while the rest of the arguments are held in ...
, which can't be accessed out-of-the-box like a & b can.
Last updated