State Machines and Me

Fighting games, a menacing thing for players. A nightmarish entanglement of code for programmers. Introducing the "new-fangled" Finite State Machine!!!!

But really, for the past week or so, I've been working on a simple fighting game alongside a friend and this game requires a rather comprehensive characterController for the level that I'm at. After about a week, I got sick of looking at my spaghetti code, and decided that I wanted to look for a much better way of managing the many states that a fighting game character can iterate through in a fighting game. This is where Finite State Machines come in! Now, finite state machines aren't new to me, but if you aren't sure what they are:

  Finite State Machine Diagram  (Nystrom, B. 2014)

Finite State Machine Diagram (Nystrom, B. 2014)

In my own words, an explanation of what a finite state machine would be: If you think of water, for example. Water has three different 'states' that is can appear in, these states are Gaseous, Liquid and Solid. Water can only be in one state at any one time, and certain parameters must be met before the water can move between these states. This is, in essence, what a finite state machine is, just with more states that an object can be in. For example, fighting characters will have many states that they can be in, these include jumping, falling, attacking, attack recoil, blocking, block stun, stunned, etc. Any on character can only be in any one of these states at any one time, and can behave differently, just like water, depending on the state they they are in.

enum State { STATE_STANDING, STATE_JUMPING, STATE_DUCKING, STATE_DIVING };

Using Enumerators to Declare the states that this character can be in (Nystrom, B. 2014)

I've utilised the finite state machine in the character controller that I've made, and I'm much better off for it. using finite state machines makes it much easier to moderate what character can do what and what time, without juggling millions of bools to make sure that they can do it. I've noticed how badly held together my code is, so I'm trying to make strides towards better coding practices, making sure that I use finite state machines where necessary is on of the easier steps forward that I could take, but a step forward regardless.

 

Tylah Kapa