0 votes

i want use get_owner function for get root node(top node).

but i can not get top node from script of added node.
i make parent node.
and i created a node named "child" under it.
"added node" added by script from the "parent node".

node structure of node is like this.
*top
**parent(attached parent.gd)
***child
***added_child(attached added_child.gd)
enter image description here

parent.gd(parent.gd called added_child scene,and output it.)

extends Spatial

var added_child = load("res://test/added_child.tscn");

func _ready():
    var instance = added_child.instance();
    self.add_child(instance);

added_child.gd(this script output "[Object:null]")

extends Spatial

func _ready():
    print(self.get_owner(),"by_added_child");

thank you,please help about this.

in Engine by (200 points)

1 Answer

0 votes

Calling add_child() does not set the owner property of the added node. Instead, it sets the node's parent. The parent of a node can be retrieved via get_parent().

Bottom line - change your get_owner() call to get_parent().

by (12,460 points)

Thank you very often. understood about it.

i decide get the root node via parent node.

but there are a few question.
why added node not have owner property?
spatial node inherits node class with owner property.

why added node not have owner property?

I'm not sure I understand your question. Anything that inherits from Node will have an owner property (as you correctly stated). So, the node you added does have an owner property, but it is not automatically set by calling add_child().

That's because an owner does not have to be a direct parent. So, you should expect owner (and thus, get_owner()) to return null unless you've explicitly assigned it to something.

Thank you very much.
I found out that I need to set the owner property.
I thought the added node would have an owner by default.

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.