[UE5] StateTree 101: Organize a Guard's Behavior with States and Transitions

Created: 2026-07-23Last updated: 2026-09-06

Learn UE5's StateTree through three states: patrolling, chasing, and searching. Try spotting and losing the player with key input and see Tasks, Transitions, passing data into conditions, and choosing between it and Behavior Trees.

"While patrolling, chase the player on sight. When you lose them, wait a moment, and chase again on spotting them." That is short to write in prose, but adding conditions in Blueprint can obscure which logic is running now.

StateTree is a mechanism for organizing that "current state" and "cue to move on". It works for enemy AI and also for doors and quest progression.

Switching between patrolling, chasing, and searching on the cues of spotting, losing, and five seconds elapsing

What You'll Learn

  • That StateTree and Behavior Tree differ in "where you read"
  • Separating a state's contents (Tasks) from the switching cues (Transitions)
  • Trying three states — patrol, chase, and search — with key input
  • Where data lives and how it reaches conditions

Here we build a guard model where keys toggle "spotted" and "lost". First confirm state changes through on-screen text, then organize how to connect perception and movement.

  • What State, Task, and Transition each handle
  • How to pass an Actor's value into a state-switching condition
  • Building interruption of a five-second wait when re-spotted while searching
  • Which of StateTree and Behavior Tree reads better for your spec

Sponsored

You read a different place than in a Behavior Tree

In a Behavior Tree, priorities such as "chase if there is a target, otherwise patrol" are expressed through branch ordering.

In a StateTree, you read the state switching as the center: "spotting while patrolling goes to chasing", "losing them while chasing goes to searching".

Behavior Tree read through prioritized branches versus StateTree read through the conditions leaving the current state
What you want to seeBehavior TreeStateTree
Which behavior takes priorityRead the Selector's branches and conditionsRead the state selection conditions and transitions
What happens when logic finishesRead the next Task in the SequenceRead the on-completion Transition
Whether it can switch partwaySet via a Decorator's Observer AbortsSet via a Transition leaving the state

UE's Behavior Trees are event-driven , switching logic on cues such as Blackboard changes. It is not a split of "a BT that examines the whole tree every frame" versus "a StateTree that runs only when needed". Focus here on which structure reads better for the same spec.

If you have split states with Enum and Switch state machines, thinking of StateTree as building that per-state logic and switching in a dedicated editor makes it easier to approach.

A state's contents and the switching cues

Start with these four.

Tasks inside a state box, with entry conditions and Transitions to the next state
ElementMeaningExample here
StateWhat it is doing nowPatrol, Chase, Search
TaskThe logic performed in that stateDisplay text, wait five seconds
ConditionExamines values and decides whether it holdsWhether PlayerVisible is true
TransitionWhen and to which state it movesChase to Search on losing the player

A transition is a state switching. A Transition sets the "Trigger" for when to check, the destination "Transition To", and "Conditions" if needed.

Conditions have placements too. Enter Conditions check whether it can enter that state. A Transition's Conditions check whether it moves to another state. Writing entry conditions does not automatically exit a running state.

Task ordering differs too. Tasks placed in the same State are not a queue. They run in parallel, such as displaying text while counting time. Our Search uses that to combine "searching display" with "a five-second wait".

Start by displaying "Patrol"

Use a project such as Third Person where you can Play and look around the level. Being able to add variables and key input in Blueprint is enough.

1. Prepare the plugins and the Actor that runs it

Search for StateTree in "Edit → Plugins", enable StateTree and GameplayStateTree , and restart.

Next create a Blueprint BP_GuardStateDemo with Actor as the parent. Add one Static Mesh Cube so its position is visible and place it near the player. That is the guard we observe states on.

Add a Boolean variable PlayerVisible with a default of false and Instance Editable on. A Boolean is the two values true and false, representing "is the player visible" here. Compile and save.

2. Match the Schema and Component

Right-click in the Content Browser and create ST_GuardStateDemo via "Artificial Intelligence → StateTree". Choose StateTreeComponentSchema for the Schema.

A Schema decides where that StateTree is used and what data it can receive. We use the combination that runs on a plain Actor.

Matching a plain Actor and a StateTree Component with a StateTreeComponentSchema asset
  1. Open ST_GuardStateDemo and set the Schema's "Context Actor Class" to BP_GuardStateDemo .
  2. Add a StateTree Component to BP_GuardStateDemo.
  3. Specify ST_GuardStateDemo in the component's "State Tree".
  4. Turn "Start Logic Automatically" on where that version shows it.

Context is the data the StateTree receives as what it runs on. Our Actor is the BP_GuardStateDemo placed in the level. Specifying the Class lets you select that Actor's PlayerVisible from conditions later.

StateTree AI Component is for AI Controllers. It differs from the plain StateTree Component we use here in both Schema and combination.

3. Create one Patrol

Right-click ST_GuardStateDemo's Root and add Patrol via "Add Child State". Root is the tree's entry and Patrol is its child.

Select Patrol and add a Debug Text Task under "Tasks" in the Details panel. Set "Text" to Patrol , Text Color to something readable, and Font Scale to a legible size. That Task exists to confirm the running state as text.

If Patrol's "Transitions" already has entries, delete them at this stage. Leave Enter Conditions empty. We use one state and keep the display running.

Placing a Debug Text Task on Root's child Patrol and confirming the Patrol display on the level's Cube

Compile and save the StateTree and Blueprint, then Play. Patrol appearing near the Cube confirms not only that the asset exists but that it runs on the placed Actor .

If nothing appears, check the asset assignment, Schema, Context Actor Class, and compile results. If the Debug Text Task has an Actor field, choose the Context Actor. This is a development-time display; a finished game's UI is built separately.

Sponsored

Hands-On: try spotting, losing, and re-spotting

Now we grow to three states. Each press of N toggles PlayerVisible between true and false.

StateWhat it displays hereThe cue that ends it
PatrolPatrolSpotting goes to Chase
ChaseChaseLosing them goes to Search
SearchSearchFive seconds returns to Patrol. Re-spotting goes to Chase

We confirm the judgment changes of "patrolling" and "chasing" as text. Making the Cube walk is considered later in "Extending to a moving enemy".

1. Toggle "visible" with the N key

Set "Auto Receive Input" to Player 0 in BP_GuardStateDemo's "Class Defaults". That lets this Actor receive key input during Play. Place only one experiment Actor in the level.

Build these connections in the Event Graph.

  1. Place the N event and wire a white exec line from "Pressed" into Set PlayerVisible .
  2. Create NOT Boolean from PlayerVisible's Get.
  3. Connect NOT's output into Set PlayerVisible's value input.

NOT flips true and false. Starting at false, one press makes it true and another makes it false.

Flipping PlayerVisible's current value with NOT and saving it on N Pressed

What we change here is only the fact of "visible". Rather than specifying Patrol or Chase directly from the N key, we let the StateTree's conditions decide.

2. Add Chase and Search

Add Chase and Search under Root. Line up Patrol, Chase, and Search as children at the same depth. Do not put Chase inside Patrol.

StateTasks
PatrolDebug Text Task: Text = Patrol
ChaseDebug Text Task: Text = Chase
SearchDebug Text Task: Text = Search, Delay Task: Duration = 5.0

Search's Delay Task completes after five seconds. Set any time-variance field to 0 and turn off any infinite-wait option. All three States have empty Enter Conditions here.

The diagram below spreads the hierarchy horizontally for readability. In the editor's list, Patrol, Chase, and Search sitting at the same depth under Root is what matters.

Three States under Root, with Search running display and a five-second wait in parallel

3. Bind PlayerVisible into transition conditions

Add one entry to Patrol's "Transitions" and configure it like this.

ItemValue
TriggerOn Tick
Transition ToChase
ConditionsBool Compare
Bool Compare's LeftBind → Actor → PlayerVisible
Bool Compare's Righttrue (on)
InvertOff

Binding specifies where a value comes from. It says "read this Actor's PlayerVisible into Left". Typing true into the left side instead compares against true regardless of the player's input, so watch for that.

Binding the Actor's PlayerVisible into Bool Compare's Left and comparing against true on the Right

On Tick is the cue to check conditions on the StateTree's update. Here, becoming true moves from Patrol to Chase. It does not mean unconditionally re-entering Chase on every update.

Build the remaining transitions the same way. Left in every comparison is the Actor's PlayerVisible.

FromTriggerConditionTo
PatrolOn TickPlayerVisible = trueChase
ChaseOn TickPlayerVisible = falseSearch
SearchOn TickPlayerVisible = trueChase
SearchOn State CompletednonePatrol

Rows comparing against false have Right off. Invert is off on all of them. Delete any pre-existing transitions such as returning to Root and keep only the four in the table.

Order Search's two with the re-spot transition to Chase above and the completion transition to Patrol below . Where Priority is specifiable, set re-spotting to High and completion to Normal so re-spotting wins when both hold.

Patrol to Chase on spotting, to Search on losing, and Search branching between re-spotting and five-second completion

4. Spot them again during the five-second wait

Compile, save, Play, click the game screen, and press N.

ActionDisplay change
Right after PlayPatrol
Press N onceChase
Press againSearch
Wait five secondsPatrol
Press N during SearchChase without waiting out the five seconds
Press N again to lose themEnters Search and waits a fresh five seconds

The five seconds are counted from entering Search . Re-spotting and moving to another State exits Search's Task too. Entering Search next time starts the Delay Task waiting anew.

Interrupting the wait on re-spotting mid-search, and counting a fresh five seconds the next time they are lost

We put the display and the Delay in the same State to "wait five seconds while displaying Search". It is not serial logic where "the Delay starts after the text display finishes".

When it does not switch properly

SymptomWhere to check
Pressing N changes nothingWhether input reaches the game screen, Auto Receive Input, the white line from Pressed
It is Chase right after PlayPlayerVisible's default, whether Bool Compare's Left became a fixed value
You cannot pick the Actor's variable in BindContext Actor Class, the variable's exposure settings, recompiling the Blueprint and StateTree
It cannot leave ChaseWhether Chase → Search compares against false
Search returns immediatelyThe Delay's Duration, whether unnecessary completion Tasks or transitions were added
Search never endsThe Delay's infinite setting, On State Completed's destination
It reaches an unexpected stateWhether all three are at the same depth and Patrol comes first in initial selection

The StateTree Debugger lets you select a running instance and see which State became active and which transitions were taken. Confirm one loop through the text display first, then follow the change history.

Sponsored

Where data lives and how it is passed

Our PlayerVisible is a variable on BP_GuardStateDemo rather than inside the StateTree. The N key changes that value and the StateTree reads it through the Context Actor.

Parameters exist too, but separating by a value's role helps more than thinking of them as the same box as a Blackboard.

DataExample use
ParametersSettings passed to this guard, such as wait time and speed
ContextThe Actor or AI Controller it runs on, and that target's values
Task and Evaluator outputsA destination chosen by logic, gathered surroundings, and so on

An Evaluator prepares information for conditions and Tasks. The Actor already had the value we needed here, so we bound directly without adding a custom Evaluator.

Parameters in the diagram illustrate passing settings from outside. We type the wait time directly into the Delay Task, so no Parameters are needed.

Input changing the Actor's variable, with the StateTree reading it through Context and Binding

Creating same-named variables in separate States does not automatically make them the same note. Decide "where the value is changed" and "which source the condition reads" before binding.

Extending to a moving enemy

With state changes understood, you can move the spotting input to AI Perception and the in-state logic to movement and animation.

To drive it from an AI Controller, combine StateTree AI Component with StateTreeAIComponentSchema . It is not a matter of assigning our Actor-oriented asset to a component with a different Schema. Match the AI Context and configure Bindings for perception results and the move target.

Our modelExtending to a moving guard
N flipping PlayerVisibleUpdate from sight spotting and losing
Debug Text of the state namePrepare Tasks for movement, waiting, animation, and more
Waiting five seconds in SearchCreate a state that waits five seconds after moving to the investigation point
A Cube ActorPrepare a Character driven by an AI Controller

Movement also needs NavMesh and AI Controller setup. When trying the Behavior Tree version too, separate the experiment Controllers so Run Behavior Tree and StateTree do not both issue movement orders to the same enemy.

"Move, then wait" means splitting the states

Placing Move To and Delay in one State starts movement and waiting simultaneously. Five seconds finishing while walking to the destination is not an investigation after arriving.

So put "move" and "wait" under a parent State "investigate". Movement success moves to waiting and waiting's completion returns to patrolling. Also decide where movement failure goes.

The difference between lining up movement and waiting in one state versus splitting child states to run them in order

StateTree has a hierarchy where a parent is active while its child is. A shared transition such as "while investigating, re-spotting returns to chasing whether moving or waiting" can be gathered on the parent.

That is an example structure for extending toward the AI Perception article's "wait five seconds after arriving at the investigation point". Our model's five seconds count from entering Search after losing the player.

Which to choose

When choosing between StateTree and Behavior Tree, write your enemy's spec out briefly.

How the spec readsThe easier option to try
You want to follow state switching, such as "spotting while idle chases" or "finishing a cast attacks"StateTree
You want to read priorities as branches, such as "healing first, then retreating, then attacking"Behavior Tree
You already have working AI or a project to referenceBuild on that structure
Few states and a short Blueprint reads fineStart with Enum and Switch

Both can express complex behavior. Build a small subject first and judge by whether you can tell where to change something when you add one condition ; that makes it easier to pick the form suiting your game.

Choosing between StateTree, Behavior Tree, an existing structure, and Enum with Switch based on how the spec reads

Bonus: good to know up front

  • Watch where you place transition conditions : StateTree makes "which state you are in" readable, but putting Transitions in the wrong place produces states you cannot leave. Putting shared interruption conditions on the parent keeps it manageable
  • Pass data with bindings : use Parameters and Context and connect Tasks with bindings. Routing through Blueprint variables makes it hard to trace which state reads what
  • Choose by role against Behavior Trees : enemy AI whose judgment you want as a tree suits Behavior Trees, and long-lived logic with clear state transitions suits StateTree. Rather than forcing a replacement, start from things whose states last a long time
  • It is useful beyond AI : it also manages door and gimmick states

Summary

In StateTree, you place Tasks on states and decide the cues to move on with Transitions. Actor information is passed with Bindings, and you separate logic that runs simultaneously from logic that runs in order.

Try changing our guard's Search wait from five seconds to two. It changes how quickly it returns to patrolling while keeping the response to re-spotting. Confirming state changes this way, add movement and perception one at a time.

Further Reading

Unreal Engine Notes in this section98