Going through complex 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.
User avatar
Ertain
Citizen
Posts: 55
Joined: Fri Nov 19, 2010 9:38 pm
Location: Texas, U.S.A.

Going through complex tables

Post by Ertain »

Hello again, everyone.

I have this little function which checks for collision. What I have to do, though, is first navigate through the large table that houses the numbers for my enemies. The table looks something like this (note: this isn't my real code; it's just for this example):

Code: Select all

group.enemy = {}
group.enemy[1] = {x = 20, y = 20, radius = 16, hasBeenHit = false}
group.enemy[2] = {
buzzard1 = { x = 20, y = 20, radius = 21, hasBeenHit = false}
buzzard2 = {x = 40, y = 40, radius = 21, hasBeenHit = false}
buzzard3 = {x = 60, y = 60, radius = 21, hasBeenHit = false}
}
group.enemy[3] = {
guy1 = {x = 20, y = 30, radius = 22, hasBeenHit = false}
guy2 = { x = 40, y = 70, radius = 22, hasBeenHit = false}
}
What I'm trying to do is go through each of the internal arrays to get to the right variable (in this case, the "x" and "y" variables). I can't figure out exactly how to do this because I can't exactly formulate the correct "for" statements to get it right. I thought of first using the ipairs() function to go through each element, but I still have to check for "x" and possibly go deeper (such as with the "buzzard" tables).
Booted, suited, and ready to get executed.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Going through complex tables

Post by Robin »

What makes it harder for yourself that some entries in the enemy table are 'entities', while others are collections of 'entities'.

You could try something like this:

Code: Select all

for i,e in ipairs(enemy) do
    if e.x then
        processenemy(e)
    else
        for k,v in pairs(e) do -- iterate over subtable
            processenemy(v)
        end
    end
end
Help us help you: attach a .love.
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: Going through complex tables

Post by thelinx »

Code: Select all

for id, enemygroup in ipairs(group.enemy) do -- loop in the enemy.group table, giving each item to the scope as "enemygroup"
  for enemy_id, enemy in pairs(enemygroup) do -- loop in the enemy.group[n] table, giving each item to the scope as "enemy"
    print(enemy.x, enemy.y) -- refer to the variables as enemy.whatever
  end
end
I hope that helps!
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: Going through complex tables

Post by zac352 »

I think he means he has nested tables. (I'm going to post an example of how to do that just in case:)

Code: Select all

function recursive(t)
    for k,v in pairs(t) do
        doSomething(t,k,v)
    end
end
recursive(someNestedTable)
Hello, I am not dead.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Going through complex tables

Post by Robin »

zac352 wrote:I think he means he has nested tables. (I'm going to post an example of how to do that just in case:)

Code: Select all

function recursive(t)
    for k,v in pairs(t) do
        doSomething(t,k,v)
    end
end
recursive(someNestedTable)
That's not recursive at all. :roll:
Help us help you: attach a .love.
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: Going through complex tables

Post by zac352 »

Robin wrote:
zac352 wrote:I think he means he has nested tables. (I'm going to post an example of how to do that just in case:)

Code: Select all

function recursive(t)
    for k,v in pairs(t) do
        doSomething(t,k,v)
    end
end
recursive(someNestedTable)
That's not recursive at all. :roll:
Wait... Eh. I changed the name of my recursive function in the middle of writing it. :emo:
Hello, I am not dead.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Going through complex tables

Post by Robin »

You mean

Code: Select all

function recursive(t)
   for k,v in pairs(t) do
        recursive(t,k,v)
    end
end
recursive(someNestedTable)
?
Then it would just be a fork bomb. (Unless the table passed is empty.)
Help us help you: attach a .love.
User avatar
Ertain
Citizen
Posts: 55
Joined: Fri Nov 19, 2010 9:38 pm
Location: Texas, U.S.A.

Re: Going through complex tables

Post by Ertain »

Thanks for the help, guys. Now all I need to do is figure out variable type (determining if a variable is a number, table, etc.).
Booted, suited, and ready to get executed.
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Going through complex tables

Post by nevon »

Ertain wrote:Now all I need to do is figure out variable type (determining if a variable is a number, table, etc.).
You can use the type() function.
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: Going through complex tables

Post by zac352 »

Robin wrote:You mean

Code: Select all

function recursive(t)
   for k,v in pairs(t) do
        recursive(t,k,v)
    end
end
recursive(someNestedTable)
?
Then it would just be a fork bomb. (Unless the table passed is empty.)
I give up. I wrote that at 7:30 AM, not my brightest time of day.
nevon wrote:
Ertain wrote:Now all I need to do is figure out variable type (determining if a variable is a number, table, etc.).
You can use the type() function.
It'd be nice if you actually gave him some information on the type function...

Code: Select all

local a,b,c,d,e,f,g=42,"lol",{},io.open("Blargh.txt","w"),coroutine.create(function() end),function() end,g
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
print(type(f))
print(type(g))
->number
->string
->table
->file
->thread
->function
->nil

Note: That last one is a string containing "nil", and not nil itself. ;)
Hello, I am not dead.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 203 guests