What ways of destroying a signal receiver automatically disconnects the signal?

Godot Version

Godot Version 4.4

Question

I was wondering if people could tell me what ways of destroying an object that also receives signals also leads to automatically disconnecting said signal. E.g. I assume if I call queue_free on the signal receiver, I wouldn’t have to disconnect it beforehand from all the signals it is listening to.

The joys of signals is that they aren’t concerned about who is listening. So, queue_free wouldn’t create any broken code from signals.

1 Like

Freeing an object automatically disconnects all signals connected to that object. The only caveat is that queue_free only schedules the object for deletion, so does not disconnect signals itself. Signals can still be received between the call to queue_free and the actual deletion, so you will either have to manually disconnect unwanted signals or guard against them in the object being deleted.

1 Like

I see. But when the scheduled deletion via queue_free takes place, it also disconnects automatically, right?

Not broken, but couldn’t it in theory lead to performance penalties if signals keep getting sent to objects that are already released?

No, because they aren’t really sent to. The signals are sent out, but if there is nothing to recieve them then there would be no extra load.

So once something is free it no longer recieves the signals.

It is why signals are so good for communication with parents or transient entities.

1 Like

Yes, the signals are disconnected when the actual deletion happens.

1 Like

If you’re really worried about signals being sent to a deletion scheduled object, you could add a bool that would be flipped when the entity is “dead” like is_dead and use an if statement where you usually process the signal.

1 Like

I am not worried about the case where they are scheduled (though also good to know that I should think of that case), just about the possibility of it leading to performance penalties due to signals still being sent to “dead” objects👍