How to check if a display object is a group, and destroy it

I’m trying to do cleanup on an entire level of a game. Some of the display objects are created via display.newImage, but some are created via display.newGroup.

When I go through the tables and delete everything like this:

for i in pairs (enemies) do  
 enemies[i]:removeSelf();  
 enemies[i] = nil;  
end  

That works if the enemy is just an image, but throws an error if it is a more complex group object (attempt to call method ‘removeSelf’ (a nil value)). So I need to know how to determine if an enemy is a group or not, and how to destroy it if that is the case. [import]uid: 52127 topic_id: 10142 reply_id: 310142[/import]

There is not yet any supported and future-proof way.

I’ve asked Ansca for a RTTI (run-time type indicator) function but that won’t make it into the product until weeks after the code-freeze has ended.

In the meantime (I’ve not tested this) you might try checking for the displayObj.numChildren attribute. If it exists, it’s probably a group, otherwise, it’s probably another type of object…

Let us know if that works [import]uid: 6175 topic_id: 10142 reply_id: 37011[/import]

What is the best way to remove a group, since you can’t use removeSelf() ? [import]uid: 52127 topic_id: 10142 reply_id: 37047[/import]

Jonathan Beebe modified the cleanGroups() function in the director.lua file to remove ALL corona display objects within a group, instead of just groups within a group.

Look at the code in the director.lua file to see how it’s done.
[import]uid: 23636 topic_id: 10142 reply_id: 37069[/import]

The following functions seem to work for me… but it would be nice is Corona would support equivalent ones:

[lua]local coronaMetaTable = getmetatable(display.getCurrentStage())

— Returns true for any object returned from display.new*().
– note that all Corona types seem to share the same metatable…
isDisplayObject = function(aDisplayObject)
return type(aDisplayObject) == “table” and getmetatable(aDisplayObject) == coronaMetaTable
end

— Returns true if aDisplayObject is a Corona-Group “table/array”.
– test uses existence of “insert” function for element
– as non-group display object has no registered insert()
isDisplayObjectGroup = function(aDisplayObject)
return isDisplayObject(aDisplayObject) and (aDisplayObject.insert ~= nil)
end[/lua]

-FrankS.
[import]uid: 8093 topic_id: 10142 reply_id: 37090[/import]

Just check if the numChildren property is set:
[lua]if myObj.numChildren then …[/lua] [import]uid: 51516 topic_id: 10142 reply_id: 37123[/import]

bump----did we ever get an RTTI api method??

Thx

Dewey

This came up in another post the other day and as far as I know we have not added it.  It would be a convenience method at best.  There are several ways to test for a group vs a display object, like numChildren or the insert() function.   You also can when you create a display.newGroup() simply add a .isGroup = true member that you can test against too.   And if you were really adventurous, you could always override the display.newGroup() object with your own that added the isGroup member for you.

We could certainly add that, but it means pushing something else off.

I would recommend that if this really important, that you go to http://feedback.coronalabs.com and request it or if it’s there vote it up.  We do use this to plan new features. 

bump----did we ever get an RTTI api method??

Thx

Dewey

This came up in another post the other day and as far as I know we have not added it.  It would be a convenience method at best.  There are several ways to test for a group vs a display object, like numChildren or the insert() function.   You also can when you create a display.newGroup() simply add a .isGroup = true member that you can test against too.   And if you were really adventurous, you could always override the display.newGroup() object with your own that added the isGroup member for you.

We could certainly add that, but it means pushing something else off.

I would recommend that if this really important, that you go to http://feedback.coronalabs.com and request it or if it’s there vote it up.  We do use this to plan new features.