Page 1 of 1

Trouble with tables

Posted: Thu Jul 12, 2012 11:25 pm
by Quentin
*Sigh* sorry for all the help threads.

Basically, I have a game that looks like this:
J1hwo.png
J1hwo.png (5.4 KiB) Viewed 338 times
You need to jump up the platforms and collect the point thingies.
I've been working on said point thingies, but I've run into a problem when picking them up.
I have the actual physics body destroyed well, but when I try to remove it from the "pickups" table, all of them graphically disppear, but I can still jump on them without their physics bodies disappearing:
k0e1h.png
k0e1h.png (5.66 KiB) Viewed 338 times
Everywhere I searched, it said that making the object nil is the proper way of removing an object from a table, and I'm using

Code: Select all

objects.pickups[i] = nil
to remove it.

http://pastebin.com/L3r78GE3
My code, no clue about using object-oriented shit(like in Java) for now, just making this thing to try it out.

Re: Trouble with tables

Posted: Thu Jul 12, 2012 11:33 pm
by Qcode
The code would help a lot. What is the i? for i = 1, or for i, v in pairs(stuff)?

Re: Trouble with tables

Posted: Fri Jul 13, 2012 12:02 am
by Quentin
Qcode wrote:The code would help a lot. What is the i? for i = 1, or for i, v in pairs(stuff)?
Yeah:

Code: Select all

for i,v in ipairs( objects.pickups ) do
    if v.fixture == b then
        objects.pickups[i].body:destroy()
        objects.pickups[i] = nil
    end
end

Re: Trouble with tables

Posted: Fri Jul 13, 2012 8:05 am
by bartbes
The problem is most likely that by doing that you remove the continuity of the table (1, 2, nothing, 4, 5), and don't account for that in your other loops.

That said, there are forum rules that would allow us to pin down exactly where you're going wrong, you chose to ignore them, we can only guess what's wrong. (And we'll be annoyed.)

Re: Trouble with tables

Posted: Fri Jul 13, 2012 8:51 am
by Quentin
bartbes wrote:The problem is most likely that by doing that you remove the continuity of the table (1, 2, nothing, 4, 5), and don't account for that in your other loops.

That said, there are forum rules that would allow us to pin down exactly where you're going wrong, you chose to ignore them, we can only guess what's wrong. (And we'll be annoyed.)
Updated the OP with my code.
Although I'm still "ignoring the rules" as they say to supply a .love of the game, but there's no point.
I didn't think anyone would need to see my code/.love since I've explained what's happening, when I "pick up" the pickup, everything goes invisible, derpy herpy derp.

Re: Trouble with tables

Posted: Fri Jul 13, 2012 9:48 am
by Santos
I think the recommendation to post a .love file is so someone who wants to help can observe the behavior first hand, easily edit the files, find what needs changing, and test the changes. A description of a problem doesn't necessarily mean the cause of the problem is obvious! :)

Replacing:

Code: Select all

for i,v in ipairs( objects.pickups ) do
    if v.fixture == b then
        objects.pickups[i].body:destroy()
        objects.pickups[i] = nil
    end
end
with:

Code: Select all

for i = #objects.pickups, 1, -1 do
    if objects.pickups[i].fixture == b then
        objects.pickups[i].body:destroy()
        table.remove(objects.pickups, i)
    end
end
should work! ^^ Notice the "reverse" for loop, used for removing table elements while looping over it.

Re: Trouble with tables

Posted: Fri Jul 13, 2012 10:19 am
by dreadkillz
It's generally unwise to delete an index while using ipairs as it can cause your for loop to randomly stop.

Re: Trouble with tables

Posted: Fri Jul 13, 2012 8:54 pm
by Quentin
Santos wrote:I think the recommendation to post a .love file is so someone who wants to help can observe the behavior first hand, easily edit the files, find what needs changing, and test the changes. A description of a problem doesn't necessarily mean the cause of the problem is obvious! :)

Replacing:

Code: Select all

for i,v in ipairs( objects.pickups ) do
    if v.fixture == b then
        objects.pickups[i].body:destroy()
        objects.pickups[i] = nil
    end
end
with:

Code: Select all

for i = #objects.pickups, 1, -1 do
    if objects.pickups[i].fixture == b then
        objects.pickups[i].body:destroy()
        table.remove(objects.pickups, i)
    end
end
should work! ^^ Notice the "reverse" for loop, used for removing table elements while looping over it.
Ah, thanks a lot!