-
Notifications
You must be signed in to change notification settings - Fork 0
Coroutine queue
The library offers an API to make sure the coroutines/actions 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 or action immediately after its queued and will only advance when the coroutine or the action is finished*.
Note: Coroutines and actions don't have separate queues.
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);
protected void QueueActions(string id, params Action[] actions);
protected void QueueActions(params Action[] actions);| Parameter name | Default value | Description |
|---|---|---|
| id | DefaultID | The ID of the queue |
| SplitYields | false |
Indicates wether the coroutine should be splitted by its yields. More detailed explanation below |
| routines | - | The coroutines to queue |
| actions | - | The actions to run |
Note: To queue multiple coroutines or actions 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);
}
}When the SplitYields boolean is 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:

