This is a plugin project that adds a simple implimentation of coroutines in C# to any Flax Engine project.
Enjoy this plugin? Here is a link to donate on ko-fi.
To add this plugin project to your game, follow the instructions in the Flax Engine documentation for adding a plugin project automatically using git or manually.
- Install the plugin into the Flax Engine game project.
- Create a method with
IEnumeratoras a return type. In the method useyield returnfollowed by the desired yield instruction. Example:yield return new WaitForSeconds(5)to wait 5 seconds. - Use the
SimpleCoroutinestatic class to start and stop coroutines.
All of these example are ran from a Script.
This example shows a simple way to wait 5 seconds in a coroutine method.
// Starting the coroutine.
void override OnStart()
{
SimpleCoroutine.StartCoroutine(MyCoroutine(), this);
}
// Coroutine Method
IEnumerator MyCoroutine()
{
yield return new WaitForSeconds(5);
Debug.Log("This is called 5 seconds later.");
}This plugin contains a simple way to execute a method after a certain amount of time using the Invoke method. Here is an example of how to use it.
void override OnStart()
{
SimpleCoroutine.Invoke(MethodCalledIn5Seconds, 5, this);
}
void MethodCalledIn5Seconds()
{
Debug.Log("This is called 5 seconds later.");
}This example waits until a specific member variable boolean is true before continuing to execute.
private bool ThisNeedsToBeTrueToContinue = false;
// Starting the coroutine.
void override OnStart()
{
SimpleCoroutine.StartCoroutine(MyCoroutine(), this);
}
// Coroutine Method
IEnumerator MyCoroutine()
{
yield return new WaitUntil(() => ThisNeedsToBeTrueToContinue);
Debug.Log("This is called after ThisNeedsToBeTrueToContinue boolean is set to true.");
}SimpleCoroutine- a static class used to interact with the coroutine system.
WaitForSeconds- waits a defined number of seconds before continuing.WaiForFrames- waits a defined number of frames before continuing.WaitForNextFrame- similar toyield return nulland waits to continue until the next frame.WaitUntil- waits until the passed in Func returns true before continuing.WaitWhile- waits until the passed in Func returns false before continuing.