The Godot Barn
Sign in
Sign in
Home
News & updates
Explore
Articles
Snippets
Shaders
Themes
Submit content
Sign in to submit content
Give or get help
Tutorials
Questions
Conversations
Coming soon!
Easy Signal Disconnects
0
Description
"## Disconnect all from signal\r\n\r\nSometimes 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!\r\n\r\n> 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()`\r\n\r\n\r\n``` GDScript\r\n## Iterates through all connections on a signal and disconnects them. Can be helpful\r\n## if a signal might have an unknown connection.\r\nfunc signal_disconnect_all(target_signal : Signal) -> void:\r\n\tfor n in target_signal.get_connections():\r\n\t\ttarget_signal.disconnect(n.get(\"callable\"))\r\n```\r\n\r\n## Safely disconnect a callable\r\n\r\nIn 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.\r\n\r\n> Is this necessary? Maybe not, but it is to me\r\n\r\n``` GDScript\r\n## Checks if a callable is connected to the signal before disconnecting it\r\nfunc signal_safe_disconnect(target_signal : Signal, callable : Callable) -> void:\r\n\tif target_signal.is_connected(callable):\r\n\t\ttarget_signal.disconnect(callable)\r\n```\r\n"
Comments
Log in to post a comment
Licensed under the MIT license
See the full license details
Submitted by
Dragon1Freak
Table of contents
Compatibility
Works in Godot
4.x
GDScript
Tags
Signals
utility
Share this snippet
Share on Bluesky
Share on X / Twitter
or share this direct link:
https://thegodotbarn.com/contributions/snippet/358/easy-signal-disconnects
Please wait ...
Okay
Okay
No
Yes
Okay