-
Notifications
You must be signed in to change notification settings - Fork 610
Coroutines #2982
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
base: master
Are you sure you want to change the base?
Coroutines #2982
Conversation
c40ad58
to
dcad850
Compare
ba67544
to
fce485a
Compare
1bc0af0
to
e07c384
Compare
414cd8a
to
cc261c6
Compare
While working on the manual, I realized the name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for late review. I've got some feedback. I can do some changes while merging.
Source/Engine/Level/Scene/Scene.h
Outdated
@@ -9,6 +9,7 @@ | |||
#include "SceneRendering.h" | |||
#include "SceneTicking.h" | |||
#include "SceneNavigation.h" | |||
#include "Engine/Scripting/Coroutines/CoroutineExecutor.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use forward decl instead includes (compile time issue).
Source/Engine/Scripting/Script.h
Outdated
#include "ScriptingObject.h" | ||
#include "ScriptingObjectReference.h" | ||
#include "Coroutines/CoroutineHandle.h" | ||
#include "Coroutines/CoroutineSequence.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use forward decl instead includes (compile time issue).
/// This method uses shared per-scene executor. The order of execution is not guaranteed. | ||
/// </remarks> | ||
API_FUNCTION() | ||
ScriptingObjectReference<CoroutineHandle> ExecuteCoroutineOnce(ScriptingObjectReference<CoroutineSequence> sequence) const; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can use CoroutineSequence*
raw pointer. Caller owns or knows the object lifetime and this actually moves the ownership into the executor.
/// Adds a coroutine to the executor to be executed multiple times. | ||
/// </summary> | ||
API_FUNCTION() | ||
ScriptingObjectReference<CoroutineHandle> ExecuteRepeats( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename to ExecuteRepeated
to match ExecuteLooped
/// <summary> | ||
/// Returns the number of coroutines currently being executed. | ||
/// </summary> | ||
API_FUNCTION() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
API_PROPERTY
/// <summary> | ||
/// Reference to a coroutine that can be used to control its execution. | ||
/// </summary> | ||
API_CLASS(Sealed) class FLAXENGINE_API CoroutineHandle final : public ScriptingObject |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be a struct for more lightweight and prevent dynamic memory allocations.
/// Stores code to be executed once previous step is completed. | ||
/// </summary> | ||
/// <remarks> | ||
/// This class is used due to limitations of Flax API. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If function takes Function<void()>
as parameter then it will be passed as C# delegate to be invoked from C++. So this CoroutineRunnable
is not needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last time I tried it didn't work. Gonna try it again.
private: | ||
void Clear(); | ||
void EmplaceCopy(const Step& other); | ||
void EmplaceMove(Step&& other) noexcept; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think memory management for such structure is overenginnering. It can be data-only (maybe with frames delta, seconds delay and suspension point as union). Only CoroutineSequence
and CoroutineExecutor
use it.
ScriptingObjectReference<CoroutineSequence> ThenRunFunc(const Function<void()>& runnable); | ||
|
||
/// <summary> QOL wrapper for waiting until predicate in native scripts. </summary> | ||
ScriptingObjectReference<CoroutineSequence> ThenWaitUntilFunc(const Function<void(bool&)>& predicate); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can be Function<bool()>
and API_FUNCTION
so C# can pass Action
directly.
execution.ContinueCoroutine(CoroutineSuspendPoint::Update, Delta{ 0.0f, 0 }); | ||
_executions.Add(MoveTemp(execution)); | ||
|
||
ScriptingObjectReference<CoroutineHandle> handle = NewObject<CoroutineHandle>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CoroutineHandle
as struct would not leak memory and be faster to use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I remember I thought API structs must not have ScriptableObjectReference
. If it is supported, then I can change it.
Previously `ExecuteRepeated`
Observations:
This quickly suggest homebrew Flax-specific solution. Unfortunately, Flax API limits require using chain of methods to set up stages of the coroutine. Thus, they are not as sexy as Unity enumerator method-based coroutines, but share the same API in both managed and native scripts. CRs are welcome!