Godot Engine:Parent和Owner

Parent
  • A node Parentis its parent tree scene
Owner
  • A node Ownermay be any other than its effective parent node (parent) or grandparent (grandparent) upper node i.e. node of the tree
  • When a storage node that it owns node also will be saved
  • If you do not change the default Owner, you can take it as a scene where the top node, if the node itself is the top node then it Ownerisnull
Experiment 1: static scene structure defaultOwner

Here Insert Picture Description

Code of each node is the same
extends Node

class_name TestNode

func _ready():
	var parent_name = "NULL"
	var owner_name = "NULL"
	if get_parent() != null:
		parent_name = get_parent().name
	if owner != null:
		owner_name = owner.name
	print(name + "'s parent is <" + parent_name + "> and it's owner is <" + owner_name + ">" )
	
Output

node_3's parent is <node_2> and it's owner is <node_0>
node_2's parent is <node_1> and it's owner is <node_0>
node_1's parent is <node_0> and it's owner is <node_0>
node_0's parent is <root> and it's owner is <NULL>

Experiment two: dynamically created nodeOwner
extends Node

func _ready():
	var n = TestNode.new()
	n.name = "<the one created in runtime>"
	add_child(n)

Output
<the one created in runtime>'s parent is <node> and it's owner is <NULL>

summary

  • Default static generated node Owneris its parent, if it has no parent isOwner
  • Dynamically generated default node Ownerisnull
Published 261 original articles · won praise 134 · views 80000 +

Guess you like

Origin blog.csdn.net/hello_tute/article/details/103929055