Godot Engine:Parent和Owner

Parent
  • 一个节点的Parent就是场景树上它的父级
Owner
  • 一个节点的Owner可以是除自身之外的任意有效的父节点(parent)或者祖父节点(grandparent)即节点树上的上级节点
  • 当存储一个节点时它所拥有的节点也都将会被保存
  • 如果不修改默认Owner的话,可以把它视为节点所在场景的顶部节点,如果该节点本身就是顶部节点那么它的Ownernull
实验一:静态场景结构中默认的Owner

在这里插入图片描述

每个节点的代码是相同的
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 + ">" )
	
输出结果

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>

实验二:动态创建的节点的Owner
extends Node

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

输出结果
<the one created in runtime>'s parent is <node> and it's owner is <NULL>

小结

  • 静态生成的节点的默认Owner是它的父节点,如果它没有父节点则Owner
  • 动态生成的节点的默认Ownernull
发布了261 篇原创文章 · 获赞 134 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/hello_tute/article/details/103929055