I found tables to be extremely challenging when first used them, so beware of the learning curve.
A table is essentially a container.
In other programmatic containers, a table could be formatted as an array, a dictionary, or tuple, triple, quadruple, a hashset, a hashmap, a vector, fortunately in lua a table can be any of these containers and more, a table could be an entire game.
However, it’s generally the same: a container, or a table, stores data.
Array is a container whose data is indexed by numbers
Dictionary is a container whose data is indexed by a key
A tuple is a two-set container that stores only two-types of data
A triple is a three-set container that stores only three-types of data
A quadruple… etc
A hashset or hashmap is rather ancient version of a dictionary.
A vector is basically an array with better optimizations and interaction.
Containers are very important in programming.
The syntax is as follows:
TableName = {} --// this is an empty table
Or, as an array: TableName = {
[1] = data,
[2] = data,
[3] = data
}
Or, as a dictionary: TableName = {
[“KeyName”] = data,
[“KeyName”] = data,
[“KeyName”] = data
}
And I believe the above can be simplified to: TableName = {
KeyName = data,
KeyName = data,
KeyName = data
}
Or, you don’t like that type of syntax than you may write:
TableName[KeyName] = data
As long as you’re assigning the data than lua automatically creates another area in that table containing the data, however, if you try to access a non-existent area it’ll throw an error.
In lua, a table can contain other tables, which may then contain other tables, which then could contain more other tables, and really, until you run out of RAM could contain as many tables as you can imagine.
So, to do something like that, you would write:
TableName = {
NestedTableName_1 = {
NestedTableName_2 = {
}
}
}
Then you could access the nested tables by accessing the TableName[] operator,
ie: TableName[NestedTableName_1] = “Bob”, “Susan”
What’s important to note is the difference of how to access the data stored in a table depends on how that data is stored. If it’s stored outside of a table, like above, than [1] would be “Bob” [2] would be “Susan”
When written like TableName[NestedTableName_1] = { Man = “Bob”, Woman = “Susan”}
Then you could use the dot operator on TableName[NestedTableName_1] to access the ‘dictionary’ key Man or Woman, like so TableName[NestedTableName_1].Man would retrieve “Bob” or TableName[NestedTableName_1].Woman would retrieve “Susan”
Alternatively, you could access the key directly via TableName[NestedTableName_1][“Man”] as long as it exists it’ll retrieve the data “Bob” but if it doesn’t exist it’ll throw an error
So, long story, the syntax is as follows:
TableName[NestedTableName_1][Data_Or_KeyName]
A table may store any primitive data such as number, string, etc, or any complex object such as Player or Model, or in your case
Example of a complex table:
TableName = {
"Bob",
"Susan",
Men = {
TheBuilder = "bob, the builder",
TheOtherBuilder = "jim, the builder"
},
OtherMen = {
"otherbob, the builder", "otherjim, the builder"
},
}
Wherein TableName[1] == "Bob"
TableName[2] == "Susan"
TableName["Men"][1] == "bob, the builder"
TableName["Men"].TheBuilder == "bob, the builder"
TableName["Men"]["TheOtherBuilder"] == "jim, the builder"
TableName["Men"].TheOtherBuilder == "jim, the builder"
TableName["OtherMen"][1] == "otherbob, the builder"
TableName["OtherMen"][2] == "otherjim, the builder"