Remove Object References In Groups

Hi everyone,

 

I have all objects inserted into a group and i can’t remove all references of my objects :confused:

 

i tried it :

 

for j=group.numChildren, 1, -1 do display.remove(group[group.numChildren]) group[group.numChildren] = nil end display.remove(group) group = nil

 

 

This just remove the objects but doesn’t remove its references …

 

anyone can help me ?

thanks.

What do you mean by it’s not removing its references?

I’d like remove the objects from memory.
The memory isn’t being cleaned up. For example : If i type :
print(someObjectOfMyGroup) It should print ‘nil’ but this reference isn’t released.

 

thanks ^^,

Hi Jesseh,

Are these objects stored in some kind of containing table, or do they have some other element(s) tied to them like transitions or event listeners?

Brent

i’m sorry … let me show the code :

 

local \_M = {} function \_M.new() local group = display.newGroup() local player = display.newSprite... group:insert(player) local function TouchFunction ... local function CollisionFunction... local function FreeMemory() for j=group.numChildren, 1, -1 do display.remove(group[group.numChildren]) group[group.numChildren] = nil end display.remove(group) group = nil --this shouldn't print 'nil' ? print(player) end player.touch = TouchFunction player:addEventListener("touch", player) player.collision = CollisionFunction player:addEventListener("collision", player) return group end return \_M

ops … the color of the code has changed o.O

i thought in put all my objects in a table and remove it like so :

 

local myTable = {} myTable.player = display.newSprite ... --After remove this way : for j=1, #myTable do display.remove(myTable[j]) myTable[j] = nil end myTable = nil 

 

But i don’t know if it would let the game more slow … what do you think ?

thanks, 

If you’re expecting “player” to be nil, it won’t. When you insert player into a group, you are inserting the object referenced by player into the group and not the variable itself. When you removed the object and set the value to nil in your for loop, it removed the object (which converted it to a table) and cleared the value in the group, but didn’t touch the value stored in “player”. What you need to do is set “player = nil” after you insert it into the table.

In the first code sample, I notice you’ve put the “player” into the group, but you’ve also placed listeners on the player object (“touch” and “collision”). Where do you remove those listeners? Somewhere else in the code that you didn’t include?

In the second code sample, that’s a reasonable approach, except that in your example, you’re adding the player as a dictionary item in the table (named “player”), and then when you clear you’re attempting to loop through by index numbers. It will not clear the items that way.

So, you’d have to change it one of two ways:

[lua]

myTable[#myTable+1] = display.newSprite …

for j = #myTable,1,-1 do --REVERSE ORDER!!!

display.remove(myTable[j])

myTable[j] = nil

end

[/lua]
—OR—

[lua]

myTable.player = display.newSprite …

for p,v in pairs( myTable ) do

display.remove( myTable[p] )

myTable[p] = nil

end

[/lua]

Thank you Tom and Brent ^^

I think i got it.

 

but if i put the objects in tables… it won’t let my game more slow ?

It shouldn’t. In fact, it might actually speed things up in the places where you’re clearing out these tables. By having them in one organized table, you can localize that table to the clear function, and the loop will perform faster. Likely though you won’t notice any improvement… but certainly, don’t worry about this slowing your game down. :slight_smile:

I will note, however, that the “pairs()” function is slower than the indexed number method, simply by Lua performance. I suggest you do not use this clear function in a time-critical game loop cycle. Hopefully by your design, this can occur between scenes or somewhere that the player won’t notice any potential frame rate skip.

Got it.

It’s awesome the efficiency and velocity of the help team this forum.

Thank you so much ^^

What do you mean by it’s not removing its references?

I’d like remove the objects from memory.
The memory isn’t being cleaned up. For example : If i type :
print(someObjectOfMyGroup) It should print ‘nil’ but this reference isn’t released.

 

thanks ^^,

Hi Jesseh,

Are these objects stored in some kind of containing table, or do they have some other element(s) tied to them like transitions or event listeners?

Brent

i’m sorry … let me show the code :

 

local \_M = {} function \_M.new() local group = display.newGroup() local player = display.newSprite... group:insert(player) local function TouchFunction ... local function CollisionFunction... local function FreeMemory() for j=group.numChildren, 1, -1 do display.remove(group[group.numChildren]) group[group.numChildren] = nil end display.remove(group) group = nil --this shouldn't print 'nil' ? print(player) end player.touch = TouchFunction player:addEventListener("touch", player) player.collision = CollisionFunction player:addEventListener("collision", player) return group end return \_M

ops … the color of the code has changed o.O

i thought in put all my objects in a table and remove it like so :

 

local myTable = {} myTable.player = display.newSprite ... --After remove this way : for j=1, #myTable do display.remove(myTable[j]) myTable[j] = nil end myTable = nil 

 

But i don’t know if it would let the game more slow … what do you think ?

thanks, 

If you’re expecting “player” to be nil, it won’t. When you insert player into a group, you are inserting the object referenced by player into the group and not the variable itself. When you removed the object and set the value to nil in your for loop, it removed the object (which converted it to a table) and cleared the value in the group, but didn’t touch the value stored in “player”. What you need to do is set “player = nil” after you insert it into the table.

In the first code sample, I notice you’ve put the “player” into the group, but you’ve also placed listeners on the player object (“touch” and “collision”). Where do you remove those listeners? Somewhere else in the code that you didn’t include?

In the second code sample, that’s a reasonable approach, except that in your example, you’re adding the player as a dictionary item in the table (named “player”), and then when you clear you’re attempting to loop through by index numbers. It will not clear the items that way.

So, you’d have to change it one of two ways:

[lua]

myTable[#myTable+1] = display.newSprite …

for j = #myTable,1,-1 do --REVERSE ORDER!!!

display.remove(myTable[j])

myTable[j] = nil

end

[/lua]
—OR—

[lua]

myTable.player = display.newSprite …

for p,v in pairs( myTable ) do

display.remove( myTable[p] )

myTable[p] = nil

end

[/lua]

Thank you Tom and Brent ^^

I think i got it.

 

but if i put the objects in tables… it won’t let my game more slow ?

It shouldn’t. In fact, it might actually speed things up in the places where you’re clearing out these tables. By having them in one organized table, you can localize that table to the clear function, and the loop will perform faster. Likely though you won’t notice any improvement… but certainly, don’t worry about this slowing your game down. :slight_smile:

I will note, however, that the “pairs()” function is slower than the indexed number method, simply by Lua performance. I suggest you do not use this clear function in a time-critical game loop cycle. Hopefully by your design, this can occur between scenes or somewhere that the player won’t notice any potential frame rate skip.