-
-
Notifications
You must be signed in to change notification settings - Fork 20.2k
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
Implement return value from yield() #1397
Comments
Unpleasant workaround:
|
I've just started working with Godot (liking it so far) but I agree that it feels that yield() should be able to return more than the function state. Yield is like returning with the ability to resume() so the function should be able to "return" a value by yielding. One possible approach I have been thinking of is augmenting [GDScript]FunctionState
GDscript already has a yield which takes two values and waits on a signal. However, this one argument version would just return its argument in the FunctionState. With just that change a generator could be written like:
This works quite nicely but there is a subtlety worth considering. When the coroutine function returns (instead of yielding) a value is returned not a FunctionState. In the above example null is returned (because the function is void) which we test to stop the loop in do_stuff(). If the coroutine function returns a value [eg. imagine I think I'll take a look at the code. EDIT: Looking at the code there are complications with the signal version of yield so the section that I'd written below about it is removed for now. May elaborate later. |
There's some problems on how If you are still interested in the idea of adding generators, please open a new proposal on the GIP tracker following the given issue template (after checking that it doesn't exist already). You can reference this issue and summarize the ideas in the proposal. Thanks in advance! |
To create generator functions, we need yield(value) to be implemented. This would act like yield(), but 'value' would be passed back and returned from the resume() call.
This would allow range() to be made into an iterator, rather than creating a potentially huge array.
This would also be a step towards nice features like list/dict comprehension.
The text was updated successfully, but these errors were encountered: