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.