Understanding the State Machine
Hello Everyone
I am a newbie Game Developer that has a vacation break at the moment. I plan on using my time effectively to teach myself Game Development Skills so that I can make myself marketable to the Game Development Industry. (I am currently Studying Bsc Computer Science with the aspiration to complete my Honors degree by 2018).
Anyways I am reading a very nice Unity Tutorial book at the moment learning some basics of C# Programming in Unity (since Game Programming is a bit different) but I have stumbled upon the concept of a State Machine. My Question is if anyone can elaborate what exactly a State Machine is, What it is used for and how important it is when making your game. From the book I can see that It is a really powerful tool and concept to use in game development (might even be a big cornerstone) but I would like to hear from you guys.
Hope everyone is doing well and good luck with all the prototypes and projects!
I am a newbie Game Developer that has a vacation break at the moment. I plan on using my time effectively to teach myself Game Development Skills so that I can make myself marketable to the Game Development Industry. (I am currently Studying Bsc Computer Science with the aspiration to complete my Honors degree by 2018).
Anyways I am reading a very nice Unity Tutorial book at the moment learning some basics of C# Programming in Unity (since Game Programming is a bit different) but I have stumbled upon the concept of a State Machine. My Question is if anyone can elaborate what exactly a State Machine is, What it is used for and how important it is when making your game. From the book I can see that It is a really powerful tool and concept to use in game development (might even be a big cornerstone) but I would like to hear from you guys.
Hope everyone is doing well and good luck with all the prototypes and projects!
Thanked by 1critic
Comments
A finite state engine takes all the states that an object can be in (eg. Player) and breaks them into discreet states (eg. Standing, Jumping, Walking, Attacking etc.). The idea is to keep each state independent from the other so tweaking one doesn't affect the other states (however you can still have them influence each other if you want).
This helps simplify a variety of things in your game:
1) Animation
By knowing which state your object is in, you can easily just apply the sprite (or skeletal animation) that is associated with that state. So:
2) Cleaner Code
Using states also helps keep your code clean. The object (and code in it) for the Standing State does not need to know anything about the Jumping State. All the Standing State needs to know is, if you press the jump button, it will cancel into the Jumping State and does not care what happens after that. Similarly the Jumping State only knows that if it hits the ground, it will cancel into the Standing State.
It also makes it easy if you want to later add intermediate states to the object like for instance a Landing State. You then simply pop it in between the Jumping and Standing state and you're done.
Jumping State -> Standing State
becomes
Jumping State -> Landing State -> Standing State
Here's an example of the flowchart of part of a State Engine:
3) AI
Finite State Engines are also very useful when designing AI (Though more complex AI would typically use something like binary search trees, or behaviour trees).
So let's look at a very simple enemy. It has states standingAI, pursuingAI and attackAI.
standingAI: If the player comes within range it changes it's state to pursuingAI.
pursuingAI: Moves towards the player. If next to player -> attackAI. If the player moves out of range -> standingAI.
attackAI: Attack the player.
Although I gave the example of using states for players and enemies, they can be applied to other systems as well such as Menus, Sound, Dialogue Trees etc.
You are pretty much using a state engine every time you use an "if" statement as you're checking the state of a variable to execute a specific block of code for that condition. It is a way of thinking about and structuring your code. How you implement it is entirely up to you.
Hope this has shined some light on the topic for you ;)
Give this article a read as well for another perspective.
Compare that to just having an "isJumping" boolean variable on your player object. If that's accessible anywhere in your game code, then anything could change that value to be either true or false at a whim. If anything else needs to be done when that "state" changes, then it would be up to the code all over your game to manage that - and if anything changed, you can bet that some of it won't be properly updated. Without a machine that can only be in one state at a time, you might even get cases where "isJumping" and "isStanding" are true at the same time, then everything gets confused!
Nice to see a fellow BSc student, a lot of theory falls away after some time, very nice to have the knowledge though. ;)