Goal
My goal is to use coroutines for handling flow of game state for a multi-player card game.
Problem
I don’t seem to understand the nature of coroutines correctly. In order to model my domain without the trouble of involving MonoBehavior any more than necessary, I opted for a CoroutineRunner class that takes the pain so that the rest of the code doesn’t have to.
Hello, I am working to learn to use coroutines for managing sequences of events. For now, I’m trying to prototype this functionality but I am running into some problems.
I hope that this example is clear enough. I have a Game object that calls Phases that call Events. Again, this is a proof of concept for me to get my head wrapped around programming phases of a turn-based game using coroutines for state.
The problem that I’m facing at the moment is the error: MissingMethodException: The best match for method RunCoroutine has some invalid parameter. Naturally, I can see that the CoroutineMethod type-hint is not the same as IEnumerator. But, I’m unclear as to what the nature of CoroutineMethod actually is. Any insight would be appreciated.
I would also love some insight as to ramifications of using this approach for managing phases of gameplay turns, etc. Any information would be appreciated.
CoroutineRunner
A MonoBehavior class that can be used to run coroutines when not using MonoBehavior derivatives in the domain.
I got this code from this blog post.
public class CoroutineRunner : MonoBehaviour
{
public delegate IEnumerator CoroutineMethod();
private IEnumerator RunCoroutine(CoroutineMethod coroutineMethod)
{
return coroutineMethod();
}
public Coroutine StartCoroutineDelegate(IEnumerator coroutineMethod)
{
return StartCoroutine("RunCoroutine", coroutineMethod);
}
}
Game
This class represents the game itself as a container for phases.
public class Game : MonoBehaviour
{
private CoroutineRunner _runner;
public void Start()
{
Debug.Log("Starting game...");
_runner = gameObject.GetComponent<CoroutineRunner>();
StartCoroutine(Routine());
}
private IEnumerator Routine()
{
var phase = new GameSetupPhase(_runner);
Debug.Log("Beginning new event...");
yield return StartCoroutine(phase.Routine());
Debug.Log("end");
}
}
GameSetupPhase
The represents a phase of the game.
public class GameSetupPhase
{
private readonly CoroutineRunner _runner;
public GameSetupPhase(CoroutineRunner runner)
{
_runner = runner;
}
public IEnumerator Routine()
{
Debug.Log("Defining sequence of phase...");
var e = new InitializeGameStateEventRoutine(_runner);
yield return _runner.StartCoroutineDelegate(e.Routine());
Debug.Log("phase sequence complete...");
}
}
InitializeGameStateEventRoutine
This represents the concept of a low level thing that needs to be done. Players need to draw 3 cards or something like that.
public class InitializeGameStateEventRoutine {
private CoroutineRunner _runner;
public InitializeGameStateEventRoutine(CoroutineRunner runner)
{
_runner = runner;
}
public IEnumerator Routine(CoroutineRunner runner) {
Debug.Log("Initializing game state...");
yield return new WaitForSeconds(5f);
}
}
Ultimately, I’d like to be able to set up the game, get players in, etc. Then, just do something like game.Start();
Then, the game knows to loop through a number of Phases, each time calling phase.Start();
or something like that. Then, each of the phases does the same until it’s all done.