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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

开发游戏的老王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值