Create a stack driven coroutine based state machine for unity

by B0rn2C0de   Last Updated September 05, 2016 08:05 AM

I am working on a game where i am using a co routine based state machine in unity . I am using it because it allows me create interactive scripts . I wanted to know how would i go about making a stack based machine , so that i can overlay game states such as when i want to pause the game to show a menu Here is the code i am using for a state

public abstract class State {

[Inject]
MonoBehaviour gameObject;

[Inject]
public EventSystem eventSystem;

StateMachine parent;

public State(StateMachine p)
{
    parent = p;
}

public abstract IEnumerator Enter();

public abstract IEnumerator Exit();

public abstract IEnumerator Update();

public IEnumerator Run()
{
    yield return gameObject.StartCoroutine(Enter());
    while(parent.CurrentState.GetType() == GetType())
    {
        yield return gameObject.StartCoroutine (Update ());
    }
    yield return gameObject.StartCoroutine (Exit());
}
}

State machine run function

 while(Current!=null)
        {
            yield return Component.StartCoroutine(Current.run());
        }

the problem with this approach is as soon as the state machine's current state changes the loop will exit and the coroutine will exit , plus the state machine cannot start another coroutine unless the previous one finishes , how do i alter this design to make it work with as a state stack



Related Questions



Sequencing method calls with time delay

Updated January 31, 2018 13:13 PM