Skip to content

Instantly share code, notes, and snippets.

@almyu
Created October 10, 2019 02:51
Show Gist options
  • Save almyu/8f6e3e7bf0c6d4d1e798b7c7c7fb59c7 to your computer and use it in GitHub Desktop.
Save almyu/8f6e3e7bf0c6d4d1e798b7c7c7fb59c7 to your computer and use it in GitHub Desktop.
Coroutine action for NPBehave
using NPBehave;
using System.Collections;
public class CoroutineAction : Task
{
System.Func<IEnumerator> starter;
IEnumerator coro;
public CoroutineAction(System.Func<IEnumerator> starter) : base("CoroutineAction") {
this.starter = starter;
this.coro = null;
}
protected override void DoStart() {
coro = starter();
if (!coro.MoveNext()) Stopped(true);
else if (coro.Current == null) RootNode.Clock.AddUpdateObserver(OnUpdate);
else Stopped(false);
}
void OnUpdate() {
if (!coro.MoveNext()) {
RootNode.Clock.RemoveUpdateObserver(OnUpdate);
Stopped(true);
}
else if (coro.Current != null) {
RootNode.Clock.RemoveUpdateObserver(OnUpdate);
Stopped(false);
}
}
protected override void DoStop() {
coro = null;
RootNode.Clock.RemoveUpdateObserver(OnUpdate);
Stopped(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment