Chuyển đến nội dung
Skillpedia
Lập trình chung6.6k · #13852

unreal-behavior-trees

Skill này làm gì

Skill này giải thích cách thiết lập AI cho nhân vật không do người chơi điều khiển trong Unreal Engine 5 bằng behavior tree. Nó dành cho nhà phát triển làm kẻ thù hoặc nhân vật game có lựa chọn.

Khi nào nên dùng

  • ✓Khi bạn đang xây một kẻ thù hoặc NPC phải tự chọn hành động trong Unreal Engine 5.
  • ✓Khi bạn cần thêm điều kiện hoặc kiểm tra định kỳ vào cấu trúc quyết định của AI.
  • ✓Khi bạn muốn viết node hành vi tùy chỉnh hoặc kết nối AI controller với một behavior tree.

Liên kết skill

https://github.com/gamedev-skills/awesome-gamedev-agent-skills/blob/main/skills/unreal/unreal-behavior-trees/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/gamedev-skills/awesome-gamedev-agent-skills/blob/main/skills/unreal/unreal-behavior-trees/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Làm kẻ thù Unreal của tôi tuần tra cho đến khi nó thấy người chơi, rồi đuổi theo họ.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Skill giải thích cách tổ chức cây bằng selector và sequence, thêm điều kiện và cập nhật nền, và chạy cây từ một AI controller dùng Blackboard để lưu thông tin dùng chung.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

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.

Behavior Tree or StateTree? UE 5.8 ships both, and Behavior Trees are not deprecated. StateTree is Epic's general-purpose hierarchical state machine: it combines BT-style selection with explicit States and Transitions. If the project already uses StateTree assets, or the AI is naturally a set of modes with clear transitions (patrol → alert → combat), check with the user before adding a Behavior Tree next to it. Keep using Behavior Trees where the project already has BT_/BB_ assets.

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.
  2. Possess and run. An AAIController possesses the pawn and calls RunBehaviorTree(BT), which also initializes the referenced Blackboard.
  3. 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.
  4. 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.
  5. 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.
  6. Do work in Tasks, which return Succeeded, Failed, or InProgress (latent tasks like MoveTo finish later).
  7. 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++)

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)

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)

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 rea
Câu hỏi thường gặp

Blackboard là gì?

Blackboard là kho thông tin dùng chung, chẳng hạn vị trí người chơi, mà behavior tree đọc.

Tôi có nên dùng StateTree thay thế không?

Skill nói hãy hỏi người dùng trước, vì cả hai hệ thống đều có trong Unreal Engine 5.8 và dự án có thể đang dùng một trong hai.

Skill này có bao quát toán tìm đường không?

Không sâu. Chuyển động dùng hệ thống điều hướng của engine, là một chủ đề riêng.

Chi tiết

Mô tả gốc của tác giả

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. Use when creating enemy/NPC AI, BT_/BB_ assets, custom BTTask or BTService nodes, or when the user mentions Behavior Tree, Blackboard, AIController, BTTask, decorator, or service.

repo: gamedev-skills/awesome-gamedev-agent-skills

Độ phổ biến

Sao GitHub
6.6k
Vị trí xếp hạng
#13852
Giấy phép
Apache-2.0
Cập nhật
28/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự