GDScript:如何Disable一个Node

在Godot中,"disable"一个Node的最简单办法就是把它从场景树中移除,但不删除它,这样当我们想启用它的时候可以再把它加入场景树,注意把节点从场景树移除的时候,同时也将它从所在组里移除了,如果我们想把它保留到组里,可能就要用到下面的方法。

Node类本身没有hideshow方法,所以调用前先用has_method判断一下:

class_name NodeHelper

static func disable(node:Node):
	if is_instance_valid(node):
		node.set_process(false)
		node.set_process_internal(false)
		node.set_physics_process(false)
		node.set_physics_process_internal(false)
		node.set_process_input(false)
		node.set_process_unhandled_input(false)
		node.set_process_unhandled_key_input(false)
		if node.has_method("hide"):
			node.hide()
	else:
		printerr("the node you are trying to disable is not valid")

static func enable(node:Node):
	if is_instance_valid(node):
		node.set_process(true)
		node.set_process_internal(true)
		node.set_physics_process(true)
		node.set_physics_process_internal(true)
		node.set_process_input(true)
		node.set_process_unhandled_input(true)
		node.set_process_unhandled_key_input(true)
		if node.has_method("show"):
			node.show()
	else:
		printerr("the node you are trying to enable is not valid")

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

开发游戏的老王

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

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

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

打赏作者

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

抵扣说明:

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

余额充值