How to remove a table via a reference?

if you can’t fight it, join it.

local tableObj={} tableObj[#tableObj+1]=display.newCircle(120, 60, 40 ) tableObj[#tableObj+1]=display.newCircle(120, 60, 30 ) print (#tableObj) print (tableObj[1]) print (tableObj[2]) for i=#tableObj, 1, -1 do display.remove(tableObj[i]) tableObj[i]=nil end print (#tableObj) print (tableObj[1]) print (tableObj[2])

NOTE: removing a table/group use always inverted for cicle. you can thank me later.

if YOU create a reference, then YOU must dispose of it, simple as that.

local a = display.newRect(... a.b = display.newRect(... a.b.c = display.newRect(... a.b.c.d = display.newRect(... display.remove(a) -- incomplete!!! -- b,c,d still exist as display objects -- AND a still exists as a table -- AND a still has a ref to b (which has a ref to c (which has a ref to d)) -- so, what would be necessary to properly clean up this mess? -- (assume that the incomplete "display.remove(a)" above has NOT been executed) display.remove(a.b.c.d) display.remove(a.b.c) display.remove(a.b) display.remove(a) a = nil -- releasing a will release b (which will release c (which will release d)) -- ok, now we're "clean" - because all refs that WE created WE deleted

there are various ways to “hide” or “obscure” or “automate” the cleanup (fe finalize listeners) but the fact remains that you must do your own cleanup somewhere.

@carloscosta, that wasn’t really the point of this thread, but thanks still :smiley:

Also, thank you everyone else for your input. Seems that the answer to my question of can I remove the originals via references alone and how is simply: “No.”

I’ll just go with my plan B and stuff all of the shadows into one table for an easy fix.

I’m not sure what your end goal is, but if you’re creating an object A with dependent objects B, C, … that you want removed and cleaned up when A is removed, just do this:

-- example of one 'dependent' object; a shadow local a = display.newCircle( 10, 10, 10 ) a.shadow = display.newCircle( 12, 12, 10 ) a.shadow:setFillColor(0,0,0.0.5) a:toFront() function a:finalize() display.remove( self.shadow ) end a:addEventListener( "finalize" )

Now, later if you do this, the shadow is auto-removed:

display.remove(a)

I had actually completely forgotten about that finalize listener. It’s pretty cool and I think I’ll implement it in my final solution.

My original idea was to have a parent with unknown number of children that, when the parent was removed, would also be removed. In addition to just removing the display objects, I also wanted to set them to nil.

This would have been a part of the shadow engine plugin that I’m finalising atm. Until a few days ago, the plugin was object-centric, i.e. each shadow was owned by the object that cast it. With this method, the parent (a light source) could have any number of children (shadows that were cast because of it) from variables of all names. For instance, there could be a light source “local candle” and objects “local chair, table, character”. I wanted to know if there was a way for me to destroy these objects and set them to nil by removing only the light source. Well, the simple answer is: No. Sure, there are ways of doing this but without knowing the objects’ names in advance there doesn’t seem to be a way as I can’t set the original references to the tables to nil from within a general function.

Now, I swapped things around and made the plugin light-centric, meaning that all of the shadows are now by the specific lights, destroying all shadows associated with a specific light is as easy as looping through a table. I’ll probably add that finalize listener to my final approach here. As for destroying all shadows cast by a specific object, I will simply give each object a name that the plugin can track by using:
 

local object = display.newCircle( 160, 240, 32 ) object.name = tostring(object):sub(8) print(object.name) -\> output: "0C341B30"

And then I just loop through every light, find the object with that name, and if found, remove that shadow from the light.
 

In the case of an arbitrary number of children, I typically just make the top object a group and put all the children in the group at <0,0> or an offset as needed.

Then move the group where you need it.

Thus, deleting the group deletes the children.

This is how I made most of the complex (multi-part) objects in SSK:

i’m a fan of using a parent display group for all children as well. makes things so much easier.