Quick Question on State Machine

Hello, all!
I have just set up a finite state machine for my player, and I would like to know this:

Is it better to run all functions of the Player (for instance, the movement script) inside of the Finite State Machine?

Or is it best practice to keep the State Machine in its own script, and have other scripts attached to the player that reference it to get the state?

Thanks- YA!

I’d recommend encapsulating it in its own script:

  1. It is important enough to be encapsulated in a separate file, since it is responsible for the player state, and this is non trivial.
  2. Readers of the code including yourself will see “PlayerFiniteStateMachine” and be reminded of its responsibility.
  3. Your Player file will be smaller and more encapsulated. Left unchecked, classes like Player can grow very bloated over time. The more code you have in a class file, the more you have to read/scroll/consider to get at what you want.
  4. Pulling out the FiniteStateMachine into a separate class/file makes it easier to run diff tools on the file, allows other programmers to work on it in parallel with less source control conflicts.
  5. It also decreases the amount of errors per file - if you have state errors, you can fix these in isolation, without thinking about other errors you may have in Player.
  6. This will allow you to follow the Single Responsibility Principle