Require help with tables
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Require help with tables
I have searched around the forums and other places online, but I can't find any information on tables that I can get my head around. Does anyone know of any good information which explains tables well for use in Love? Thanks a lot!
Re: Require help with tables
Tables in LOVE are the same than Lua (the language code behind LOVE) tables:Jaykin wrote:I have searched around the forums and other places online, but I can't find any information on tables that I can get my head around. Does anyone know of any good information which explains tables well for use in Love? Thanks a lot!
http://lua-users.org/wiki/TablesTutorial
http://lua.gts-stolberg.de/en/Tables.php
http://www.troubleshooters.com/codecorn ... tables.htm
There's a lot more, but those should be good for now.
Re: Require help with tables
I do vaguely understand the concept generally, it's just applying it to Love I find difficult.
- Jasoco
- Inner party member
- Posts: 3726
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Require help with tables
It's not that difficult really. Tables are easy. They can contain anything at all. Including, but not limited to, numbers, strings, functions, booleans, image data, font data, sound data, other data, other tables. And those other tables can contain any of that stuff too, including other tables.
They're written as such:
OR
Very versatile. Don't know how we'd make it any clearer.
They're written as such:
Code: Select all
table = {
thing1 = whatever,
thing2 = whatever,
...
}
Code: Select all
table = {}
table[1] = whatever
table.thing1 = whatever
table["a string"] = whatever
table[a_variable] = whatever
...
Re: Require help with tables
Thanks, that does make sense. However the for i,v ipairs or pairs is what gets me...
Re: Require help with tables
A table is just a container for data. For every entry into the table there must be a key (also called an index) and a value.
If you make a list of contents when you first create a table then those contents have numbers as keys.
pairs and ipairs are functions that return what's called "iterators". When you use iterators in a for-loop they go over every key and value pair inside of a table (this is why the functions are called "pairs"). They're useful if you want to apply something to everything inside of a table
Code: Select all
-- Create an empty table
local person = {}
-- Create new data in the table
-- "name" is the key and "Bill" is the value
person["name"] = "Bill"
--"age" is the key and "Bill" is the value
person["age"] = "23"
-- If the key is a string you can do this as a shortcut:
person.gender = "male"
for key, value in pairs(person) do
print(key, value)
end
-- Prints:
-- name Bill
-- age 23
-- gender male
Code: Select all
local t = {"one", "two", "three"}
for key, value in pairs(t) do
print(key, value)
end
-- Prints:
-- 1 one
-- 2 two
-- 3 three
Code: Select all
-- Create a table of three numbers
local numbers = {1, 5, 10}
-- Multiple all numbers by 5 and store it back
for key, value in pairs(numbers) do
numbers[key] = value * 5
end
-- Multiple all numbers by 3 and store it back
for key, value in pairs(numbers) do
numbers[key] = value * 3
end
-- Print all pairs
for key, value in pairs(numbers) do
print(key, value)
end
-- Prints:
-- 15
-- 75
-- 150
Re: Require help with tables
Thanks a lot! That was extremely helpful
Does anyone have any examples of how it is applied in a simple game?
Does anyone have any examples of how it is applied in a simple game?
- Jasoco
- Inner party member
- Posts: 3726
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Require help with tables
A thing to remember is that ipairs() will only iterate over a table whose children are numbered in order from 1 on. Any gaps and it will stop. So if you have a table with things at 1, 2, 3, 4, 5 it'll run 1 to 5 with no problem. But leave one out, 1, 2, 4, 5 and it will stop at 2 instead. pairs will instead run over all items in the table, even ones with no specified index and any index you choose. But it might be a TINY BIT slower, but usually not noticeable. Also, pairs will not start at 0 and will always start at 1. So if you're used to languages where arrays begin at 0, get used to using 1 instead.
for i, t in ipairs(table) do would be the equivalent to for i = 1, #table do where the # part returns the "length" of a tables numerically indexed children. (Also remembering how the numbers have to be in order.)
When removing a table child using table.remove, remember that if you are currently in a ipairs() loop, you will throw everything off so, as was mentioned elsewhere a few days ago, make sure to go backwards through the table. (for i = #table, 1, -1 do where -1 means to step backwards and it starts at the end of the table and goes towards the beginning.) table.remove will remove a child, and then reconfigure everything to keep them in order. For instance if you have 1, 2, 3, 4, 5 and you table.remove 2, it'll then move 3 to 2, 4 to 3 and 5 to 4. Also remember this will slow things down with larger tables since it is literally for looping through every table element to move them.
I hope that didn't confuse the crap out of you. That's all slightly more advanced. Start slow and play around a bit to get used to it. You'll see how awesome tables are.
Edit: I edited it so hopefully it's correct now. If not, it's Nixola's fault.
for i, t in ipairs(table) do would be the equivalent to for i = 1, #table do where the # part returns the "length" of a tables numerically indexed children. (Also remembering how the numbers have to be in order.)
When removing a table child using table.remove, remember that if you are currently in a ipairs() loop, you will throw everything off so, as was mentioned elsewhere a few days ago, make sure to go backwards through the table. (for i = #table, 1, -1 do where -1 means to step backwards and it starts at the end of the table and goes towards the beginning.) table.remove will remove a child, and then reconfigure everything to keep them in order. For instance if you have 1, 2, 3, 4, 5 and you table.remove 2, it'll then move 3 to 2, 4 to 3 and 5 to 4. Also remember this will slow things down with larger tables since it is literally for looping through every table element to move them.
I hope that didn't confuse the crap out of you. That's all slightly more advanced. Start slow and play around a bit to get used to it. You'll see how awesome tables are.
Code: Select all
table = {
item = {
item = {
item = {
item = {
item = {
value = "Hahaha!!!"
}
}
}
}
}
}
print(table.item.item.item.item.item.value)
Code: Select all
> Hahaha!!!
Last edited by Jasoco on Thu Jul 19, 2012 12:35 am, edited 1 time in total.
Re: Require help with tables
Lets say you have multiple enemies, they all are basically the same but you need to keep all of them moving. You could use it to do that.
Theres a silly little example of how they'd both move in a game. Assuming they're all drawn at their own respective x and y's then the 2 enemies will move in the same way, at their different places.
Code: Select all
enemy = {}
enemy[1] = {x = 0, y = 0}
enemy[2] = {x = 30, y = 30}
for key, value in pairs(enemy) do
enemy[key].x = enemy[key].x + 30
enemy[key].y = enemy[key].y + 30
end
Re: Require help with tables
Jasoco, you confused pairs and iparis, it's the opposite
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Who is online
Users browsing this forum: darkfrei, Google [Bot] and 7 guests