How to setup a STATE MACHINE?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Jan Gabriel

I’ve been looking for how to make a state machine. I see a lot different kinds of state machines and I don’t understand how to set them up or use them.

I wanna use a state machine that uses script not animation.

:bust_in_silhouette: Reply From: SnapCracklins

This link by David E Pesce has some great notes on implementation, but in script i see many doing it with an Enum and then calling its constants in code when state changes.


Enum myState {STATE_1, STATE_2, STATE_3}
var playerState = myState.STATE_1

And then set some bool variables to check for when to shift to the next state.

 var myFlag_1 = false
 var myFlag_2 = false

And when those states change, progress the state.

Func my_state_changer():
    If myFlag_1 == true:
        playerState = myState.STATE_2
    If myFlag_2 == true:
       playerState = myState.STATE_3

Then run a function that checks the state and updates the object depending on what state you choose.
Also fun - if you put this little state machine in a singleton or separate object, you can call it whenever you need any event check. Then its just a matter of plugging events into whatever states.