Open Daylily-Zeleen opened 7 months ago
This is exactly what I was looking for. I hope it will be implemented some day, especially co_await functionality
@Nikitf777 Please don't bump issues without contributing significant new information. Use the :+1: reaction button on the first post instead.
Describe the project you are working on
Using GDExtension c++ binding in my own project.
I have implemented a class named "Future" to realize, this proposal just to describe my solution and try to make it become a core feature for crossing languages async mechanism.
Because if we use C++ with GDScript to implement game logic, we are likely to have a need for mutual waiting, unless we avoid this situation deliberately.
I'm not sure if it's just my personal need, I want some feedback to decide whether to add this feature into the godot source code or not.
Describe the problem or limitation you are having in your project
I need to binding c++ function which calls a async method from GDScript and wait for its completion, then return a result to GDScript. Obviously, I need to wait this bound method in GDScript, too.
In c++, I can get a
Object *
which class name is "GDScriptFunctionState" from an async method in GDScript, and wait its signal "completed" ( but "GDScriptFunctionState" is not a valid class in c++ binding of GDExtension).In GDScript, there have not a elegent way to await a c++ bound methods.
Describe the feature / enhancement and how it helps to overcome the problem or limitation
Like
GDScriptionFunctionState
, we can implements a class "Future( may be we have a more appropriate name to name it)" which represents an async task, and hold a "completed" signal to return a result when its task completed.So we can pass this object cross language and connect its "completed" signal to await an async task.
How to do some thing async this depend on specific language self, it is not a question here. In other words, "Future" is an interface/promise of an async task.
Now we can do something like this:
Or use "GDScriptFunctionState" to simplify GDScript code:
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
This feature can be realized by just implementing "Future":
Optionally, we can do some special operation to make
GDScriptFunctionState
to be contained in "extension_apis.json" when dumping extension apis. So we can realize awaitng an async GDScript method with typed safe in other languages. ( We still not need to expose "GDScriptFunctionState" neither editor or GDScript.)Here have some addtional features:
C++20 coroutine:
I know godot is using c++17, so this is mainly used for c++ binding of GDExtension.
If we allow c++20, we can implement a specialized template of Ref and
SignalAwaiter
to make all thing elegentNow we can do this:
We can use "GDScriptFunctionState" to get rid of Future in GDScript, too ( refer to previos section).
Typed Future
Like typed Array, we can implemente a "TypedFuture< ResultType >" inheirt from Future or just make "Future" as a tempalte( Future< ResultType >). Then make GDScript can await a "Future/TypedFuture" wihout ".completed", and make its returen value typed can be detected automatically like typed Array ( I don't know the detail of this mechanism).
If this enhancement will not be used often, can it be worked around with a few lines of script?
The core concept of this proposal is very simple, users can define a simple "Future" class like my solution by themselves.
Is there a reason why this should be core and not an add-on in the asset library?
This feature is about GDScript and any binding of GDExtension , it is best to be supported by core.