How to store and delete objects into and from a table?

Hi,

I spawn and remove objects in my game and it runs fine as I get their collision handle and call removeSelf and nil upon them and it’s fine but at certain situations I need to remove all those objects that I spawned so I have to store their references in some data structure, and in Lua I believe it’s a table, so I can call removeSelf and nil them.

Problem is, I can’t get my head around doing it. I used several methods and used several things as keys, even added a custom string as key and then used ipairs to retrieve them but it just won’t work. Because all iterators apparently require an index at 1 but I may have removed object at that 1 index previously.

So to sum it up, how can I store objects into a table, have a proper key to find them and easily be able to remove (and by remove I mean three things 1) calling removeSelf upon it 2) assigning nil to it and 3) removing it from that table object holding it’s reference) any of them or all of them at once?

Thanks. [import]uid: 206803 topic_id: 36808 reply_id: 336808[/import]

I have no problem making a table as I want to, problem is I can’t iterate over all it’s objects when it becomes something like this:

myTable = {3 = table1, 5 = table2, 11 = table13}

Because apparently iterators need a 1 index.

Thing is when my table becomes something like this:
t = { 7 = table6} then when I call ipairs on it, it just skips and won’t go inside it.

even if I make that index a string, it will be the same:

t = { “7” = table6}, the ipairs iterator skips it. [import]uid: 206803 topic_id: 36808 reply_id: 145091[/import]

Hi there,

Yes, when you have numerical keys for a table, using ipairs will start at a key of 1 and keep going until it gets to the first element that is nil. So, if your table index doesn’t start at 1, ipairs won’t loop through anything, it will just stop immediately. And if your table index has “gaps” in it (like keys of 1,2,3,6,7), it will stop at the first gap (because that element is nil).

Instead of ipairs, try just using pairs. Store the elements of your table using a key that is not a number, like a string ID that you make up. Then, loop through the table using the pairs (not ipairs) iterator. This will get every element of the table that has a non-numerical key.

Hope this helps.

  • Andrew [import]uid: 109711 topic_id: 36808 reply_id: 145097[/import]

Thanks, I just go that before I left work so now that I came to update my post that saw your lovely post with brilliant explanation!

Thanks again.
[import]uid: 206803 topic_id: 36808 reply_id: 145100[/import]

I have no problem making a table as I want to, problem is I can’t iterate over all it’s objects when it becomes something like this:

myTable = {3 = table1, 5 = table2, 11 = table13}

Because apparently iterators need a 1 index.

Thing is when my table becomes something like this:
t = { 7 = table6} then when I call ipairs on it, it just skips and won’t go inside it.

even if I make that index a string, it will be the same:

t = { “7” = table6}, the ipairs iterator skips it. [import]uid: 206803 topic_id: 36808 reply_id: 145091[/import]

Hi there,

Yes, when you have numerical keys for a table, using ipairs will start at a key of 1 and keep going until it gets to the first element that is nil. So, if your table index doesn’t start at 1, ipairs won’t loop through anything, it will just stop immediately. And if your table index has “gaps” in it (like keys of 1,2,3,6,7), it will stop at the first gap (because that element is nil).

Instead of ipairs, try just using pairs. Store the elements of your table using a key that is not a number, like a string ID that you make up. Then, loop through the table using the pairs (not ipairs) iterator. This will get every element of the table that has a non-numerical key.

Hope this helps.

  • Andrew [import]uid: 109711 topic_id: 36808 reply_id: 145097[/import]

Thanks, I just go that before I left work so now that I came to update my post that saw your lovely post with brilliant explanation!

Thanks again.
[import]uid: 206803 topic_id: 36808 reply_id: 145100[/import]

I have no problem making a table as I want to, problem is I can’t iterate over all it’s objects when it becomes something like this:

myTable = {3 = table1, 5 = table2, 11 = table13}

Because apparently iterators need a 1 index.

Thing is when my table becomes something like this:
t = { 7 = table6} then when I call ipairs on it, it just skips and won’t go inside it.

even if I make that index a string, it will be the same:

t = { “7” = table6}, the ipairs iterator skips it. [import]uid: 206803 topic_id: 36808 reply_id: 145091[/import]

Hi there,

Yes, when you have numerical keys for a table, using ipairs will start at a key of 1 and keep going until it gets to the first element that is nil. So, if your table index doesn’t start at 1, ipairs won’t loop through anything, it will just stop immediately. And if your table index has “gaps” in it (like keys of 1,2,3,6,7), it will stop at the first gap (because that element is nil).

Instead of ipairs, try just using pairs. Store the elements of your table using a key that is not a number, like a string ID that you make up. Then, loop through the table using the pairs (not ipairs) iterator. This will get every element of the table that has a non-numerical key.

Hope this helps.

  • Andrew [import]uid: 109711 topic_id: 36808 reply_id: 145097[/import]

Thanks, I just go that before I left work so now that I came to update my post that saw your lovely post with brilliant explanation!

Thanks again.
[import]uid: 206803 topic_id: 36808 reply_id: 145100[/import]

I have no problem making a table as I want to, problem is I can’t iterate over all it’s objects when it becomes something like this:

myTable = {3 = table1, 5 = table2, 11 = table13}

Because apparently iterators need a 1 index.

Thing is when my table becomes something like this:
t = { 7 = table6} then when I call ipairs on it, it just skips and won’t go inside it.

even if I make that index a string, it will be the same:

t = { “7” = table6}, the ipairs iterator skips it. [import]uid: 206803 topic_id: 36808 reply_id: 145091[/import]

Hi there,

Yes, when you have numerical keys for a table, using ipairs will start at a key of 1 and keep going until it gets to the first element that is nil. So, if your table index doesn’t start at 1, ipairs won’t loop through anything, it will just stop immediately. And if your table index has “gaps” in it (like keys of 1,2,3,6,7), it will stop at the first gap (because that element is nil).

Instead of ipairs, try just using pairs. Store the elements of your table using a key that is not a number, like a string ID that you make up. Then, loop through the table using the pairs (not ipairs) iterator. This will get every element of the table that has a non-numerical key.

Hope this helps.

  • Andrew [import]uid: 109711 topic_id: 36808 reply_id: 145097[/import]

Thanks, I just go that before I left work so now that I came to update my post that saw your lovely post with brilliant explanation!

Thanks again.
[import]uid: 206803 topic_id: 36808 reply_id: 145100[/import]