Most of the beginners on the site are teenagers enrolled in secondary school. Often they pick up concepts here and there over the Internet when they think it will help, but don't take the time to learn topics that don't seem interesting.
State machines. Bah! Who needs them, right?
Games need them.
Let's get the boring part over with...
The Computer Science Part
Finite State Machines, FSMs, or Finite State Automata, are an abstract machine. The machine is made up of multiple states, also called a node or vertex. The machine transitions to a new state by a trigger or event or condition. The transitions are also called edges. One node is designated as the start node. Notes may be marked as exit nodes. The machine enters at the start node, transitions to new states, and eventually ends on an exit node.
So what does that mean?
We can explain the same state machine in many different ways. It is usually easier to understand a state machine when it is put into a pretty picture. It is usually easier to modify, codify, and program state machines when they are in tables. So we'll do both.
Let's start with this very simple state machine:
Note that there are 2 entries for state 2. That is because it has two transitions.
Graphically it looks like this:
But we don't have to use boring names like 0, 1, 2, and 3 for state names, just like we don't have to stick with x and y for variable names.
Let's redraw the graphs with better names:
Suddenly it looks much less like a boring theory topic and more like something to do with games.
In fact, it looks like an NPC's game logic.
Implementing a simple state machine
Now we can start out simple implementation.
For the common quick-and-dirty simple state machine that will never change, we can simply hard code the machine into the code. In that case the programmer calls on his favorite little friend, the switch statement:
public class StateMachine{public enum State{Routing,Sentrying,Attacking,Ending}State mState = State.Routing;Random rng = new Random();public string GetStateName(){return mState.ToString();}public string UpdateState(){return "Running state " + GetStateName() +". Your game logic goes here.";}public void NextState(){switch (mState){case State.Routing:mState = State.Sentrying;break;case State.Sentrying:mState = State.Attacking;break;case State.Attacking:// TODO: Make this based on game logic instead of random name generatorif (rng.NextDouble() < 0.75){Console.WriteLine("Random generator says NPC has survived.");mState = State.Routing;}else{Console.WriteLine("Random generator says NPC did not survive.");mState = State.Ending;}break;case State.Ending:// Nothing to do.break;}}public bool IsDone(){return mState == State.Ending;}}
"But wait", you might exclaim, "That doesn't look like a big ugly table or a fancy graph!" That's right. I said above that you can have many different ways to represent a state machine, and this is one of them.
Always Leave Them Wanting More
This sums up the first lesson. You know what a state machine is in an abstract way. You saw that they might potentially have some uses in games. And you hopefully want to know more.
Stop by next time for the second lesson about extending the basic state machine.
Source code is attached. You can download it to get a sneak peak at what is coming up.
I am loving these articles