how to trigger callback for a "removeSelf()" call?

Wondering if there is a way to trigger a callback on your custom object(table) when a “removeSelf()” is called upon it? Is there a way?

Background:
* Assume you have instantiated an object ( in an OO style )
* So let’s say in Main you go: inventory:removeSelf(), then a way to add a method to the class such that it would automatically pickup this callback and then you could put in here custom clean up code (e.g. removing listeners, removing ParticleCandy stuff etc)
* Idea would then be you could build you custom classes such that:

  • each would have it’s own “cleanup” method
  • in the parent you just call removeSelf(), without a need to call the class’s “cleanup” method first
  • i.e. implying here from main you’d normally have to clean up via:
    inventory:cleanup() – which you write
    inventory:removeSelf()
    Example of OO Style I was referring to

[code]
MAIN

Inventory = require(“inventory”)
inventory = Inventory:new()
inventory:printScore() – Works fine (using “self”)
CLASS

– OO Class called Inventory
Inventory = {}
function Inventory:new()
local inventory = display.newGroup()

– Class Contents: Start ---------------------
inventory.score = 6

local inventoryImage = display.newImage( “inventory_button.png”, 0, 0)
inventoryImage.y = display.contentHeight - inventoryImage.height/2
inventory:insert(inventoryImage)

function inventory:printScore()
print("In printScore: Score = " … self.score)
end

– NOTE: With listeners you can use “self” if “method you define via : and the event name have to match”
function inventory:touch(event)
if (event.phase == “began”) then
print("In test_Listener: Score = " … self.score)
end
end
inventory:addEventListener(“touch”, inventory)

– Class Contents: End ---------------------

return inventory
end
return Inventory
[/code] [import]uid: 140210 topic_id: 34532 reply_id: 334532[/import]

I would decorate the image object and implement my own removeSelf that calls the images removeSelf. If you don’t want to mess with metaTables you would create a different method and bind it such as removeMySelf.

local inventory:removeMySelf(event)  
 -- do my special stuff here  
 self:removeSelf()  
end  

Then removeMySelf is whats called by your event. You may have to do a custom event dispatcher if this is being called by something else that only knows about removeSelf();

If you decorated the object and used metatables like the example I have here: https://github.com/chris-allnutt/unit-tested-corona/blob/master/lib/ChangeTextColorButton.lua the custom event listener isn’t necessary. [import]uid: 130806 topic_id: 34532 reply_id: 137346[/import]

Tks - hadn’t thought of use of “self:removeSelf()” for effectively the code removing the object that it is part of. So no doubt this must work ok then from what you’re saying.
[import]uid: 140210 topic_id: 34532 reply_id: 137348[/import]

I would decorate the image object and implement my own removeSelf that calls the images removeSelf. If you don’t want to mess with metaTables you would create a different method and bind it such as removeMySelf.

local inventory:removeMySelf(event)  
 -- do my special stuff here  
 self:removeSelf()  
end  

Then removeMySelf is whats called by your event. You may have to do a custom event dispatcher if this is being called by something else that only knows about removeSelf();

If you decorated the object and used metatables like the example I have here: https://github.com/chris-allnutt/unit-tested-corona/blob/master/lib/ChangeTextColorButton.lua the custom event listener isn’t necessary. [import]uid: 130806 topic_id: 34532 reply_id: 137346[/import]

Tks - hadn’t thought of use of “self:removeSelf()” for effectively the code removing the object that it is part of. So no doubt this must work ok then from what you’re saying.
[import]uid: 140210 topic_id: 34532 reply_id: 137348[/import]