Discussion on getting node in Cocos Creator

In general, we set variables and then assign a node to it in the Cocos Creator.
But also we could use “node.children[0]”.
like this:
this.buttonPopup.children[5].active = true;
this.buttonPopup.children[6].active = false;
this.buttonPopup.children[7].active = true;
When the node is called only one time I often use this method.
But if we change the order of child nodes, it throws errors.
If could be considered as poor coding.
In case we set all the nodes into variables, there can be too many variables in the code.
I think this will cause a longer refresh time in the creator.

The question is which one is the proper way?
Which method is faster in code running?
Is it ok if we set as many variables in the code?
Or there is any other good way to solve this problem?

I am open to any opinion.

Why not use the tag property for this? Each node can be assigned an int as a tag, then you can getChildByTag(), and this is independent of child index in any array. You can also assign userInfo/userData. You can also create custom nodes and follow a more object oriented pattern, but in this simple scenario I would just set the tag of your nodes. (Edit, it seems tag is C++ cocos2d-x, Cocos Creator analogy below)

It seems in Cocos Creator you can also do this with a name for the Node. Then:

this.buttonPopup.getChildByName(“1”).active = false …
this.buttonPopup.getChildByName(“2”).active = true …

https://docs.cocos2d-x.org/creator/api/en/classes/Node.html#getchildbyname

Thanks for your good advice. I think using uuid can be solution. But what if code should be readable for further change? find by name also can throw errors if the parent change. And I wonder getChildByName is ok in speed.

I assume it will just return a null if it does not exist anymore on parent. Then you just null check before setting any properties.

Performance of any code depends on its use. If you have 10,000 Nodes, and are calling getChildByName 10,000 times each update frame, along with other complex algorithms, then I would advise not to do it that way. Stick to instance variables, or better designed code architecture to allow 10,000 nodes. In the simple example case, It is fine to use.

So you think setting many variables is not a good idea? So you suggest using getChildByName() rather than set a variable for the node?

That is a very subjective question. I was just trying to show other options to your first post in what you were saying and asking. Performance is rather subjective as you can get away with certain things at certain times and still ‘perform’ at 60fps etc. Build the game, then if you are getting below performance thresholds look for the biggest bottlenecks and work your way down. If getChildByName seems to be an issue at that point, then refactor.

1 Like

I see. Actually the speed is not a problem in this game. I was just asking it as a general option. Thank you.

1 Like

No worries. I also was just giving a general answer. I would usually always have a variable or reference/pointer available to any object I know I will be needing to modify at runtime etc. That or store the nodes in a better structured format (hash map etc) other than looking for it inside a child array of a parent.