在Godot中,"disable"一个Node
的最简单办法就是把它从场景树中移除,但不删除它,这样当我们想启用它的时候可以再把它加入场景树,注意把节点从场景树移除的时候,同时也将它从所在组里移除了,如果我们想把它保留到组里,可能就要用到下面的方法。
Node
类本身没有hide
和show
方法,所以调用前先用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")