Territory & Patrols

Documentation Unreal Engine AI Territory Patrol

Give an enemy posts to guard, a route to walk, and a leash that brings it home when a chase goes too far.


Place SEC Territory Post actors, list them on your enemy, and it guards them, patrols them, and walks back to them when a fight ends.

When to Use This

  • A guard standing at a doorway who returns to it after you run away.
  • A patrol walking a route between three or four posts.
  • An enemy loitering around a courtyard rather than standing still.
  • A boss that fights only inside its arena and refuses to be pulled out.
  • Any enemy that should stop chasing you across the level.
Add nothing and nothing changes. An enemy without a SEC Territory component stands where you place it and chases as far as it likes.

Setup

Two things: posts in the level, and an enemy told which ones are its own.

1. Place the posts

Drag a SEC Territory Post into the level for each spot. One post is a guard station. Several are a patrol route, walked in the order you list them.
Any actor can be a post. Add a SEC Territory Post component to a statue, a brazier or a doorframe already in your level and it becomes one, tuned in that actor's Details panel. An actor with no component works too and reads as a spot to stand on, with no wait and no wander area.
Details
SEC Territory Post (SECTerritoryPostComponent)
Post
Shape
Point
How To Pick
Random
Pass Through
Track Owner Movement
Wander
Wander Radius
400
Arrival
Wait Time
3
Wait Time Variance
1
Face Anchor Rotation On Arrival
Arrival Radius Override
0
Performance
Point Sample Attempts
12
Wander Radius and How To Pick show for the Radius shape only.
Shape decides what ground the post owns. Point is a spot to stand on, and the enemy walks as close to it as the navmesh allows. Radius gives it a patch to drift around inside, picking a reachable spot within Wander Radius on every visit, which reads as loitering rather than standing to attention.
The component carries its own transform, so move it to put the standing spot somewhere other than the actor's pivot. That matters on a large mesh whose pivot sits in the middle of it.
A wander area needs navmesh under it. A post whose area offers nothing reachable falls back to standing on the post itself and logs once.

2. Give the posts to an enemy

Add a SEC Territory component to the enemy pawn, or use AEnemyCharacterBase, which already carries one.
Details
SEC Territory (SECTerritoryComponent)
Territory
Posts
0 Array elements
Post Order
Sequential Loop
Arrival Radius
100
Leash
Leash Radius
2000
Leash Anchor
Home Post
Only Fight Inside Leash
Leash Break Grace Time
2
Disengage Behavior
Return To Post
Route
Patrol Start Delay
1
Patrol Start Variance
1
Walk Retry Interval
1
Walk Retry Limit
3
Performance
Wander Sample Attempts
4
Debug
Debug Overlay Territory
Posts live on this component alone; an empty list means the enemy owns no ground.
Fill in Posts on the component. It sits on the pawn you placed, so the route is edited next to the enemy that walks it, and a level designer never has to open a controller.

3. Pick who drives it

The component holds the route and answers questions about it. Something else decides when to walk. You have three choices, and they do the same job.
DeciderSet it up by
SEC Brain (default)Nothing. An AI Config with no StateTree runs the native loop, which walks the route on its own.
StateTreePoint the AI Config at a StateTree and use the territory nodes. See StateTree Integration.
Your own BlueprintAbout fourteen nodes and no Event Tick. See the recipe below.
Press Play. The enemy waits a moment, walks to its nearest post, stands there for that post's Wait Time, and moves on.

The Leash

Leash Radius on the territory component says how far the enemy may be dragged off its ground before it gives up and heads home. Leash Anchor says what it is measured from: the post the route is on, or the middle of the whole route for a long patrol.
The leash is never smaller than the widest wander area plus the arrival radius, so an enemy standing anywhere in its own patch never reads as out of bounds. Set Leash Radius to 0 for an enemy that chases forever.
Leash Radius is how far it chases. Wander Radius on a post is how far it drifts while idle. They are different distances and the enemy uses both.

What the enemy does about it

These sit on the territory component beside the leash they describe, so a placed enemy is tuned where it stands.
Only Fight Inside Leash makes an enemy ignore a target beyond its ring and engage the moment one steps inside. It will not go and look at one either. Turn it off for an enemy that charges anything it notices and is dragged home by the leash instead.
Leash Break Grace Time lets it follow you a step past the line and carry on if you come back, rather than turning on the spot like a tripwire. When the window does run out, On Leash Broken fires once and the enemy heads home.
Disengage Behavior decides what a finished fight leaves behind:
ValueResult
Return To PostWalks back to the post the route was last on and picks the round up from there. The choice for a guard who owns a place.
Hold PositionTakes the spot it is standing on as its ground and stays. Its posts stay set but stop being its ground, so it neither walks back nor patrols until something points the route at a post again.
Two stay on the brain, because they describe what it does on the walk home rather than what the ground is.
Details
SEC Brain (SECBrainComponent)
SEC|Brain
Territory
Disengage Focus Policy
Keep Target
Block Actions While Disengaging
Territory category on the brain. What it does while walking home, as opposed to the ground itself, which the Territory component on the pawn describes.

Tune

AdvancedRoute order and pacing
Post Order decides which post comes next, and is ignored when there is only one:
ValueUse
Sequential LoopFirst to last, then back to the first. The ordinary patrol.
Sequential Ping PongFirst to last, then last to first. Use it for a corridor where walking through a wall to reach post one would look wrong.
RandomA random post each time, never the one just left. Use it for an enemy that should not be readable.
Wait Time and Wait Time Variance on each post set how long the enemy stands there. Pass Through skips the wait entirely, which shapes a route around a corner without adding a pause.
Patrol Start Delay and Patrol Start Variance on the territory component hold the enemy where it is before a round begins. A row of guards placed together with the defaults will not set off on the same frame.
Face Anchor Rotation On Arrival turns the enemy to face the way the post points once it arrives. Rotate the post component to set that direction, which can differ from the actor's own rotation when the post sits at an offset.
How To Pick decides which spot inside the wander radius the enemy walks to, and shows only for the Radius shape:
ValueResult
RandomA different spot each time, anywhere inside the radius. The ordinary choice for an enemy loitering with nothing to guard.
NearestThe near edge of the radius, so an enemy arriving from the east settles on the east side instead of walking on to the middle. Use it for a patch several enemies approach from different directions.
FarthestThe deepest part of the radius away from the enemy, so it backs off rather than settles where it stands. Use it for a spot an enemy falls back to.
SpreadA different spot each time, favouring ones well away from where the enemy stands, so a wide patch gets walked instead of paced in one corner.
AdvancedArrival and unreachable ground
Arrival Radius on the component says how close counts as reached. A single post can override it with Arrival Radius Override, for one in a crowded spot the enemy cannot stand exactly on.
A walk counts as arriving when navigation reports it reached the spot, or when the enemy is standing inside the arrival radius anyway. A route that only reached part of the way satisfies neither, so it reads as a walk that found no route. When that happens the enemy waits Walk Retry Interval and tries again, up to Walk Retry Limit times, then stands where it is and logs once. Both sit on the territory component.
Wander Sample Attempts is how many times a wander area may fail to offer a reachable spot before the enemy treats that post as somewhere to stand for the rest of the visit. It gets a fresh set of attempts on the next visit.
AdvancedWhat the enemy does with the target while it walks home
Disengage Focus Policy picks between two feels:
ValueResult
Keep TargetKeeps facing and remembering the target, so it turns back the moment it sees you again. Costs nothing.
Forget TargetStops facing the target and leaves combat coordination, then rejoins on arrival. Use it when an enemy walking home while staring at you looks wrong. Its combat role is re-picked on arrival, so it may come back as a different role.
Block Actions While Disengaging stops the enemy attacking on the way home, so it does not swing at nothing. Turn it off for one that should keep throwing ranged attacks as it withdraws.
AlternativeDrive it from your own Blueprint
The component holds the route order, the wander sampling and the arrival maths, so a graph of your own never reimplements them. It decides two things: when to walk, and how long to wait.
  1. Event Possessed → cache the Territory and Movement Evaluator components.
  2. Set Route To Nearest Post.
  3. Delay for your opening pause.
  4. Pick Destination For Current PostMake SEC Return OptionsBegin Return To.
  5. Bind Event to On Return Complete → a custom event.
  6. On that event, Did Last Return Reach Destination on the Movement Evaluator → Branch. True: Get Wait Time HereDelayAdvance Route → back to step 4. False: Delay, then back to step 4 without advancing.
That is the whole loop, with no Event Tick and no variables of your own.
Pick Destination For Current Post hands out a fresh spot inside a wander area on every call, so ask it once per leg and keep the answer. To judge a leg, read the walk's own report; Has Arrived At Destination and Has Arrived At Location answer a different question, which is where the enemy is standing right now.
Useful readings alongside those: Get Next Post, Get Home Post, Get Nearest Post, Get Live Posts, Is Current Post Pass Through, Owns Ground, Is Outside Leash, Is Target Outside Leash, Get Distance From Leash Anchor, Get Effective Leash Radius, and Set Ground Here to make the spot underfoot the new anchor without cancelling anything.
AdvancedReading it at runtime
The brain reports what it is doing through Get Brain Phase, which reads Idle, Patrol, Investigate or Combat. Bind On Brain Phase Changed to drive an animation set from it.
Alongside it: Is Walking, Get Seconds Outside Leash, and Is Refusing Target. The last one separates two cases that look identical from outside: an enemy standing on its ground with nothing to fight, and one standing there ignoring a target beyond its leash.
Turn on the overlay with SEC.Debug.Territory 1, plus Debug Overlay Territory on the component for a single enemy. It draws the leash ring, every post with its wander area, a line to the spot being walked to, and a line of text saying whether the enemy is walking, standing, or refusing a target. Selecting a post on its own draws its arrival perimeter and wander radius; one no enemy lists draws in a muted colour, which is how a post nobody points at gives itself away.
C++C++ surface
// Route, on USECTerritoryComponent. Nothing here moves the pawn.
FVector  PickDestinationForCurrentPost();          // fresh wander spot each call
AActor*  AdvanceRoute();                           // step the cursor under Post Order
AActor*  SetRouteToNearestPost();
AActor*  SetRouteToHomePost();
bool     SetGroundHere();                          // take the spot underfoot as the anchor
float    GetWaitTimeHere() const;
bool     IsCurrentPostPassThrough() const;
bool     HasArrivedAtDestination() const;
bool     HasArrivedAtLocation(const FVector& Location) const;
 
// Leash readings.
FVector  GetLeashAnchorLocation() const;
float    GetEffectiveLeashRadius() const;
bool     IsOutsideLeash() const;                   // measures the pawn
bool     IsTargetOutsideLeash(AActor* Target) const;  // measures a target
bool     OwnsGround() const;
USECTerritoryPostComponent holds what a post is tuned by, and USECTerritoryPostComponent::FindPostComponent(Actor) resolves it from any actor. Override PickPointInArea in a Blueprint subclass of the component for a post that works its destination out at runtime; the base picks among reachable spots inside the area under How To Pick.
USECTerritoryComponent::GetPostLocationFor(Post) and GetWanderExtentFor(Post) answer for any actor, whether it carries a post component or not.
USECTerritoryComponent::FindTerritoryComponent(Controller) resolves the component from a controller, checking its possessed pawn first.
Everything runs on the server.

Integration

SystemLink
Awareness SystemDecides what the enemy knows. Territory decides where it belongs. Together they give patrol, investigate and return.
StateTree IntegrationTerritory Patrol, Territory Investigate, Hold Out Of Combat, Territory Owns Ground, Pawn Outside Leash, Target Outside Leash.
Movement SystemWalks the pawn to each spot. Target-relative movement stands aside while a walk runs.
Combat RolesAn enemy walking home under Forget Target leaves coordination, then rejoins and takes a fresh role.