This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

If I want to find a node by its name I usually do get_tree().get_root().find_node(), but it doesn't work for instanced scenes, even if they are placed in the tree in the editor.

Whats the reason behind making it to work like that? I often create new scenes from branches of the main scene and suddenly all code dependent on find_node() stops working because its not a node I'm looking for, but an instanced scene.

in Engine by (17 points)
edited by

4 Answers

0 votes

Possibly a bug, you might want to report or ask in the Github issues.

Otherwise you could work around it with something like this (though this returns the first name match it finds):

func _ready():

    var node = find_node_by_name(get_tree().get_root(), "my_node_name")
    if(node): printt(node, node.get_name())

func find_node_by_name(root, name):

    if(root.get_name() == name): return root

    for child in root.get_children():
        if(child.get_name() == name):
            return child

        var found = find_node_by_name(child, name)

        if(found): return found

    return null
by (5,286 points)
+1 vote

First of all, try to use Remote Inspector to see what scene tree actually is made.
enter image description here

then I guess, you can see the name of instanced node is something like @block@155 like screenshot above.
you should set a specific, non-conflict name to instanced node if you want to find it by find_node
if you set it as same name with another which is already in scene tree and same depth, it will be renamed to something unique.

by (9,800 points)

só para complementar. Assim que você faria para mudar o nome:

    myInstance = preObj.instance()
0 votes

What I do is adding it to a group and use getnodesin_group to call it

for example I have a Player scene. I added it to group "player"

to get player I do

var player = (gettree().getnodesingroup("player"))[0]

by (29 points)
+3 votes

Please try the following and report back if it resolved the issue:

get_tree().get_root().find_node("BlaBla", true, false)

Setting owned to false solved similar problems I had.

by (270 points)

with "owned = false" works, what does this "owned" does?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.