Skip to main content
Header image by Roberto Júnior © Roberto Júnior

How to Disable a Node in Godot

Learn how to disable a node in both Godot 3 and 4.

Introduction #

In this tutorial, we will look at how to disable a node in Godot. This is useful for any reason, like saving memory, preventing a hidden interface from being processed, or even preventing an enemy from being active under certain conditions.

Using Godot 4 #

From the inspector #

Godot 4 has a new Disabled property in the inspector under Node -> Process -> Mode. By default, the node is set up as Inherit.

How to select the disable property from the insepctor

Using GDScript #

Is it possible to disable a node using GDScript like this:

process_mode = Node.PROCESS_MODE_DISABLED

Why is this property helpful? #

This was a very highly asked feature in Godot, as it was hardly possible to disable a node in Godot 3. The significant difference between Godot 3 and 4 is that using the Disabled property will disable the node and all its children. Simple and useful.

Using Godot 3 #

It becomes more complicated as there is no comparable disabled property in Godot 3. However, there are some techniques to achieve kind of the same result.

In Godot, it is possible to disable some processes like _process, _physics_process, or _input. The goal is to disable all of them 🙂

Here is an example to achieve this:

# I used "_ready" here, but it could be in any other function.
func _ready() -> void:
	set_process(false)
	set_physics_process(false)
	set_process_input(false)
	set_process_unhandled_input(false)
	set_process_unhandled_key_input(false)

This code will disable some processes, but you must know it is not entirely disabled. The other important point here is that children will still be processed.

Using the method propagate_call #

If you want children to be disabled, you need to call the same functions on each of them. Don’t be scared! Godot comes with a practical method called propagate_call. When calling this method, it will propagate the call to all children, precisely what we want.

Stop talking, and let’s see how to use it:

func _ready() -> void:
	propagate_call("set_process", [false])
	propagate_call("set_physics_process", [false])
	propagate_call("set_process_input", [false])
	propagate_call("set_process_unhandled_input", [false])
	propagate_call("set_process_unhandled_key_input", [false])

Let’s analyze this code together. Instead of using set_process or another one directly, we use the method propagate_call and pass as the first parameter the desired process we want to stop. The second parameter is an array of arguments to pass to the desired function. In this example, false will be passed as the first parameter, but if we add another value to the array, this value will be passed as the second parameter.

The downside of this method #

Be careful using this method! In the previous example, it will disable the current node and all its children; it might be what you want but keep in mind that when you activate back the node using the same method but changing the parameter from false to true, it will enable all children too, which means that if you already disabled a specific process on a precise child, it will be enabled again, so don’t forget to disable it again.

Conclusion #

Godot 4 fixes a massive problem with an elegant solution, while in Godot 3, you have methods to do it, but you need to be careful when using them.

Thank you for reading me. If you enjoyed this article and consider offering me a ☕, I will be very grateful, and it will help me write new articles.

Tags

See Also

No results...