Looking for suggestions on a state machine system to organize player character code

Does anyone have recommendations on a state machine system to implement a more complex character system? Looking at the Lyra project and other examples out there, it doesn’t look like it’s a popular thing to do for Unreal.

I’m not sure if it’s because of performance or network replication reasons or whatever. All references to a state machine in Unreal generally just refer to the AI behavior trees and animation graphs.

How would you go about implementing complex behavior like having different and nuanced behavior based on state, movement type, etc.? I can see using a similar pattern to implement complex game mode logic as well.

I 100% agree with you that distributing any extensive AI logic into Anim Graphs and Behavior Trees is a recipe for pain (IMHO :wink: ). With pure C++ code you can diff any check-in and see exactly what logic changed without even opening the Editor or Visual Studio.

We do our NPC characters logic 100% in C++ and just use the blueprints to fire animations, from C++ events. At a high level the main stuff we did was:

  • create state machines with standard functions like OnEnter(), OnExit(), Tick(), etc. functions
  • major events like MovementComplete(), TargetLost(), AnimationComplete(), etc.
  • define Movement states that are mutually exclusive to all others, e.g. Idle, MoveToLocation, FollowPath, FollowLeader, CircleEnemy, etc.
  • define Logic states that are mutually exclusive to all others, e.g. Idle, LookingForTargets, AttackingTarget, FleeingTarget, etc.
  • define Behaviors that can happen in any state, and potentially stack, for example, playing animations, focusing a particular location or object, using an ability, etc.

So for example, your NPC would initially be Idle Movement, LookingForTargets Logic, and playing an Idle Animation, occasionally saying some Idle Dialogue.

Then when a target is detected your NPC would go into ApproachEnemy Movement, AttackTarget logic, while focusing the enemy, firing a weapon, using abilities, and firing off animations.

2 Likes