Godot: Node get_child(String)

Created on 13 Sep 2018  路  6Comments  路  Source: godotengine/godot

Godot 3.1 alpha 1

When a script uses PackedScene to keep script and player model independent , any time we can change the player model sub scene, while the script will continue to work.

export(PackedScene) var playerModel

We need a way in script to quickly access each child of playerMode sub scene in the example above.

There is Node get_child(int) , but changing the sub scene node order or after a re import , it won't work.
We need a stable way that uses node names : get_child(String).

archived

Most helpful comment

I've added a mention to get_node in the get_child documentation, just in case.

All 6 comments

You mean like get_node()...?

I understand that get_node is the way to go and I already got used to it, but I also remembered it was weird to have the get_child function there without working like the get_node does. Is there a reason when you would use the get_child? I couldn't find any use for it.

get_child() is a direct reference to the node, so there's no search needed. It is used a lot in the engine code, especially when iterating the children. Now get_node() can get _any node_ in the tree, not only the children, so the naming is accurate.

get_child() is a direct reference to the node, so there's no search needed. It is used a lot in the engine code, especially when iterating the children. Now get_node() can get any node in the tree, not only the children, so the naming is accurate.

This is something to put on Godot documentation.

What about get_child(String name) ?
If we want a fast search among direct childs of a parent.

There's no point to add get_child(name) as it would be the same as get_node(name).
get_node is actually meant to get children, you can use it to access parents or node at an arbitrary path, but that's bad practice - your should keep your scenes self-contained as far as possible.

I've added a mention to get_node in the get_child documentation, just in case.

Was this page helpful?
0 / 5 - 0 ratings