+1 vote

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.

in Engine by (695 points)

1 Answer

+2 votes
Best answer

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.

by (372 points)
selected by

how does this work when gettree() returns null? seems getroot() would need something more than null to be able to be called... no?

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

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.

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?

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.