Implementing state pattern through player and playerController

I am developing a platform game and I already know the state pattern in general and I am inspired from the Game programming pattern from Robert Nystrom, here is the sample :

class HeroineState   {  
        virtual ~HeroineState() {}  
        virtual void handleInput(Heroine& heroine, Input input) {}  
        virtual void update(Heroine& heroine) {}  }; 

and :

 class DuckingState : public HeroineState
     DuckingState()   : chargeTime_(0)   {}    
     virtual void handleInput(Heroine& heroine, Input input)
         if (input == RELEASE_DOWN)    
         // Change to standing state...      
      virtual void update(Heroine& heroine) {    
           if (chargeTime_ > MAX_CHARGE)    
  private:   int chargeTime_;

The thing is I do not understand how I could link this handleInput function into my implementation which is basically binding input in player controller (through setupInputComponent) and delegating the action to the pawn.

Thanks to anyone who would spend time on this problem.

In our state machine, each player state gets a pointer to a character it is controlling when pushed onto the state stack. The call order where detecting player input is like this:

PlayerController->LeftMouseReleased(): fire the LeftMouseReleased() event on the top state in the state stack
PlayerState->LeftMouseReleased(): fire an event on the character currently being controlled
Character->FireWeapon(): shoot gun

I think I understand this point, I binded gamepad A button to PlayerController::Jump() so I think I have to “reroute” the binded function to a concrete state ?
Because one Player function can be called from multiple state like PlayerController::Jump() could be called from IdleState or RunningState.

Anyway thank you for the answer :slight_smile:

If someone has more info about this … :slight_smile:


Typically you can make a stack of PlayerStates and the current “active” state is the one on top.

And how can I push a state on the top of the stack ?
The problem is that I cannot make the connections between the pattern and unreal API.
Mainly because the input queue cannot be accessed directly, it is binded to unique function…

I don’t wanna be “that guy”, but I’m confused about what the benefits are of programming a system like this when Unreal already has a perfectly good input system?

My problem is that I have a player who need to have certain behavior given the input it is receiving and the objects it is colliding with. It can roll, jump and 2 kind of dash, but it cannot do certain action if it is in given condition like it cannot jump while in air and also if he is touching given colliders.
So I can code this without using state pattern but with a ton of if then else instead so that was why I looked to this pattern and how implement it with unreal’s Api and architecture.

I use my Fsm system for every character/controller I build currently:

And the only thing I allow input events to do is try to set a given state or evaluate a rule that results in state changing, that’s all;

With delegates subscription I let the FSM decide by itself if the given input would result on a Jump or it’s ignored because the character is crouching, climbing, etc…
So for my setup all a controller input does is “request a state change”; the Fsm rules decides if that request will be executed or not, always with all the logic required to perform that action request self-contained within the State’s Begin(), Update() or Exit() functions.

That is really interesting, I think I will look more precisely on your tool more than trying hardly to make mine :slight_smile: