Action Scheduler Documentation

Overview

This asset aims to streamline the process of creating coroutine actions in a straightforward manner. Additionally, it enables seamless communication between distinct scripts by employing notifications to execute actions, with support for parameter sharing.

Features

  • Notify: Send notifications and parameters between scripts.
  • Wait: Execute actions after specific conditions (time, frames, delegates, notifications).
  • Interval: Schedule recurring actions with various triggers.
  • UntilWait: Halt routines prematurely based on custom conditions.
  • Set and get Fields: Share and access values across scripts.

How To Use

Access functions via the singleton to simplify communication and parameter sharing across scripts.

using static LB.ActionScheduler.Singleton;

public class SampleScript : MonoBehaviour
{
    private void Start()
    {
        Scheduler.Notify("test");
    }
}

Alternatively, use inheritance. Note that parameter sharing requires the singleton.

using LB.ActionScheduler;
using static LB.ActionScheduler.Singleton;

public class SampleScript : ActionScheduler
{
    private void Start()
    {
        Notify("test2");
        Scheduler.Wait("test", () => Debug.Log("Hi!"));
    }
}

Notify

Use Notify to send notifications with a string key and optional parameters (max 10).

Notify("test");
Notify("test2", 5, 3.5f, "hi!", new GameObject(), true);

Use DelayNotify or FrameDelayNotify for delayed notifications.

DelayNotify(3, "test");
FrameDelayNotify(3, "test");

Wait for conditions using ConditionNotify:

bool test = false;
ConditionNotify(() => { return test; }, "test");

private bool TestBool() { return test; }
ConditionNotify(TestBool, "test");

Wait

Execute actions after a delay, frames, or notifications:

Wait(3, () => Debug.Log("Hi!"));
FrameWait(200, () => Debug.Log("Hi!"));
DelayNotify(3, "test");
Wait("test", () => Debug.Log("Hi!"));

Use lambdas to receive parameters or wait for conditions:

DelayNotify(3, "test", "Jimmy", 80);
Wait("test", (name, health) =>
{
    Debug.Log("Player: " + name);
    Debug.Log("Health: " + health);
});

Wait(() => test, () => Debug.Log("Hi!"));

Interval

Schedule recurring actions based on time, frames, or custom triggers.

Interval(1, () => Debug.Log("Hi!"));
FrameInterval(200, () => Debug.Log("Hi!"));

Control intervals dynamically:

int count = 0;
Interval(1, () =>
{
    if (count == 3) return false;

    Debug.Log(count);
    count++;

    return true;
});

IEnumerators

This asset also provides functions similar to the ones previously mentioned, allowing you to use coroutines as awaits.

private IEnumerator Sample()
{
    yield return Wait(3);
    Debug.Log("Hi!");

    yield return FrameWait(200);
    Debug.Log("Hi!");

    DelayNotify(3, "test");

    yield return Wait("test");
    Debug.Log("Hi!");

    DelayNotify(3, "level_end");

    yield return Wait("death", "level_end");
    Debug.Log("Hi!");

    test = false;
    Wait(3, () => test = true);

    yield return Wait(TestBool);
    Debug.Log("Hi!");
}

You can also perform multiple waits where the coroutine will continue once any of the conditions are fulfilled:

private IEnumerator Sample()
{
    DelayNotify(3, "test");

    yield return Wait(Wait(5), Wait("test"));
    Debug.Log("Hi!");
}

To retrieve parameters from notifications, use the Parameters class with WaitParameters. This class provides a Wait method for awaiting and a Values array to access parameters.

private IEnumerator Sample()
{
    DelayNotify(3, "test", "Jimmy", 80);

    Parameters parameters = WaitParameters("test");
    yield return parameters.Wait;

    Debug.Log("Player: " + parameters.Values[0]);
    Debug.Log("Health: " + parameters.Values[1]);
}

UntilWait

Use UntilWait to start a coroutine and stop it based on certain conditions.

private void Start()
{
    DelayNotify(2, "test");
    UntilWait(Sample(), Wait("test"));
}

private IEnumerator Sample()
{
    yield return Wait(5);
    Debug.Log("Hi!");
}

SetField

Store an object with a specific key, making it accessible later. If stored in the singleton, it will be accessible across different scripts.

SetField("key0", 90);
SetField("key1", "Hi!");

GetField

Retrieve an object by its key using GetField.

if (GetField("key0") == 90)
{
    Debug.Log(GetField("key1"));
}

HasField

Use HasField to check if an object exists for a given key. This prevents errors when attempting to access non-existent objects.

if (!HasField("test"))
{
    SetField("test", false);
}
Debug.Log(GetField("test"));