상세 컨텐츠

본문 제목

Behavior Tree (4. Task Nodes)

Unreal Engine

by geminanolja 2025. 1. 13. 03:31

본문

Task Nodes are a core component of the Behavior Tree system, used to define specific actions or operations that the AI performs. These nodes represent the "leaves" of the tree, as they execute concrete actions like moving to a location, playing an animation, or interacting with the environment.

 


What Are Task Nodes in Behavior Trees?

  • Purpose: Task Nodes perform actions or logic for the AI.
  • Execution Context: They are the endpoints of a Behavior Tree branch, where actual work is carried out.
  • Result: A task can return a result of Success, Failure, or In Progress, which determines how the parent node proceeds.

Key Characteristics of Task Nodes

  1. Action-Oriented:
    • Tasks execute a specific action, such as navigating to a target or updating blackboard keys.
  2. Blocking or Non-blocking:
    • Tasks can block the flow of the Behavior Tree until they are complete or return immediately.
  3. Reusable Logic:
    • Tasks can be reused across different parts of the Behavior Tree or across different AIs.
  4. Customization:
    • You can use built-in tasks or create custom tasks using Blueprints or C++.

Built-in Task Nodes

Unreal Engine provides several built-in task nodes that handle common AI actions, such as:

  1. Move To:
    • Directs the AI to navigate to a specific location or actor.
    • Configurable using blackboard keys for dynamic targets.
  2. Wait:
    • Makes the AI wait for a specified duration before continuing.
  3. Play Animation:
    • Executes a specific animation on the AI character.
  4. Run Behavior:
    • Executes a sub-behavior tree, allowing complex behavior compositions.

Creating Custom Task Nodes

You can create your own task nodes using Blueprints or C++.


Blueprint-Based Custom Task

  1. Create a New Task:
    • In the Content Browser, create a new Blueprint Class based on BTTaskNode.
  2. Define Task Logic:
    • Open the Blueprint and implement logic for:
      • Event Receive Execute AI: Runs when the task starts.
      • Event Receive Abort AI: Runs when the task is aborted (optional).
    • Use the AI Controller, Pawn, and Blackboard components to interact with the AI.

Example: Updating a Blackboard key with the AI's current health:

Event Receive Execute AI:
    - Get AI Pawn
    - Get Health
    - Set Blackboard Key "CurrentHealth"
    - Finish Execute (Success)

C++ Custom Task

To create a custom task in C++:

  1. Create a Task Class:
    • Create a new class derived from UBTTaskNode.
  2. Implement the Task Logic:
    • Override the ExecuteTask and optionally AbortTask methods.

Example:

#include "BehaviorTree/Tasks/BTTaskNode.h"
#include "CustomBTTask.generated.h"

UCLASS()
class YOURGAME_API UCustomBTTask : public UBTTaskNode
{
    GENERATED_BODY()

public:
    UCustomBTTask();

protected:
    virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) override;
};

#include "CustomBTTask.h"
#include "AIController.h"
#include "GameFramework/Actor.h"

UCustomBTTask::UCustomBTTask()
{
    NodeName = "Custom Task";
}

EBTNodeResult::Type UCustomBTTask::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
    AAIController* AIController = OwnerComp.GetAIOwner();
    if (AIController)
    {
        APawn* Pawn = AIController->GetPawn();
        if (Pawn)
        {
            // Example: Print a debug message
            UE_LOG(LogTemp, Warning, TEXT("Task executed by %s"), *Pawn->GetName());
            return EBTNodeResult::Succeeded;
        }
    }
    return EBTNodeResult::Failed;
}
  1. Compile and Use:
    • Attach the custom task to a Behavior Tree and configure its properties.

How to Use Task Nodes

  1. Add to Behavior Tree:
    • Open the Behavior Tree Editor.
    • Drag a task node under a composite node (e.g., Sequence, Selector).
  2. Configure Properties:
    • For built-in tasks, configure settings like target locations, animations, or blackboard keys.
  3. Connect Nodes:
    • Link the task node to its parent composite node.

Example Scenario: Patrolling AI

  1. Selector Node:
    • Decides between "Patrol" and "Chase Player."
  2. Sequence Node (Patrol):
    • Task: Move To: Navigate to a waypoint.
    • Task: Wait: Pause for 2 seconds at the waypoint.
  3. Sequence Node (Chase Player):
    • Task: Move To: Navigate to the player's location.
    • Task: Attack Player: Play an attack animation.

 

관련글 더보기