Create_tables
Empty Table
To create an empty table, use the opening and closing curly braces {}
, like this:
mytable = {}
mytable | |
---|---|
This is useful for preparing a variable to hold the table in your _init()
function during set up. Then use add()
to insert data into the table later.
Below are examples of using tables in different ways and the terms that we use to refer to these different "types" of tables are often confusing because they can mean specific things in other programming languages. Just know that in Lua, these are all simply tables. We do use these terms to help specify how the table is configured, what type of data it holds, or how we will use it. But at the end of the day, they are all just tables.
List
To create a list type of table (a.k.a array), you can write the values inside the curly braces, separated by commas, and let the table automatically number them.
mytable = { "superman", "spiderman", "ironman" }
mytable | |
---|---|
1 | superman |
2 | spiderman |
3 | ironman |
You can also number them yourself, if you need to (and order them yourself):
mytable = { [2]="kirk",
[1]="spock", [3]="scotty" }
mytable | |
---|---|
1 | spock |
2 | kirk |
3 | scotty |
Or you can add numbered keys to any table already created by using square brackets []
after the table name, mytable[ key ]
, like this:
mytable = {} --empty table
mytable[1] = "picard"
mytable[2] = "laforge"
mytable[3] = "worf"
mytable | |
---|---|
1 | picard |
2 | laforge |
3 | worf |
Object
To create an object type of table (a.k.a "dictionary", or "key-value map"), you can name the keys and use any data type as the value; even other tables or functions.
mytable = { sprite=1, x=10, y=5, state="run" }
mytable | |
---|---|
sprite | 1 |
x | 10 |
y | 5 |
state | run |
Another way to write this is vertically, so that it is more readable for more complex object tables:
mytable = {
sprite = 1,
x = 10,
y = 5,
state = "run",
}
When writing your table entries vertically like this, make sure you still separate each entry with a comma. Forgetting a comma is often the cause of the "unclosed {
" error.
Another way to write this type of table vertically, that some people prefer, is to create the empty object table first, then insert each entry separately by using square brackets []
after the table name, mytable[ "key" ]
, like this:
mytable = {}
mytable["sprite"] = 1
mytable["x"] = 10
mytable["y"] = 5
mytable["state"] = "run"
Notice that this way does not require commas and is less likely to cause confusing errors, however it costs more tokens and characters. So if you already know what should be inside the table, it is better to insert the entries at the same time as creating the table.
Collection of Objects
To create a collection type of table (a.k.a. Matrices and Multi-Dimensional Arrays) that holds multiple objects, you will want to use nested tables. (What is a Nested Table?)
mytable= { --open outer table
{} --inner table
} --close outer table
mytable | |||||
---|---|---|---|---|---|
1 |
|
Nested tables can be hard to wrap your head around at first, but the more you work with them, the easier and more logical they will be to use. Here is what it looks like to have 2 object tables inside of a collection.
items= {
{ name="sword", attack="5" }, --inner table 1
{ name="sheild", armor="10" }, --inner table 2
}
items | |||||
---|---|---|---|---|---|
1 |
|
||||
2 |
|
A collection of objects is a great way to create, store, and control multiple objects in your game such as bullets, items, enemies, and particles.
Collection of Bullets:
bullets={ { x=65, y=51 }, { x=22, y=25 }, { x=33, y=15 }, }
Collection of Items:
coins={ { sprite=1, x=10, y=5, name="copper" }, { sprite=2, x=20, y=25, name="silver" }, { sprite=3, x=30, y=15, name="gold" }, }
Collection of Enemies:
enemies={ { sprite=10, x=30, y=15, name="goomba" }, { sprite=11, x=40, y=35, name="koopa" }, { sprite=12, x=50, y=65, name="boo" }, }
These collections of objects can be easily updated and drawn in your game using loops.
Shorthand
You can use the shorthand .
to refer to a table key instead of []
, like this:
mytable[key]
mytable = {}
mytable["sprite"] = 1
mytable["x"] = 10
mytable["y"] = 5
mytable["state"] = "run"
mytable.key
mytable = {}
mytable.sprite = 1
mytable.x = 10
mytable.y = 5
mytable.state = "run"
These two can also be used together to access inner table keys of nested tables, useful when you have a collection of objects (described above).
enemies[1].sprite
This will look at the enemies table, then find the first entry [1]
inside of that, then it will look inside of that inner table and find the sprite
key, and get the value stored there (which would be 6 in the below example table).
enemies | |||||||
---|---|---|---|---|---|---|---|
1 |
|
Related Topics
1731
28 Apr 2024