Finite State Machine using Command Pattern?

class EnemyController : MonoBehaviour { IState PatrolState; IState AttackState; StateMachine FSM; void Awake() { //The state has an Action delegate as an argument called Enter, Execute, Exit in order PatrolState = new State(StartWalkingAnim, Patrol, StopWalkingAnim); //an overloaded one with only execute as an argument AttackState = new State(Attack); FSM = new StateMachine(); FSM.SetCurrentState(AttackState); } void Update() { FSM.Update(); } private void StopWalkingAnim() { //stop walking animation } private void StartWalkingAnim() { //stop walking animation } private void Patrol() { //if (player.Onsight) // FSM.ChangeState(Attack) //movement.Patrol(); } private void Attack() { //if (!player.Onsight) // FSM.ChangeState(Idle) //AttackController.Attack(); } } The benefit I could see here is all the functions has a direct access to member variables so no passing of reference is necessary. and also no need for seperate class for each state.…


Link to Full Article: Finite State Machine using Command Pattern?