Skip to main content
Tweeted twitter.com/StackGameDev/status/1104079553037778947
removed redundancy and tag from the title.
Link
Vaillancourt
  • 16.4k
  • 17
  • 56
  • 61

How to write a custom reusable WaitUntil that is reusable - Unity?

Source Link

How to write custom reusable WaitUntil that is reusable - Unity

In my turn based game, I like to have a function similar to Unity's WaitForSeconds, WaitUntil that can be called within a IEnumerator that suspends the coroutine execution for the given amount of moves. I already have a public working UnityEvent (moveEvent) which I could listen to.

So I could call it from any IEnumerator like:

IEnumerator Example() { yield return new WaitForMoves(2); } 

I think I'd need a separate class like:

class WaitForMoves { int moves = 0; void Start() { Game.moveEvent.AddListener(OnMoveComplete); } void OnMoveComplete() { moves++; } // I'm not sure how to write this in a way that it waits // until this.moves === movesToWait public WaitForMoves(int movesToWait) { } }