Skip to content

Coroutine queue

Qkrisi edited this page Feb 21, 2021 · 10 revisions

Coroutine queue

The library offers an API to make sure the coroutines don't run at the same time when there are multiple instances of the module.

You can run multiple coroutine queues simultaneously using queue IDs.

A queue will start its first coroutine immediately after its queued and will only advance when the coroutine is finished*.


protected const string DefaultID;

This constant holds the ID of the default queue.


protected void QueueRoutines(string id, bool SplitYields, params IEnumerator[] routines);
protected void QueueRoutines(string id, params IEnumerator[] routines);
protected void QueueRoutines(bool SplitYields, params IEnumerator[] routines);
protected void QueueRoutines(params IEnumerator[] routines);

Declaring method: Awake

Parameter name Default value Description
id DefaultID The ID of the queue
SplitYields false Indicates whether the coroutine should be splitted by its yields. More detailed explanation below
routines - The coroutines to enqueue

ArgumentException: The specified ID is either null, or only has whitespaces.

Note: To queue multiple coroutines at the same time, seperate them by spaces (for ex. QueueRoutines(Coroutine1(), Coroutine2()))

Example:

Default coroutine queue:

protected override void Start()
{
    base.Start();
    GetComponent<KMBombModule>().OnActivate += () => {
        QueueRoutines(PlayAudio())   //Now the start sounds won't play at the same time
    };
}

IEnumerator PlayAudio()
{
    for(int i = 0; i<5; i++){
        string SoundName = "Sound"+i.ToString();
        GetComponent<KMAudio>().PlaySoundAtTransform(SoundName, transform);
        yield return new WaitForSeconds(.5f);
    }
}

Queues with IDs:

protected override void Start()
{
    base.Start();
    GetComponent<KMBombModule>().OnActivate += () => {
        QueueRoutines("Audio", PlayAudio());
        QueueRoutines("Animation", PlayAnimation());
        //Now there won't be more than one audio or animation playing, but there can be audio and animation playing at the same time.
    };
}

IEnumerator PlayAudio()
{
    for(int i = 0; i<5; i++){
        string SoundName = "Sound"+i.ToString();
        GetComponent<KMAudio>().PlaySoundAtTransform(SoundName, transform);
        yield return new WaitForSeconds(.5f);
    }
}

IEnumerator PlayAnimation()
{
    for(int i = 0; i<10; i++){
        transform.position*=.1f;
        yield return new WaitForSeconds(.5f);
    }
}

protected class SkipRequeue

public SkipRequeue(object value);

When using SplitYields with the value of true, yielding an instance of this class will skip the reque and won't skip to the next coroutine in the queue.

value is the object the coroutine will yield.

Example:

IEnumerator ShowButton()
{
    foreach(var Button in Buttons)
    {
        Button.SetActive(true);
        yield return new SkipRequeue(new WaitForSeconds(3f));   //The next part won't be requeued
        Button.SetActive(false);
        yield return new WaitForSeconds(5f);     //The next button show will be requeued
    }
}

*SplitYields

If the SplitYields argument is set to true, when the queue gets to the coroutine, only its first part will run (the code before the first yield), then the yield gets executed (for ex. WaitForSeconds), and then the next part will be put at the end of the queue.

This cycle continues for every yield of the coroutine.

Representation of false SplitYields:

Representation of true SplitYields:

Clone this wiki locally