Modular Classes and Destructors?

Recently i read through http://blog.anscamobile.com/2011/09/tutorial-modular-classes-in-corona/ about modular classes in corona.

  1. I’ve been playing around with it for awhile, and i can’t figure out how to create a destructor for it. Anyone have any idea on this? The reason i want a destructor is because my object has images/sprites. Currently what i did was i wrote a obj:remove() function that removes the object image/sprite itself. However this doesn’t remove the table from the metatable.

  2. Is the below code one way to write/simulate class correctly? Are there any downsides compared to the tutorial?

local CLASS = {}  
  
-------------------------------------------------  
  
local function create( name, health, mana )  
 local obj = display.newGroup()  
  
 local image = display.newImage( name .. ".png" )   
 obj:insert( image )   
 image = nil  
  
 obj.name = name  
 obj.health = health  
 obj.mana = mana  
  
 -------------------------------------------------  
 -- Private Methods  
 -------------------------------------------------  
 local function removeObj()  
 display.remove( obj )  
 obj = nil  
 end  
  
 -------------------------------------------------  
  
 local function attacked( event )  
 if event.phase == "ended" then  
 obj.health = obj.health - 1  
  
 print( obj.name .. "'s HP: " .. obj.health )  
  
 if obj.health \<= 0 then  
 removeObj()  
 end  
 end  
 end  
  
 obj:addEventListener( "touch", attacked )  
  
 -------------------------------------------------  
 -- Public Methods  
 -------------------------------------------------  
 function obj:setPos( x, y )  
 self.x = x  
 self.y = y  
 end  
  
 -------------------------------------------------  
  
 function obj:printInfo()  
 print( "Name:" .. self.name )  
 print( "HP:" .. self.health )  
 print( "Mana:" .. self.mana )  
 print( "-------------------" )  
 end  
  
 -------------------------------------------------  
  
 function obj:modHealth( value )  
 self.health = self.health + value  
 end  
  
 function obj:modMana( value )  
 self.health = self.health + value  
 end  
  
 function obj:remove()  
 removeObj()  
 end  
  
 -------------------------------------------------  
  
 return obj  
end  
  
-------------------------------------------------  
  
CLASS.create = create  
  
-------------------------------------------------  
  
return CLASS  

Thanks in advance. [import]uid: 74723 topic_id: 27256 reply_id: 327256[/import]

bump, still looking for help [import]uid: 74723 topic_id: 27256 reply_id: 121487[/import]

What are you trying to achieve by declaring a new display group and adding the object into that each time? (I’m relatively new with Corona so please forgive my ignorance) Your function seems to work fine as is, including freeing the associated memory. So, let’s compare two situations here: one with the display group in the class and one without.

--main.lua (group in class)  
local class = require("CLASS")  
for i=1, 20 do  
 local testBox = class.create("box", 10, 10)  
 testBox.x = 10\*i  
 timer.performWithDelay(3000, function() testBox:remove() end, 1)  
end  
  
for k,v in pairs(class) do print(k,v) end --Prints 'create' func  

Now if we put the group into the main and remove the group from the class. Take your function above and eliminate image=nil and replace all the obj with image.

--main.lua (group in main)  
local class = require("CLASS")  
local group = display.newGroup()  
for i=1, 20 do  
 local testBox = class.create("box", 10, 10)  
 testBox.x = 10\*i  
 group:insert(testBox)  
end  
  
timer.performWithDelay(3000, function() display.remove(group) end, 1)  
  
for k,v in pairs(class) do print(k,v) end --Prints 'create' func  

In the second case, the obj:remove function is never called and the image is never nilled. I don’t think the image needs to be nilled though it seems possible that some part of your object remains in memory. There does appear to be a few k more in memory using the second approach. Thinking about it, why don’t you try something like this? (Replace obj in class to image as in step 2 and eliminate the new display group each time)

--main.lua (group in main, call remove)  
local class = require("CLASS")  
local group = display.newGroup()  
for i=1, 20 do  
 local testBox = class.create("box", 10, 10)  
 testBox.x = 10\*i  
 group:insert(testBox)  
 timer.performWithDelay(3000, function() testBox:remove() end, 1)  
end  
  
timer.performWithDelay(4000, function() display.remove(group) end, 1)  
  
for k,v in pairs(class) do print(k,v) end --Prints 'create' func  

This last sample shows the same memory usage as the first and gives you access to the display group from your parent lua file. Not sure if that’s what you want, but it seems straight forward enough. After running these tests, it looks like your code works fine and if the game works, use it. If someone else points out a better way than what you’ve done, decide if it’s worth updating. Best of luck! [import]uid: 168249 topic_id: 27256 reply_id: 121491[/import]

Hello, thanks for the reply. We are all learning so its all good [: The reason i am having a display group is because i want to give my character equipments which are separate images. Attaching equipments onto my character and then returning the whole display group as an object. (Not shown on the above sample code) . So i can’t remove the display group in the “class”. Unless i am doing it wrongly, i will need a new way to handle attachments for characters.

I kinda agree with you on the image thing. I’ve been reading lua guides and other forums for awhile and most people just say that let the garbage collector do its job. However i still feel uneasy until i know the correct solution. [import]uid: 74723 topic_id: 27256 reply_id: 121493[/import]

bump, still looking for help [import]uid: 74723 topic_id: 27256 reply_id: 121487[/import]

What are you trying to achieve by declaring a new display group and adding the object into that each time? (I’m relatively new with Corona so please forgive my ignorance) Your function seems to work fine as is, including freeing the associated memory. So, let’s compare two situations here: one with the display group in the class and one without.

--main.lua (group in class)  
local class = require("CLASS")  
for i=1, 20 do  
 local testBox = class.create("box", 10, 10)  
 testBox.x = 10\*i  
 timer.performWithDelay(3000, function() testBox:remove() end, 1)  
end  
  
for k,v in pairs(class) do print(k,v) end --Prints 'create' func  

Now if we put the group into the main and remove the group from the class. Take your function above and eliminate image=nil and replace all the obj with image.

--main.lua (group in main)  
local class = require("CLASS")  
local group = display.newGroup()  
for i=1, 20 do  
 local testBox = class.create("box", 10, 10)  
 testBox.x = 10\*i  
 group:insert(testBox)  
end  
  
timer.performWithDelay(3000, function() display.remove(group) end, 1)  
  
for k,v in pairs(class) do print(k,v) end --Prints 'create' func  

In the second case, the obj:remove function is never called and the image is never nilled. I don’t think the image needs to be nilled though it seems possible that some part of your object remains in memory. There does appear to be a few k more in memory using the second approach. Thinking about it, why don’t you try something like this? (Replace obj in class to image as in step 2 and eliminate the new display group each time)

--main.lua (group in main, call remove)  
local class = require("CLASS")  
local group = display.newGroup()  
for i=1, 20 do  
 local testBox = class.create("box", 10, 10)  
 testBox.x = 10\*i  
 group:insert(testBox)  
 timer.performWithDelay(3000, function() testBox:remove() end, 1)  
end  
  
timer.performWithDelay(4000, function() display.remove(group) end, 1)  
  
for k,v in pairs(class) do print(k,v) end --Prints 'create' func  

This last sample shows the same memory usage as the first and gives you access to the display group from your parent lua file. Not sure if that’s what you want, but it seems straight forward enough. After running these tests, it looks like your code works fine and if the game works, use it. If someone else points out a better way than what you’ve done, decide if it’s worth updating. Best of luck! [import]uid: 168249 topic_id: 27256 reply_id: 121491[/import]

Hello, thanks for the reply. We are all learning so its all good [: The reason i am having a display group is because i want to give my character equipments which are separate images. Attaching equipments onto my character and then returning the whole display group as an object. (Not shown on the above sample code) . So i can’t remove the display group in the “class”. Unless i am doing it wrongly, i will need a new way to handle attachments for characters.

I kinda agree with you on the image thing. I’ve been reading lua guides and other forums for awhile and most people just say that let the garbage collector do its job. However i still feel uneasy until i know the correct solution. [import]uid: 74723 topic_id: 27256 reply_id: 121493[/import]