Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

What is the correct way to removeSelf for items in a table?
Started by ammar71 May 01 2019 08:58 AM

2 replies to this topic
removeself table

Best Answer Alan PlantPot , 01 May 2019 - 10:18 AM

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

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

ammar71

[GLOBAL: userInfoPane.html]
ammar71
  • Enthusiast

  • 32 posts
  • Corona SDK

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?



[TOPIC: post.html]
#2

Alan PlantPot

[GLOBAL: userInfoPane.html]
Alan PlantPot
  • Contributor

  • 915 posts
  • Corona SDK

  Best Answer

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


[TOPIC: post.html]
#3

ammar71

[GLOBAL: userInfoPane.html]
ammar71
  • Enthusiast

  • 32 posts
  • Corona SDK

@Alan PlantPot

 

Thanks. That did solve the problem  :)




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: removeself, table