Pawn State Control


1 Framework Design


In the game, a character has different states, such as walk, sprint, vault, jump, attack and so on. While developing the logic of a character, developers need to control these states to promise the character display correctly. The character states have different mutex relationships. For example, the character cannont enter sprint state while scoping or attacking. In this chapter, we indicate how to control these states appropriately.

The key design idea of our state control is given below.


  • Use a state list to record all character states which is replicated. We use an array to record for blueprint-only project and you can use a bitmap for c++ project.

  • Maintain a state mutex config asset to determine the different states mutex relationship.

  • Use the same function to change the character states. The states should be changed at the authority side.


2 Development Flow


2.1 Add New State


Notice that the developer can add a new state type to the following blueprint enum:

  • /Game/USGT/Framework/Character/State/EN_CharacterState.

newstate


2.2 State Mutex Config


Setup the state mutex config from the character blueprint, as following.

mutexstateconfig

  • MutexStates: Cannot enter target state if pawn is in already these config states. For example (from above picture), character can not enter jump state if pawn is crouch or prone state.

  • LeaveStatesIfFail: Leave these states if enter target state failed. For example, if charater enters jump state failed, the character will leave crouch or prone state so as to make the character standing up.

  • LeaveStatesIfSuccess: Leave these states if enter target state successful.


2.3 State Modification


Use following function to control state modification.

  • CanEnterState: Whether the character can enter target state.

  • IsInState: Whether the character is in target state now.

  • EnterState: Enter target state and return enter state result.

  • LeaveState: Leave target state.

Notice that character states should be changed at the authority side, especially for the multiplayer version.


2.4 State Response


While adding a new state, you need to add logic to response it after the state change. For example, ADS state can break the sprint state so that you need to change the move speed after entering ADS state.

There are two ways to do this.

  • Add logic into the ProcessOnStateChanged function of BP_Character_USG.

  • Bind event of OnNotifyStateChanged of BP_Character_USG.

The example is given below.

onstatechanged

Setup move speed from the character blueprint.

pawnsetup


3 Build-in Character States


  • Idle

  • Walk

  • Sprint

  • Jump

  • Crouch

  • ADS

  • HoldWeapon

  • Fire


4 API


Click here to see API details.