Can't use get_node() with absolute paths from outside the active scene tree

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jarlowrey
:warning: Old Version Published before Godot 3 was released.

Attempting to instance a node and add it as a child to another node (calling add_child and set_owner). In the script attached to the instanced node, I am trying to get the scenes root node via get_node("/root"), but it throws this error. get_tree() is returning null.

:bust_in_silhouette: Reply From: rredesigns

The method is get_tree().get_root()

Also, set_owner() shouldn’t be necessary if you are adding the child to the right node. And try to leave as little logic as possible in things that spawn massively like bullets. In any case create a global script in the root node and make it call other methods in the instances.

how does this work when get_tree() returns null? seems get_root() would need something more than null to be able to be called… no?

Allen Kennedy Jr. | 2020-05-27 17:14

gettree could never return null. Any scene will always have a root, so that’s just imposible.

rredesigns | 2020-05-27 17:36

Get_tree can and DOES return null, when your scene is not part of a tree. for example, when you instance it via code and have not added it in anywhere.

Allen Kennedy Jr. | 2020-05-27 17:49

As far as I know scripts attached to an instanced scene shouldn’t be executed until the scene has entered the tree.

I’m kinda lost here. It’s been like 2 years since the last time I used Godot too, some things I don’t remember, some things might have changed.

I still don’t get it. There’s only one tree, so what are you trying to achieve here, and why can’t you just create a probe scene, attach it to the main scene and do all the path finding you need?

rredesigns | 2020-05-30 02:43