+1 vote

I have a fight scene with the hierarchy of

Main
-Player
-UI

UI creates another node, Enemy, and connects to it using

var root = get_tree().get_root()
root.add_child(enemy_node)
connect("enemyTurn", enemy_node, "_on_UI_enemy_Turn")

This works fine, now in the Enemy node, I try to find the UI node with

var root = get_tree().get_root()
var ui_node = root.find_node("UI")

Printing root and ui_node gives

root:[Viewport:1220][Object:null]
Godot version 3.4.2
in Engine by (16 points)

2 Answers

+2 votes
Best answer

The method find_node has an bool parameter "owned" which has default variable true.

You can take a look at Godot Doc, it says that "If owned is true, this method only finds nodes whose owner is this node. This is especially important for scenes instantiated through a script, because those scenes don't have an owner."

In your case, root = gettree().getroot() don't have owner, so you get "Object : null".

by (526 points)
selected by
0 votes

Try using get-node() instead of find-node().

by (17 points)
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 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 webmaster@godotengine.org with your username.