Require help with tables

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Jaykin
Prole
Posts: 4
Joined: Wed Jul 18, 2012 3:27 pm

Require help with tables

Post by Jaykin »

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!
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Require help with tables

Post by coffee »

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!
Tables in LOVE are the same than Lua (the language code behind LOVE) tables:
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. :)
Jaykin
Prole
Posts: 4
Joined: Wed Jul 18, 2012 3:27 pm

Re: Require help with tables

Post by Jaykin »

I do vaguely understand the concept generally, it's just applying it to Love I find difficult.
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Require help with tables

Post by Jasoco »

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:

Code: Select all

table = {
  thing1 = whatever,
  thing2 = whatever,
  ...
}
OR

Code: Select all

table = {}
table[1] = whatever
table.thing1 = whatever
table["a string"] = whatever
table[a_variable] = whatever
...
Very versatile. Don't know how we'd make it any clearer.
Jaykin
Prole
Posts: 4
Joined: Wed Jul 18, 2012 3:27 pm

Re: Require help with tables

Post by Jaykin »

Thanks, that does make sense. However the for i,v ipairs or pairs is what gets me...
User avatar
Kadoba
Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

Re: Require help with tables

Post by Kadoba »

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.

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
If you make a list of contents when you first create a table then those contents have numbers as keys.

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
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 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
Jaykin
Prole
Posts: 4
Joined: Wed Jul 18, 2012 3:27 pm

Re: Require help with tables

Post by Jaykin »

Thanks a lot! That was extremely helpful :D

Does anyone have any examples of how it is applied in a simple game?
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Require help with tables

Post by Jasoco »

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.

Code: Select all

table = {
  item = {
    item = {
      item = {
        item = {
          item = {
            value = "Hahaha!!!"
          }
        }
      }
    }
  }
}
print(table.item.item.item.item.item.value)

Code: Select all

> Hahaha!!!
Edit: I edited it so hopefully it's correct now. If not, it's Nixola's fault.
Last edited by Jasoco on Thu Jul 19, 2012 12:35 am, edited 1 time in total.
User avatar
Qcode
Party member
Posts: 170
Joined: Tue Jan 10, 2012 1:35 am

Re: Require help with tables

Post by Qcode »

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.

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
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.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Require help with tables

Post by Nixola »

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
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests