Custom NPC Behavior: Teaching a Villager to Flee
Custom NPC Behavior: Teaching a Villager to Flee
So far we've driven NPCs from the outside — a device commanding a spawner. Now we go inside the character itself. An npc_behavior is a Verse class that rides on top of an NPC's existing brain, gets a handle to its own body, and steers it directly. This is how you build medics, shopkeepers, bosses, fleeing villagers — anything with a personality.
device vs. npc_behavior
<!-- section-art:device-vs-npc-behavior -->

Device Trigger
A creative_device is a thing in the world that commands other things. An npc_behavior is the NPC's mind:
creative_device |
npc_behavior |
|
|---|---|---|
| Lives where | placed in the level | attached to a Character Definition |
| Gets its character how | @editable slots |
GetAgent[] / GetFortCharacter[] |
| Entry point | OnBegin<override>()<suspends> |
OnBegin<override>()<suspends> |
| Imports | /Fortnite.com/Devices |
/Fortnite.com/AI + /Fortnite.com/Characters |
You attach the behavior to an NPC Character Definition in the editor, then OnBegin runs the instant that character starts simulating.

The behavior toolkit: navigatable + focus
Once you have your fort_character, two interfaces do the heavy lifting:
navigatable(viaGetNavigatable[]) —NavigateTo(target),SetMovementSpeedMultiplier,StopNavigation,Wait. Targets come fromMakeNavigationTarget(agent_or_position).focus_interface(viaGetFocusInterface[]) —MaintainFocus(agent_or_location)keeps the character looking at something. It never finishes on its own, so youspawn{}it.
Both interfaces are epic_internal — you can't implement them — but their getters and methods are all public, so you call them freely.
A villager that flees when hurt
<!-- section-art:a-villager-that-flees-when-hurt -->

Fleeing Villager
This behavior stands quietly until something damages the villager — then it sprints directly away from the attacker while keeping its eyes locked on the threat:
using { /Fortnite.com/AI }
using { /Fortnite.com/Characters }
using { /Fortnite.com/Game }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/SpatialMath }
using { /UnrealEngine.com/Temporary/Diagnostics }
# CUSTOM NPC BEHAVIOR — teach a villager to flee when hurt.
#
# An npc_behavior is a Verse class that rides ON TOP of an NPC's built-in brain.
# You attach it to an NPC Character Definition; OnBegin runs the moment the
# character is simulating. Unlike a creative_device, an npc_behavior gets a
# handle to its OWN character through GetAgent[] / GetFortCharacter[], then drives
# it with the navigatable and focus interfaces.
#
# This villager stands still until something damages them — then it sprints away
# from the attacker and keeps its eyes locked on the threat the whole time.
fleeing_villager := class(npc_behavior):
# How far to run when spooked (centimeters).
FleeDistance : float = 1200.0
# OnBegin runs when the NPC enters the simulation. Wire up its damaged event.
OnBegin<override>()<suspends> : void =
if:
Agent := GetAgent[]
Character := Agent.GetFortCharacter[]
then:
Character.DamagedEvent().Subscribe(OnDamaged)
Print("Villager is on patrol duty... nervously.")
# When hit, run directly away from the instigator and stare them down.
OnDamaged(Result : damage_result) : void =
if:
Agent := GetAgent[]
Character := Agent.GetFortCharacter[]
Navigatable := Character.GetNavigatable[]
Focus := Character.GetFocusInterface[]
Attacker := agent[Result.Instigator?]
AttackerChar := Attacker.GetFortCharacter[]
then:
MyPos := Character.GetTransform().Translation
ThreatPos := AttackerChar.GetTransform().Translation
# Direction pointing FROM the threat TO us, normalized, then pushed out.
Away := (MyPos - ThreatPos)
FleePoint := MyPos + Away * FleeDistance
Target := MakeNavigationTarget(FleePoint)
Print("Villager spooked — fleeing!")
# Keep looking at the attacker while we run (focus never completes
# on its own, so spawn it as its own fiber).
spawn{ Focus.MaintainFocus(Attacker) }
spawn{ RunAway(Navigatable, Target) }
# Sprint to the flee point; report whether we made it.
RunAway(Navigatable : navigatable, Target : navigation_target)<suspends> : void =
Navigatable.SetMovementSpeedMultiplier(2.0)
Result := Navigatable.NavigateTo(Target, ?MovementType := movement_type.Running)
if (Result = navigation_result.Reached):
Print("Villager reached safety.")
else:
Print("Villager's escape was blocked!")
The moving parts:
fleeing_villager := class(npc_behavior)— note the parent. There is nocreative_devicehere; this class is the NPC.GetAgent[]thenGetFortCharacter[]— failable accessors (square brackets) that hand us our own agent and its character. We chain them inside anif:/then:block so everything is bound before we use it.DamagedEvent()— from thedamageableinterface every NPC implements. Subscribing tells us the instant the villager takes a hit. It hands us adamage_result, whoseInstigator?we cast back to anagentwithagent[...].- The flee vector —
MyPos - ThreatPospoints from the threat toward us; pushing out along it lands a flee point behind the villager.MakeNavigationTarget(FleePoint)turns that position into somethingNavigateTounderstands. - Two
spawn{}blocks —MaintainFocusnever returns, and we don't want it to block the flee. Running each in its own fiber lets the villager run away while still staring at the attacker.
This behavior is compile-verified in UEFN 5.8 (the project builds clean as a standalone Verse class). Create an NPC Behavior file, paste it in, and assign it as the Verse behavior on a Custom-type NPC Character Definition.
Recap
- An
npc_behavioris a Verse class that is the NPC's mind — attached to a Character Definition, it gets its own body viaGetAgent[]/GetFortCharacter[]. - Steer the character with the
navigatableinterface (NavigateTo,SetMovementSpeedMultiplier) andfocus_interface(MaintainFocus). MakeNavigationTargetturns an agent or avector3into a navigation target.MaintainFocusnever completes, sospawn{}it; that's also how you let an NPC move and look at once.- Subscribe to the character's
DamagedEvent()to react to being hit.
Next — Part 4: Sidekick Companion — we combine everything into a brand-new feature: a Sidekick pet that follows you and emotes how it feels.
Verse source files
- 03-standalone.verse · standalone
Check your understanding
Test yourself with an interactive quiz and track your progress + earn XP — free for members.
Turn this into a guided course
Add UEFN Verse NPCs — npc_behavior class, GetAgent/GetFortCharacter, navigatable (NavigateTo, SetMovementSpeedMultiplier), focus_interface (MaintainFocus), MakeNavigationTarget, DamagedEvent, spawn{} to your free study plan — we'll suggest related pages and stitch the lot into one compile-checked, self-guided lesson with worked examples and quizzes.
References
Original tutorial generated by Verse Island from the Verse/UEFN knowledge base, with references to the Epic Games sources above. Code is validated against the knowledge base.