Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Remove Object References In Groups
Started by Jesseh Mar 13 2013 01:35 PM

- - - - -
10 replies to this topic
remove objects reference group
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Jesseh

[GLOBAL: userInfoPane.html]
Jesseh
  • Enthusiast

  • 39 posts
  • Corona SDK

Hi everyone,

 

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

 

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.



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,615 posts
  • Enterprise

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



[TOPIC: post.html]
#3

Jesseh

[GLOBAL: userInfoPane.html]
Jesseh
  • Enthusiast

  • 39 posts
  • Corona SDK

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 ^^,



[TOPIC: post.html]
#4

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

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



[TOPIC: post.html]
#5

Jesseh

[GLOBAL: userInfoPane.html]
Jesseh
  • Enthusiast

  • 39 posts
  • Corona SDK

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



[TOPIC: post.html]
#6

Jesseh

[GLOBAL: userInfoPane.html]
Jesseh
  • Enthusiast

  • 39 posts
  • Corona SDK

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, 


[TOPIC: post.html]
#7

Tom

[GLOBAL: userInfoPane.html]
Tom
  • Moderator

  • 1,480 posts
  • Corona Staff

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.



[TOPIC: post.html]
#8

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

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:

myTable[#myTable+1] = display.newSprite ...
for j = #myTable,1,-1 do --REVERSE ORDER!!!
display.remove(myTable[j])
myTable[j] = nil
end

---OR---

myTable.player = display.newSprite ...
for p,v in pairs( myTable ) do
display.remove( myTable[p] )
myTable[p] = nil
end



[TOPIC: post.html]
#9

Jesseh

[GLOBAL: userInfoPane.html]
Jesseh
  • Enthusiast

  • 39 posts
  • Corona SDK

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 ?



[TOPIC: post.html]
#10

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

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. :)

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.



[TOPIC: post.html]
#11

Jesseh

[GLOBAL: userInfoPane.html]
Jesseh
  • Enthusiast

  • 39 posts
  • Corona SDK

Got it.
It's awesome the efficiency and velocity of the help team this forum.
Thank you so much ^^



[topic_controls]
[/topic_controls]