godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.14k stars 93 forks source link

Add connection_check flag or parameter in connect/disconnect methods #1585

Open me2beats opened 4 years ago

me2beats commented 4 years ago

Describe the project you are working on: gdscript editor plugins

Describe the problem or limitation you are having in your project: often when connecting a signal, I cannot know in advance whether the signal has already been connected to a method. In this case, I should use if not obj.is_connected(), otherwise I get the error

Signal is already connected to given method in that object

this check (using is_connected) makes the code noisier and less readable.

Describe the feature / enhancement and how it helps to overcome the problem or limitation: I think it would be convenient to check a connection when using CONNECT_CHECK flag in Object connect method.

Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams: enum ConnectFlags: CONNECT_DEFERRED = 1 CONNECT_PERSIST = 2 CONNECT_ONESHOT = 4 CONNECT_REFERENCE_COUNTED = 8 _CONNECTCHECK = 16 Connect a signal only if it is not connected

the same goes for the disconnect method.

If you try to disconnect a connection that does not exist, the method will throw an error. Use is_connected() to ensure that the connection exists

connect_check parameter or flag could have been implemented to free a user from having to use is_connected()

If this enhancement will not be used often, can it be worked around with a few lines of script?: It's possible to create a ConnectUtils class and a static function something like connect_once() in it (or ConnectUtils singleton with connect_once() method) that would do the connection check, but this generates additional dependencies and it is not much more convenient than just using if ! is_connected() in the code every time. Using a flag would be more convenient imo Is there a reason why this should be core and not an add-on in the asset library?: The connect method belongs to the Object class, so I think it would be more logical and handy to have this feature in the core.

KoBeWi commented 4 years ago

often when connecting a signal, I cannot know in advance whether the signal has already been connected to a method.

This doesn't sound like common problem and implies that something is wrong with your code. I never had this issue nor seen anyone mentioning it before.

me2beats commented 3 years ago

@KoBeWi I often face this problem when creating tool scripts/editor plugins.

  1. Hotreloading can cause this (at the moment I don't have a simple example)
  2. Some Editor Plugin methods are fired twice, for example https://github.com/godotengine/godot/issues/40458

Some frameworks do these checks by default (for example kivy bind)