Remove display object from table without loop

Hi,

I am spawning display objects into a table that are destroyed on the screen randomly. The objects are constantly updated with new body types, which is done via a loop back to all the objects in the table. However, when objects are removed they are not removed from the table, which is causing problems when updating the individual bodies around the table.

In short, is there a way to remove an object from a table by its name and not by an index number, since I have no way of determining which object corresponds to which index number.

Thanks!

If you have assigned an object to a table by name reference you can simply remove it by name reference.

Something is off about your logic. Why are you doing it this way? Why can’t you identify which object is which by index?

Hi,

You can set a reference by the object itself:

[lua]local tbl = {}

local rect = display.newRect(5, 5, 100, 100)

tbl[rect] = rect[/lua]

Or, by adding an “id” property, though this may be a little more risky if there is a change in the display api:

[lua]local tbl = {}

local rect = display.newRect(5, 5, 100, 100)
rect.id = “some-rect-id”

tbl[rect.id] = rect[/lua]

Though I do agree with @horacebury in that there is probably a better way to approach the problem.

-dev

Use a checker so the loop will now if table data is nil:

local yourTable = {}

–lets say this is your loop
for i=1,#yourTable,1 do
–assuming you put your display object directly on index of your table
if yourTable[i] ~= nil then
–Put code to update body here
–The code will only run if the object
–in table is not nill
end
end

(ps t tested since im on mobile)

and yes you can use string as table name, for example:

yourTable[“object1”] = display.newRect(0,0,100,50)

yourTable[“object1”]:removeSelf()
yourTable[“object1”] = nil

If you have assigned an object to a table by name reference you can simply remove it by name reference.

Something is off about your logic. Why are you doing it this way? Why can’t you identify which object is which by index?

Hi,

You can set a reference by the object itself:

[lua]local tbl = {}

local rect = display.newRect(5, 5, 100, 100)

tbl[rect] = rect[/lua]

Or, by adding an “id” property, though this may be a little more risky if there is a change in the display api:

[lua]local tbl = {}

local rect = display.newRect(5, 5, 100, 100)
rect.id = “some-rect-id”

tbl[rect.id] = rect[/lua]

Though I do agree with @horacebury in that there is probably a better way to approach the problem.

-dev

Use a checker so the loop will now if table data is nil:

local yourTable = {}

–lets say this is your loop
for i=1,#yourTable,1 do
–assuming you put your display object directly on index of your table
if yourTable[i] ~= nil then
–Put code to update body here
–The code will only run if the object
–in table is not nill
end
end

(ps t tested since im on mobile)

and yes you can use string as table name, for example:

yourTable[“object1”] = display.newRect(0,0,100,50)

yourTable[“object1”]:removeSelf()
yourTable[“object1”] = nil