3rd person combat - how to call a PlayerBaseState method from all states

Hi folks,

I’m extending the state machine implementation in 3ps combat for a game I’m working on.

Right now, any “happens in any state” Action subscriptions are all done in PlayerStateMachine.cs, and the methods they trigger are contained in the same class.

I want to try to call something in the PlayerBaseState instead - after all, that’s where most of my shared logic is, and so I’d like to subscribe to something in all states (without going through my oodles of states) and then have that method run. (actually - I just want to read the “r” key being pressed, and go straight to the death method.)

I’m hitting a wall with how to do it - I don’t have any way to reference the actual states from PlayerStateMachine, do I need to subscribe/unsubscribe on every enter/exit from each of my states to do this? I feel like this can’t be right.

For things that are consistent across states, such as OnTakeDamage and OnDie, the StateMachine really is the best place for these, as otherwise you’re going to be adding these subscribe/unsubscribe clauses to every state you create.

There is a way you could do this with PlayerBaseState, by utilizing virtual methods for Enter and Exit

public virtual void Enter()
{
     stateMachine.Health.OnTakeDamage+=HandleTakeDamage;
     stateMachine.Health.OnDie += HandleDie;
}

Then in each state, enter would need to be an override

public virtual void Enter() override
{
    base.Enter(); //call original Enter method
    //rest of Enter
}

Now a disadvantage to this method is that when we create a new state and let it auto create methods, VS Code automatically creates the Enter, Tick, and Exit methods because these are left as abstract methods in PlayerBaseState. Once we’ve given these methods a concrete implementation in PlayerBaseState, you’ll need to override the methods manually within your child states.

As there is a finite set of conditions in which we want to globally force a state, to me it makes more sense to leave it in PlayerStateMachine.