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;
    }
}

Any chance you could post the errors you're getting?

No problem, I also running 5.3.5. First and foremost "Map String, IState" is coming in as not found. This little guy and his family is just said to be invalid: gGameMode.Add("mainmenu", new MainMenuState(gGameMode)); Should I make him a Method?

2 Answers

2

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.

"arbitrary" probably means: In the physical order they are found in the filesystem.

I think it's worth saying that if the order of execution makes a difference, there's probably a better way of doing things. If the set-up methods of UI elements (or any other GameObjects, really) are inter-dependent in some way, IMO it's better to find a way of having them set each other up, or create a manager object of some sort that sets them up (in their dependent aspects, at least), rather than relying on Start() functions.

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.