What is the correct way to removeSelf for items in a table?

Hi all,

In a scene, I defined a table (otside of the Scene event functions) as follows:

local t = {}

In the scene:show event, I add newSprites to the table in a loop.

t[i] = = display.newSprite( sceneGroup, t\_sheet, t\_sequence )

In the scene:hide I try to destroy them as follows:

for k, v in pairs(t) do v:removeSelf() v = nil end

This works most  of the time, but occasionally I get:

attempt to call method 'removeSelf' (a nil value)

I am thinking that maybe removing the objects inside the loop leads to the pairs function getting confused. Am I right? 

Is there a better way to do this?

I tend to use:

display.remove(v)

instead of:

v:removeSelf()

display.remove() doesn’t fail if the object being passed in does not exist, but removeSelf will fail if you try and call it on an object which doesn’t have that function. That might sound confusing because sprites do have a removeSelf function, right? 

What’s happening is that at some point your objects are being removed but are not being set to nil (possibly as part of the in pairs() process, maybe outside of this loop somewhere).  

So technically “v” in the loop still exists, but it’s no longer a display object because that was removed and so “v” doesn’t have a “removeSelf” function any more.

If your table is always indexed with numbers (i.e. you always use things like t[i] where i is a number or an explicit number like t[23], and not a string key such as t[“myThing”]), you could just use a normal for loop which counts down to avoid complications where items at the start of the table are removed causing other items to shift down:

for i = #t, 1, -1 do display.remove(t[i]) t[i] = nil end t = {} --just to be extra sure the whole table is cleared

@Alan PlantPot

Thanks. That did solve the problem  :slight_smile:

I tend to use:

display.remove(v)

instead of:

v:removeSelf()

display.remove() doesn’t fail if the object being passed in does not exist, but removeSelf will fail if you try and call it on an object which doesn’t have that function. That might sound confusing because sprites do have a removeSelf function, right? 

What’s happening is that at some point your objects are being removed but are not being set to nil (possibly as part of the in pairs() process, maybe outside of this loop somewhere).  

So technically “v” in the loop still exists, but it’s no longer a display object because that was removed and so “v” doesn’t have a “removeSelf” function any more.

If your table is always indexed with numbers (i.e. you always use things like t[i] where i is a number or an explicit number like t[23], and not a string key such as t[“myThing”]), you could just use a normal for loop which counts down to avoid complications where items at the start of the table are removed causing other items to shift down:

for i = #t, 1, -1 do display.remove(t[i]) t[i] = nil end t = {} --just to be extra sure the whole table is cleared

@Alan PlantPot

Thanks. That did solve the problem  :slight_smile: