Check out our latest project ✨ OpenChapter.io: free ebooks the way its meant to be 📖

Godot Coroutines (Unity like)

An asset by Apophis Software
The page banner background of a mountain and forest
Godot Coroutines (Unity like) hero image

Quick Information

0 ratings
Godot Coroutines (Unity like) icon image
Apophis Software
Godot Coroutines (Unity like)

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.)

Supported Engine Version
4.1
Version String
1.0.1
License Version
MIT
Support Level
community
Modified Date
1 year ago
Git URL
Issue URL

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

0 ratings

Your Rating

Headline must be at least 3 characters but not more than 50
Review must be at least 5 characters but not more than 500
Please sign in to add a review

Quick Information

0 ratings
Godot Coroutines (Unity like) icon image
Apophis Software
Godot Coroutines (Unity like)

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.)

Supported Engine Version
4.1
Version String
1.0.1
License Version
MIT
Support Level
community
Modified Date
1 year ago
Git URL
Issue URL

Open Source

Released under the AGPLv3 license

Plug and Play

Browse assets directly from Godot

Community Driven

Created by developers for developers