Trouble 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
User avatar
Quentin
Prole
Posts: 8
Joined: Thu Jul 12, 2012 3:52 am

Trouble with tables

Post 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 480 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 480 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.
Last edited by Quentin on Fri Jul 13, 2012 8:49 am, edited 1 time in total.
User avatar
Qcode
Party member
Posts: 170
Joined: Tue Jan 10, 2012 1:35 am

Re: Trouble with tables

Post by Qcode »

The code would help a lot. What is the i? for i = 1, or for i, v in pairs(stuff)?
User avatar
Quentin
Prole
Posts: 8
Joined: Thu Jul 12, 2012 3:52 am

Re: Trouble with tables

Post 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
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Trouble with tables

Post 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.)
User avatar
Quentin
Prole
Posts: 8
Joined: Thu Jul 12, 2012 3:52 am

Re: Trouble with tables

Post 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.
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: Trouble with tables

Post 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.
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: Trouble with tables

Post by dreadkillz »

It's generally unwise to delete an index while using ipairs as it can cause your for loop to randomly stop.
User avatar
Quentin
Prole
Posts: 8
Joined: Thu Jul 12, 2012 3:52 am

Re: Trouble with tables

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

Who is online

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