Skip to content

Tryibion/FlaxSimpleCoroutines

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Coroutines for the Flax Engine

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.

Installation

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.

Setup

  1. Install the plugin into the Flax Engine game project.
  2. Create a method with IEnumerator as a return type. In the method use yield return followed by the desired yield instruction. Example: yield return new WaitForSeconds(5) to wait 5 seconds.
  3. Use the SimpleCoroutine static class to start and stop coroutines.

Examples

All of these example are ran from a Script.

WaitForSeconds

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.");
}

Simple Invoke

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.");
}

Wait Until

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.");
}

Important classes

  • SimpleCoroutine - a static class used to interact with the coroutine system.

Built in Yield Instructions

  • WaitForSeconds - waits a defined number of seconds before continuing.
  • WaiForFrames - waits a defined number of frames before continuing.
  • WaitForNextFrame - similar to yield return null and 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.

About

A coroutine plugin project for the Flax Engine

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages