Get child by index

Get child by index
0.0 0


Given a node in Lua, what is the best way to get a child directly by index, without too much overhead or creating too much garbage?

Does one have to create an array of children etc.?

This is using 2.1.5. Thanks.


Any caveats to

*node:getChildren():objectAtIndex(i)* ?

Would it be OK to run that in accessors run many times per frame?

Won’t it create garbage? Is there a better way to get specific children?


I have to say, I’m having a problem with the above code, where the parent sprite is CCSpriteBatchNode and the children are CCSprite.

When I call getChildrenCount() on the batch node, it says I have a bunch of children as expected (say 10). But when I try to get one (say index 5), I get a userdata with no methods on it (like no setColor, no setScale, no getChildrenCount).

If somewhere I stash away a reference to the children, it works fine. But if I don’t, it does not.

This leads me to wonder if they aren’t somehow being garbage collected. Is there something special I have to do if the sprites are parented by a batch node?

Or it might just be a bug in my code, but I’m not seeing it.

I’m literally seeing there are 10 children, getting a child, and having a userdata without its own getChildrenCount method on it. How can that happen?


Maybe,You can try to use tolua.cast method to cast the CCObject to CCSprite.


Thanks I can try that, but I do find it odd that it would work if I kept a reference to the objects around (somewhere else) and still accessed it through the child array, but not through the child array if I don’t keep the references.


OK I tried a few things and learned a few things.

First, the odd thing is, walking down about 3 levels of nodes, it’s only the last level where I start to get “method-less objects”. Not the earlier ones.

So in the node walking, I added some casts to CCNode, that got me through there. No more dying on getting the last child.

Of course, when my code then tries to set the color on the resulting object, it fails with missing setColor method. So I added a cast to CCSprite.

And that works. But why?

I am guessing, that the wrapper objects have their derived identity, and stick around as long as they are referenced in Lua. And that even if I get them through another interface (that is just an array of objects), I get the “derived” wrapped object. But then if they aren’t referenced, they might (or might not be garbage collected, and later when I get them through the array-of-objects interface, I only get the “base” wrapped object, not the full “derived” one.

Does that sound right? Is that how tolua works? (I’ve used SWIG but not tolua.)

So how heavyweight are these wrapper objects? Derived vs. base? At this point, I’m doing a lot of making child arrays, getting element, casting, and so on to walk down a path of nodes to get a child sprite. It’s starting to tingle my garbage-collection spider sense. I’m wondering if I shouldn’t just use closures or maybe store an additional reference in some other objects so I don’t have to look them up. (I was hoping lookups would avoid extra storage.)

How do people normally walk down a tree of nodes, in Lua, to get a specific one? (Say fourth child of first child of third child of a node?)