-
-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a way to avoid creating callbacks when connecting signals #1990
Comments
first, one suggestion: maybe reuse same keyword for same feature |
I would say the opposite: putting callbacks into a function makes it more readable than the proposed inline solution. In my oppinion, your problem (So I need to jump to on_ methods every time to know what I do there.) doesn't justify a new keyword + syntactic sugar. |
Isn't the bigger problem that this would require full closure support? For instance: func _ready():
var foo = ...
var f = when pressed:
print(foo) Where should I vaguely remember that there were some previous discussions about introducing lambdas. |
I dont think that this proposal it's the best way to resolve the problem of legibility, its sounds like just put the connection code in another place with a litte better code organization, but its not solve the real problem of complex architetures. I solved this question in my projects using intermediate proxies to centralize the connections and show me the dependencies in a more efficiente way: I thintk that the rastreability problem of a lot of connections could be solved more efficiently with a better arquiteture and separation of resposabilities in your especific project. |
|
@Host32 |
@Jummit Yes, I really think that frequent jumping from one method / class to another worsens the readability of the code, especially when the code base grows. For example imo using yield looks clearer than |
func _ready():
print('hello')
var foo = 1
var f = when pressed:
print(foo)
print('world')
this is how I see it. |
Closing in favor of lambdas #2431 |
Describe the project you are working on
Godot plugins
Describe the problem or limitation you are having in your project
Gdscript signal system is powerful and flexible.
But it seems the more connections I have the less readable my code is.
I also faced this problem in other frameworks that use similar pattern.
I think this is mainly because of:
connect()
, I also create a callback method and put there what I want to do when the signal fired. But that means the code becomes less straightforward = less readable.So I need to jump to
on_
methods every time to know what I do there.Describe the feature / enhancement and how it helps to overcome the problem or limitation
A way to write more straightforward code without the need to create callback methods (at least explicitly) when connecting signals would solve the problem.
One way could be adding
when
keyword that could work this way:That means each time you press the button, 'button pressed' is printed.
Then you can continue to write the code as usual
In this case 'hi' would be printed, without waiting for the button press, unlike
yield
works for example.As you can see there's no need for creating callbacks like
on_pressed()
. The code becomes more readable and straightforward.Another example:
This would print 'button pressed' twice every time button is pressed.
This ability to write multiple
when
blocks would be very useful to follow divide-and-conquer algorithm and have small separated code blocks.Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
Each
when
statement could implicitly create a unique function and connect a signal ('pressed' in the example above) to it.Or this could be created as a method of object where
when
statement is created.In order to be able to disconnect this implicitly created function, the statement could return it for example this way:
Where
f
could be the callback nameI think there's no need to return Error code (that
connect()
does) because the callback function with connected signal should be created anyway.other_node
, this syntax could be used:This prints
pressed
signal args array (which are passed inemit_signal
) if any.Not sure if it should have connect flags like
connect()
has.The downsides I can see:
when
)when
keyword (c#, nim etc)when
statementsIf this enhancement will not be used often, can it be worked around with a few lines of script?
not possible now.
Is there a reason why this should be core and not an add-on in the asset library?
Gdscript syntax feature that may hopefully be used often.
The text was updated successfully, but these errors were encountered: