GMS 2 Basics about state machines


I having a hard time figuring out what are the main elements of a state machine in a general scope. So far I know that there are 3 main elements:

1. the switch in the step event that is supposed to verify on which state the object is at each moment, then it activates the script accordly to the state.

2. The state scripts, that probably run the commands for that state and need to have a way to escape from that state to some other state(maybe normal state?).

3. A script that can be run on step event that tells on which state the object is at each moment by verifying the actual conditions of the object(not sure about this script).

Is there something more that I should know?


I would say that that is both more and less than a state machine is. It is more than a state machine is because in its most basic form is an abstract machine that can be in exactly one of a finite number of states at any given time. It doesn't need to be a switch statement in a step event. It could be composed of multiple objects, where each state is an object, it could be composed of a timer, and so on.

But if you are talking about the general GM main player character state machine then I would say there's probably a couple of other things you would want. In general my player's state machine is composed of a series of states where each state is defined in general by what will happen when the state starts, what the state does, what will end the state, and what happens when the state ends. But this can be different for every state.

Also state machines don't need to be scripts. Other common versions are switch statements and, slightly less common, object - where each state is a separate instance of an object.


A state machine is simply a method of making sure only a particular set of code is run each step without relying on countless conditionals. There is no right or wrong way to go about it, technically.