Install Asset
Install via Godot
To maintain one source of truth, Godot Asset Library is just a mirror of the old asset library so you can download directly on Godot via the integrated asset library browser

Quick Information

Creating a Unity-Like Coroutine Manager / Coroutine class to perform CoRoutines in the Godot Engine.Requires Godot 4.0+ C# edition.(Updated to address found issues.)
Godot-Coroutines
Creating a Unity-Like Coroutine Manager / Coroutine class to perform CoRoutines in the Godot Engine.
Requires Godot 4.0+ C# edition.
Usage:
Add the Coroutine Manager (tscn file) to the active scene. Then you can put something like the following into your scripts. Caution: DO NOT Unload the Coroutine Manager on Scene change!!! Doing so will arbitrarily stop the manager, and all running coroutines!!!
If you are unsure, then look for a tutorial on how to put things into the always loaded section of Godot. (Such as... https://www.youtube.com/watch?v=oAkdii5B918)
private Coroutine MyCoroutineReturn;
...
// Stop the running Coroutine on scene removal. Kinda like Unity's OnDisable() call back.
public override void _ExitTree() {
if (MyCoroutineReturn != null){
CoroutineManager.Instance.StopCoroutine(MyCoroutineReturn);
MyCoroutineReturn = null;
}
base._ExitTree();
}
...
// Call the Coroutine:
MyCoroutineReturn = CoroutineManager.Instance.StartCoroutine(MyCoroutine());
// Or..
MyCoroutineReturn = CoroutineManager.Instance.StartCoroutine(MyTimedCoroutine());
...
// The actual Coroutine Function - Using WaitOneFrame...
private IEnumerator MyCoroutine() {
WaitOneFrame WaitAFrame = new WaitOneFrame();
While (true){
GD.Print("Coroutine 'MyCoroutine' was fired.");
yield return WaitAFrame; // yield one frame...
}
...
}
// The actual Coroutine Function - Using WaitForSeconds...
private IEnumerator MyTimedCoroutine() {
WaitForSeconds WaitThreeSeconds = new WaitForSeconds(3.0f);
While (true){
GD.Print("Coroutine 'MyTimedCoroutine' was fired.");
yield return WaitThreeSeconds; // yield for 3 seconds...
}
...
}
Cleanup Function Usage.
If you want to perform Cleanup, such as file closing / flushing, etc., when your coroutine stops, you can make a function for the Coroutine Manager to call on ending the Coroutine. This applies to Stop(coroutine), StopAllCoroutines(), or a natural finish. To do so, you pass the function in the Coroutine Start call.
// Call the Coroutine:
MyCoroutineReturn = CoroutineManager.Instance.StartCoroutine(MyCoroutine(), CoroutineCleanup());
...
void CoroutineCleanup(){
// Do stuff here.
}
This will automatically be called, and the cleanup will occur. Most coroutines are supposed to be in-memory operations, so something like iterating across a list, or the like. But, if you are using it to load up a list/dictionary from a file, say for grabbing the game's settings, then use the call with cleanup function, to close out your files, and to do any memory cleanup that you need to do to prevent things like open file handles and memory leaks.
If you have any questions, please use the Issue system here.
Credits:
Credit goes out to PeakyCoroutines ( @Brian-McElroy Brian-McElroy). One for showing me that this could be done, and Two for motivating me to make my own, to handle the missing features that I needed. "Borrowed" their WaitOneFrame
and WaitForSeconds
classes that I modified to work with my own system.
See also the Credits.md file for Attribution per the CC-BY-SA 4.0 license.
Creating a Unity-Like Coroutine Manager / Coroutine class to perform CoRoutines in the Godot Engine.
Requires Godot 4.0+ C# edition.
(Updated to address found issues.)
Reviews
Quick Information

Creating a Unity-Like Coroutine Manager / Coroutine class to perform CoRoutines in the Godot Engine.Requires Godot 4.0+ C# edition.(Updated to address found issues.)