​Player State Machine / State Pattern:


This helped me maintain the player's different states inside the game. 

So basically my character has 3 states as of now and there are as follows.

  • Moving State: During this state, the character automatically moves toward its current direction.
  • Changing Direction State: The player will switch to this state, whenever the player tries to change the direction according to the input given, then once the direction change finishes it will switch back to "Moving State".
  • Player Stop State: This state works when we want to stop the player.

Why State Machine?

  • Each state's code is self-contained.
  • Allows us to modify the behavior Independently.
  • Changes to one state will not impact other states.
  • We can Implement the state pattern with a Finite State Machine.

Finite State Machine:

  • There are a limited number of states in a Finite State Machine. Only limited to the number of states we program.
  • The player can be in one state at a time.

How to Create:

For creating a State Machine we have to follow 3 components.

  1. Context
  2. Abstract State
  3. Concrete Stat

To know more about these components check the diagram attached.

Basic Code Example: PlayerStateMachineBasicCodeExample

Leave a comment

Log in with itch.io to leave a comment.