Implement a remove_user_signal method #4959
Replies: 3 comments 1 reply
-
I think that there is no need for this, removing signals is like removing methods. Frequent addition/deletion/connection/disconnection of signals should be avoided. If you have many similar signals, then it's better to use one signal with different arguments. I don't remember using |
Beta Was this translation helpful? Give feedback.
-
It is a very rare use case. Frequent connection and disconnection of signals is fine. Evidence of this is the fact there is a However, it is better when the object that uses the signal is responsible for said connections and disconnections (or the connection is there for the lifetime of the objects). If there is one script connecting and disconnecting and a different script receiving the signals, it makes it harder to reason about the code. To be clear: it is not a no-no, but do it consciously. And that is the issue with If disconnect removes the connection when there are none left - which is the other alternative you suggest - the code that connects to it does not know if it has been removed. Thus, you must always check if the signal exists, and create it if it does not, before attempting to connect to it. Perhaps this fine with you, if this is how you are already using user signals. For example, you have generic mechanism that might emit a user signal if it exists, and then it is responsibility of the code that connects to create the user signals. However, that is not the only possible pattern. A user signal not existing can also be meaningful. Because it needs a mechanism for emitting it, and there is no point on creating user signals if you can't emit them… So there is no point in creating signals to connect to them if said mechanism that would emit them is not in place... Which would be the case if the mechanism is not generic for the possible user signals. To be fair, even when connections are there for the lifetime of objects we can run into problems. For example, when an object that yields is freed, when the signal is emitted Godot will try to resume execution, but can't. Thus, it is better when the object that yields is also responsible for its own lifetime. We can work around that, by the way: if we used connections with By the way, if you have many signal - as @dalexeev mentions - you can make a single one and distinguish by arguments. However, you need to think about how it will be used. Because merging different signals might mean that objects will get unnecessary notifications. For example, In fact, that is one way you could create and remove signals: create and remove objects that have signals. And you can have arrays or dictionaries of said objects. But before you go on doing that, I would challenge the idea that you need this. As I said at the start, it is a very rare use case. Do you actually need to create signals at runtime, or is it just a convenience to not declare signals? Because if it is a convenience for not declaring signals I have an alternative: resource based communication. You can define signals on a custom resource via an script. You can create resource files of the custom resource type on the file system. And everywhere you |
Beta Was this translation helpful? Give feedback.
-
Closed by godotengine/godot#90674 |
Beta Was this translation helpful? Give feedback.
-
I find it odd that there is a add_user_signal method on Object but there is no way to remove it. I see that that a signal is erased from the internal signal_map on Object, only on the destructor and on disconnect if it's not a user signal.
I suggest either to implement a method remove_user_signal or remove user signals on the disconnect method (however this would have impact on already existing applications).
Beta Was this translation helpful? Give feedback.
All reactions