0 votes

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")
    add_child(asteroid_instance)
func handle_asteroid_destroy():
    ... # TO SOME STUFFS WHEN ASTEROID DESTROYED...
    ... # How to disconnect this handler from here?

But the problem is, I want to disconnect handleasteroiddestroy 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?

in Engine by (138 points)

1 Answer

+1 vote
Best answer

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")
    add_child(asteroid_instance)

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")
by (1,650 points)
selected by

@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?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.