How can I disable/enable a node that is instanced in a scene without otherwise changing it?
Disable / Enable a Node in the Scene
The easiest way is to use a node's show() and hide() methods, which should work as long as it's a Node object or Spatial object.
Otherwise, there should be a disabled field, which you can set to either true or false.
How about for a UI node? Like one of the BoxContainers? They don't appear to be effected by hide()/show() or disable().
Just use Node as parent for BoxContainer and hide/show them.
@marty said: How about for a UI node? Like one of the BoxContainers? They don't appear to be effected by hide()/show() or disable(). You did something wrong: doing hide() or visible = false works on GUI controls too.
@marty said: How about for a UI node? Like one of the BoxContainers? They don't appear to be effected by hide()/show() or disable().
disabled
is not a function, it should be a field that you can see even in the inspector, though it could also be called visible
on some nodes (I'm not sure, it's been a while since I've messed with UI stuff specifically).
However, show() or or hide() should work... but here's some pseudo-code I wrote that seems to do the job:
# determines whether this node will be visible or not
var active
# Called when the node enters the scene tree for the first time.
func _ready():
active = true # default to true
func _process(delta):
# flip the visiblity of a node
if Input.is_action_just_pressed("flip_node"):
active = !active
# set the visible flag equal to active
visible = active
The intention here is ambiguous; fully disabling a node implies more than just not rendering the node, it would also entail preventing any kind of interaction with that node, and stopping any processes running on that node.
To fully stop a node and all its children from rendering, processing, receiving input et cetera, the simplest way is to pull this node out of the scene tree:
var my_node = $SomeNode
func _ready():
remove_child(my_node)
Then, to re-enable:
add_child(my_node)
Be careful with this, as Godot does not garbage collect Node
s, so if you somehow lose a reference to this node, it will hang around in memory for the remaining lifetime of the game.
You can still call functions on this node even while it is outside of the scene tree, but if you try to manipulate anything to do with the node that depends on it being inside of the scene tree, Godot may complain (e.g. you can't move_and_slide()
a KinematicBody
while it's outside of the tree.)
If you only want to disable some aspects of the node and not others, or your game depends on this node remaining in the scene tree always, or you want to disable a parent node but not its children, you can set the callbacks for the node (on top of setting visibility if you want that):
set_process(false)
set_physics_process(false)
set_process_input(false)
You can also require some boolean to be active for certain things to fire:
func _process(delta):
if is_active():
...
func is_active():
return this_cond and that_cond and that_cond_over_there