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!
Thanked by 1critic

Comments

  • Hi @mkShogun96, I am actually busy programming a prototype using a finite state engine (and have made a couple in the past as well) so this explanation will be to help you as well as myself to understand it a bit better :)

    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:
    if (stateRunning == true){
    	sprite = spriteRunning;
    } else if (stateStanding == true){
    	sprite = spriteStanding;
    }


    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:
    image

    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.
  • Wow @pieter Thank you so much for the reply. I think that your post is so valuable and it really helped me understand the concept of a state machine. Now that I think of it, it is a pretty neat way of organizing your logic and code (as you mentioned above). Thanks for the diagrams and everything. Good luck with your finite state machine. I look forward to implementing my own state machine in the future :D. Again thank you for taking the time to write this reply is really helped me a lot!
  • A very important part of using state machines is that there should only be one thing that controls the actual state-change of the object. This helps keep things organised and coherent: If your state machine changes a state, you only have to go to the state machine if you want to change how that works or to add behaviors.

    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!
  • From a practical point of view, the weak point in any State Machine implementation no matter how well designed it is, is the state switching. From my experience it's very useful to design your state switching in such a way to allow the triggers to be checked multiple times and still function as intended. I usually assume that a if statement can fail due to some magical circumstances and code in such a way that it wont be called only once.

    Nice to see a fellow BSc student, a lot of theory falls away after some time, very nice to have the knowledge though. ;)
    Thanked by 1mkShogun96
  • @dislekcia and @critic thank you for your valuable input. It is so nice to be able to ask a question like this and get difference perspectives and input that help me understand the bigger picture of this concept. Until I haven't really dealt with State Machines and now I think that it is a really powerful tool to use in future development both in CS and Game Development. Thank you for the help and information. :D
    Thanked by 1critic
  • I remember that unity did quite a cool state machine tutorial not to long aga. Check for it on their site (learn tab, then live training tab). Helped me a lot.
    Thanked by 2Sash mkShogun96
Sign In or Register to comment.