gamedev-skills/awesome-gamedev-agent-skills

unreal-behavior-trees

Build NPC AI in Unreal Engine 5 with Behavior Trees and Blackboards: composites (Selector/Sequence), tasks, decorators, services, and running the tree from an AIController.

Vedi sorgente
Documento Skill originale

Contenuto dal repository con titoli, esempi, codice, tabelle, link e immagini preservati.

Unreal Behavior Trees

Author NPC decision-making in UE5 with Behavior Trees driven by a Blackboard: structure the tree with composites, gate branches with decorators, keep state current with services, and run it from an AIController. Targets UE 5.8.

When to use

  • Use when building enemy/NPC AI: creating a BT_/BB_ asset pair, structuring

Selector/Sequence branches, adding decorators (conditions) and services (periodic updates), writing custom BTTask/BTService nodes, or wiring an AIController to run the tree.

  • Use when the project has Behavior Tree (BT_) and Blackboard (BB_) assets and an

AAIController.

When not to use: the concept* of AI (FSM vs BT vs steering, cross-engine) → game-ai. Pure navigation/pathing math is engine navmesh (BT's MoveTo uses it). Simple one-off logic may be cheaper as a small state machine than a full tree.

Core workflow

  1. Create the pair: a Blackboard (BB_) holds typed keys (the AI's memory: TargetActor,

LastKnownLocation, bIsInvestigating); a Behavior Tree (BT_) references that Blackboard.

  1. Possess and run. An AAIController possesses the pawn and calls RunBehaviorTree(BT),

which also initializes the referenced Blackboard.

  1. Structure with composites. Selector runs children left→right until one succeeds

(priority/fallback: "attack, else chase, else patrol"). Sequence runs children until one fails (do-all: "move to cover → reload → peek"). Simple Parallel runs one main task alongside a secondary.

  1. Gate branches with Decorators that read Blackboard keys (e.g. "Has Target?" guards the

combat branch). Set Observer Aborts so the tree re-evaluates when the key changes.

  1. Keep the Blackboard current with Services attached to a branch — they tick periodically

(e.g. update TargetActor via a sight check) only while that branch is active.

  1. Do work in Tasks, which return Succeeded, Failed, or InProgress (latent tasks like

MoveTo finish later).

  1. Verify with the Behavior Tree debugger during PIE — it highlights the running node and

shows live Blackboard values, so you see exactly which branch executes.

Patterns

1. AIController that runs the tree (C++)

cpp
void AEnemyAIController::OnPossess(APawn* InPawn)
{
    Super::OnPossess(InPawn);
    if (BehaviorTree)                 // UPROPERTY(EditAnywhere) TObjectPtr<UBehaviorTree>
        RunBehaviorTree(BehaviorTree); // initializes & uses the Blackboard the BT references
}

2. A priority tree (node structure)

text
ROOT
└── Selector (try combat, else investigate, else patrol)
    ├── Sequence            [Decorator: Blackboard 'TargetActor' Is Set, Observer Aborts: Both]
    │     ├── Task: MoveTo (TargetActor)          // latent: returns InProgress then Succeeded
    │     └── Task: Attack
    ├── Sequence            [Decorator: 'LastKnownLocation' Is Set]
    │     ├── Task: MoveTo (LastKnownLocation)
    │     └── Task: Wait (3s) + clear key
    └── Task: Patrol (BTTask_FindPatrolPoint -> MoveTo)

Observer Aborts: Both makes the combat branch interrupt patrol the instant TargetActor is set, and bail out when it's cleared — this is what makes the AI feel reactive.

3. Updating the Blackboard from code (e.g. on seeing the player)

cpp
void AEnemyAIController::SetTarget(AActor* Target)
{
    if (UBlackboardComponent* BB = GetBlackboardComponent())
        BB->SetValueAsObject(TEXT("TargetActor"), Target);   // key name must match the BB asset
}
// Clear with BB->ClearValue(TEXT("TargetActor")); to drop back to a lower-priority branch.

Pitfalls

  • AI never starts — the pawn isn't possessed (set the Pawn's Auto Possess AI to "Placed

in World or Spawned" and assign the AIController), or RunBehaviorTree was never called.

  • `MoveTo` instantly fails — no NavMesh in the level (add a Nav Mesh Bounds Volume), or the

target is off the navmesh.

  • Branch doesn't react to changes — the gating Decorator's Observer Aborts is set to

None; set it to Self/Lower Priority/Both so the tree re-evaluates when the key changes.

  • A task hangs the tree — a custom task returned InProgress and never calls

FinishLatentTask. Always complete latent tasks.

  • Blackboard key typosSetValueAsObject("Taget", ...) silently does nothing; match the

key name and type exactly, or use a cached FBlackboardKeySelector.

  • Sequence vs Selector confusion — Sequence = AND (stops on first failure); Selector = OR

(stops on first success). Swapping them inverts the behaviour.

References

  • For a custom C++ `UBTTaskNode` (instant and latent ExecuteTask returning EBTNodeResult,

with a FBlackboardKeySelector), read references/custom-bttask.md.

  • Primary docs: "Behavior Trees in Unreal Engine"

(https://dev.epicgames.com/documentation/en-us/unreal-engine/behavior-trees-in-unreal-engine).

Related skills

  • game-ai — engine-agnostic AI design (FSM, BT, steering, pathfinding choices).
  • unreal-cpp-gameplay — the AIController and pawn classes in C++.
  • fps-shooter / tower-defense — genres that compose enemy AI.
dallo stesso repository

Altri Skills

Tutti gli Skills
gamedev-skills
Community

game-feel

Add "juice" and game feel that makes actions satisfying — screen shake, hit-stop/freeze frames, tweened/eased motion, squash & stretch, knockback, and layered audio-visual feedback — as engine-neutral techniques that pair with the detected engine's tween, particle, and camera APIs. Use when the user mentions game feel, juice, "make it feel good/punchy", screen shake, hit stop, screen freeze, easing, squash and stretch, impact frames, or feedback/polish on hits, jumps, pickups, and deaths.

installazioni
5
GitHub Stars
1,1K
Aggiornato
10 set
gamedev-skills
Community

game-ui-ux

Design and build game UI/UX — HUDs, menus, and overlays — that survive every screen: anchor- based responsive layout, resolution/aspect scaling and safe areas, keyboard/gamepad focus navigation, a screen/menu state stack, and event-driven (not polled) HUD updates. Engine- neutral patterns that pair with the detected engine's UI skill. Use when the user mentions HUD, health bar, main menu, pause menu, settings screen, UI layout, anchors, UI scaling, aspect ratio, safe area, controller/keyboard menu navigation, or wiring UI to game state.

installazioni
5
GitHub Stars
1,1K
Aggiornato
10 set
gamedev-skills
Community

godot-3d-essentials

Set up a Godot 4.7 3D scene: Node3D transforms, Camera3D, lighting (DirectionalLight3D/OmniLight3D), WorldEnvironment for sky/ambient/tonemap/post, MeshInstance3D materials, and GridMap for tile-based 3D levels. Use when building a 3D scene in a Godot project, placing cameras/lights, configuring environment and post-processing, or working with Node3D/.tscn 3D content and GridMap.

installazioni
2
GitHub Stars
1,1K
Aggiornato
10 set
gamedev-skills
Community

godot-animation

Animate in Godot 4.7 three ways: AnimationPlayer for keyframed clips (incl. call and signal tracks), AnimationTree with state machines and blend spaces for character animation, and Tween for short procedural/UI tweens via createtween(). Use when working with AnimationPlayer/AnimationTree nodes in a .tscn, blending character states, sprite-sheet animation, or code-driven Tweens.

installazioni
2
GitHub Stars
1,1K
Aggiornato
10 set