State Machine Hell

Okay so, I came across a nice little fix for my menu and start game problem in my coding. The problem is that I got errors coming from every orifice mostly the top half. This is the template I had:

And Unity was loving it for the first few bits. Any tips would help and other than the StateMachine I got it all word for word.

class StateMachine {
    Map<String, IState> mStates = new Map<String, IState>();
    IState mCurrentState = EmptyState;
    StateMachine gGameMode = new StateMachine();

    gGameMode.Add("mainmenu", new MainMenuState(gGameMode));
    gGameMode.Add("openingscrean", new OpeningScreanState(gGameMode));
    gGameMode.Add("localmap", new LocalMap(gGameMode));

    gGameMode.Change("openingscrean");

    public void Update(float elapsedTime) {
        mCurrentState.Update(elapsedTime);
    }
    public void Update() {
        float elapsedTime = GetElapsedFrameTime();
        gGameMode.Update(elapsedTime);
        gGameMode.Render();
    }
    public void Render() {
        mCurrentState.Render();
    }
    public void Change(String stateName) {
        mCurrentState.OnExit();
        mCurrentState = mStates[stateName];
        mCurrentState.OnEnter();
    }
    public void Add(String name, IState state) {
        mStates[name] = state;
    }
}

As it turns out I could have just looked at that tutorial. Look at the top…

So that code you typed in is “Psudo JS” otherwise known as BS!

I’d find a different tutorial to go off if I were you and if possible try one that’s C# unless you already know a fair bit JS. C# is faster in unity and if you’re learning a new language anyway C# will be better as the unity tutorials are in that.

EDIT - got a bit of time to look and THIS looks pretty good if you’re trying a 2D RPG style game.

Hi,

Finite State Machine (FSM) can quickly become a hell to manage as the number of states is growing. Because the number of transitions grows quadratically and any modification to the state machine requires to fiddle with the transitions, which is tedious and error prone.

If you are looking for an alternative to FSM, have a look at Behaviour Tree, which is far more flexible and easier to manage.

I’m the author of Panda BT (www.pandabehaviour.com), it’s a script based behaviour tree engine.

If you have a question about using this tool, you’re welcome on this thread.