Disconnect signal that connected from script

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By rico345100

I connected signal when a scene is instantiated.

# AsteroidSpawner.gd
func spawn_asteroid():
    var asteroid_instance = asteroid_scene.instance()
    asteroid_instance.connect("destroy", self, "handle_asteroid_destroy")
func handle_asteroid_destroy():
    ... # How to disconnect this handler from here?

But the problem is, I want to disconnect handle_asteroid_destroy handler when object destroyed(or will destroy, use queue_free). How do I disconnect signal on specific object? Is Godot Engine will free the handler automatically?

:bust_in_silhouette: Reply From: Eric Ellingson

you’ll need to save a reference to the asteroid in your AsteroidSpawner

var asteroid_instance

func spawn_asteroid():
    asteroid_instance = asteroid_scene.instance()
    asteroid_instance.connect("destroy", self, "handle_asteroid_destroy")

but then its as simple as calling .disconnect() with the exact same params called for .connect()

func handle_asteroid_destroy():
    asteroid_instance.disconnect("destroy", self, "handle_asteroid_destroy")

However, I don’t think you need to worry about doing this. I think this is only for when you no longer want a node to listen to a signal, not for cleanup which should be done for you.

Also, if you don’t want to save the reference to the specific asteroid in the spawner, you can pass that along with the destroy signal:

# Asteroid.gd

func some_function():
   emit_signal("destroy", self)

then in your handle_asteroid_destroy() function:

# AsteroidSpawner.gd

func handle_asteroid_destroy(asteroid):
    asteroid.disconnect("destroy", self, "handle_asteroid_destroy")

@Eric Ellingson Thanks, however I want to make sure the handler was disconnected. I couldn’t find any related information about this, how do I get list of connected signal handlers?

rico345100 | 2019-11-09 05:52