if object ~= nil seems to be being ignored?

I seem to have a problem with ‘if object ~= nil’

Basically I have a function that removes objects as and when I need, in this function all objects that I wish to have removed look exactly like this:

 if bossGroup ~= nil then  
 bossGroup:removeSelf()  
 bossGroup = nil  
 end  

Yet I get a console error:
ERROR: Attempt to remove an object that’s already been removed from the stage or whose parent/ancestor group has already been removed.

And it points to certain objects (but not all) within my remove function list. However if I’m stating ‘if object is not equal to nil’, why would I still be getting the error? [import]uid: 107915 topic_id: 34934 reply_id: 334934[/import]

  1. It’s easier to use display.remove() since that both performs :removeSelf() and if ~= nil check
  2. Technically even if you set bossGroup to nil it still exists until garbage cleanup. (You can even make it come back to life this way). Smells more like an order-error in your code somewhere… [import]uid: 41884 topic_id: 34934 reply_id: 138823[/import]

Thanks Richard. display.remove seems a much better way of doing things!

You were right, essentially it was an order-error in the code that was causing the issue. I was removing the group first and then below that trying to remove the children of that group, not realising that once a group has been removed all the children are removed as well!

Having said that all the children would be at nil anyway (one the group had been removed) so not sure why it was still showing the error! Anyway, it’s good to know all the children are removed so I only need to call display.remove on the group, saves some typing…thanks again. [import]uid: 107915 topic_id: 34934 reply_id: 138843[/import]

Lua has some hidden picky-ness when it comes to addressing objects that have been destroyed but not yet cleaned. By trying to address the object you’re basically delaying the cleaning process, but then Lua gets confused because its parent object is gone too.

I imagine if the garbage collection cycle occured before you attempted :removeSelf() it might behave more as you expect, but there are situations where that is not the case. (ie: if aTable.isHere ~= nil will give you an error if aTable{} does not exist, for instance. Technically the statement is true if conditions aren’t perfect most programming languages will give you “something is wrong” instead of “technically right”)

Glad it helped. [import]uid: 41884 topic_id: 34934 reply_id: 138846[/import]

  1. It’s easier to use display.remove() since that both performs :removeSelf() and if ~= nil check
  2. Technically even if you set bossGroup to nil it still exists until garbage cleanup. (You can even make it come back to life this way). Smells more like an order-error in your code somewhere… [import]uid: 41884 topic_id: 34934 reply_id: 138823[/import]

Thanks Richard. display.remove seems a much better way of doing things!

You were right, essentially it was an order-error in the code that was causing the issue. I was removing the group first and then below that trying to remove the children of that group, not realising that once a group has been removed all the children are removed as well!

Having said that all the children would be at nil anyway (one the group had been removed) so not sure why it was still showing the error! Anyway, it’s good to know all the children are removed so I only need to call display.remove on the group, saves some typing…thanks again. [import]uid: 107915 topic_id: 34934 reply_id: 138843[/import]

Lua has some hidden picky-ness when it comes to addressing objects that have been destroyed but not yet cleaned. By trying to address the object you’re basically delaying the cleaning process, but then Lua gets confused because its parent object is gone too.

I imagine if the garbage collection cycle occured before you attempted :removeSelf() it might behave more as you expect, but there are situations where that is not the case. (ie: if aTable.isHere ~= nil will give you an error if aTable{} does not exist, for instance. Technically the statement is true if conditions aren’t perfect most programming languages will give you “something is wrong” instead of “technically right”)

Glad it helped. [import]uid: 41884 topic_id: 34934 reply_id: 138846[/import]