Easy Signal Disconnect #
Disconnect all from signal #
Sometimes you’ll want to disconnect everything from a signal for various reasons. Maybe you need to reset the timer state or want to wipe all enemies connected to a respawn timer but aren’t 100% sure what signals are connected. The simple snippet below makes that easier to do, just pass a signal to it like signal_disconnect_all(my_timer_node.timeout)
and it’ll clear any connected callables!
I’d suggest making a Utils class that’s either a Global Autoload or uses static functions so you can call your helper functions with
Utils.function_name()
## Iterates through all connections on a signal and disconnects them. Can be helpful
## if a signal might have an unknown connection.
func signal_disconnect_all(target_signal : Signal) -> void:
for n in target_signal.get_connections():
target_signal.disconnect(n.get("callable"))
Safely disconnect a callable #
In a similar vein, maybe you’re dynamically connecting and disconnecting callables in code, but having to check if the callable is connected just to disconnect it gets annoying. It’s not hard or anything, but that extraneous if statement has always bothered me lol. So instead just make it a helper function and be done with it! Use it like signal_safe_disconnect(my_timer_node.timeout, my_callable_name)
and be done with it.
Is this necessary? Maybe not, but it is to me :)
## Checks if a callable is connected to the signal before disconnecting it
func signal_safe_disconnect(target_signal : Signal, callable : Callable) -> void:
if target_signal.is_connected(callable):
target_signal.disconnect(callable)