상세 컨텐츠

본문 제목

Behavior Tree (2. Decorator Nodes)

Unreal Engine

by geminanolja 2025. 1. 13. 03:23

본문

What Are Decorator Nodes in Unreal Engine?

Can be attached to a Composite Node or task node as a gate keeper

Define if a branch/ Node can be executed base on certain conditions

 

  • Execution Context: If a decorator's condition evaluates to true, the child node is allowed to execute. If false, the child node is skipped.
  • Customization: Decorators can be either prebuilt (provided by Unreal Engine) or custom-built in C++/Blueprint.

 


Common Use Cases

  1. Conditional Execution:
    • Example: Run a task only if the AI has detected the player or is within a certain range.
    • Decorators like Blackboard-Based Condition are frequently used.
  2. Behavior Modification:
    • Example: Retry a task multiple times or invert the success/failure status.
    • Decorators like Retry or Invert Result help refine AI logic.
  3. Runtime Checks:
    • Example: Ensure a specific resource is available before proceeding with a task.
    • Decorators evaluate conditions dynamically during execution.

Built-in Decorator Nodes

Unreal Engine provides several out-of-the-box decorator nodes:

  1. Blackboard-Based Condition:
    • Allows you to check a specific value in the Blackboard (AI's shared memory).
    • Example: Ensure the TargetActor key is set before running a task.
  2. Cooldown:
    • Prevents a node from being executed too frequently by enforcing a cooldown period.
  3. Force Success/Failure:
    • Overrides the result of a child node to always succeed or fail.
  4. Loop/Retry:
    • Repeats a child node for a specified number of iterations or until a condition is met.

Custom Decorator Nodes

You can create custom decorator nodes using Blueprints or C++.

Blueprint-Based Custom Decorator

  1. Go to the Content Browser.
  2. Create a new Blueprint Class and select BTDecorator.
  3. Implement the logic for:
    • PerformConditionCheck: Define the condition that determines whether the child node executes.
    • Optional: Add variables and functionality to make the decorator reusable.

C++ Custom Decorator

#include "BehaviorTree/Decorators/BTDecorator_BlueprintBase.h"

UCLASS()
class YOURGAME_API UCustomDecorator : public UBTDecorator_BlueprintBase
{
    GENERATED_BODY()

protected:
    virtual bool CalculateRawConditionValue(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) const override
    {
        // Example: Check if AI has more than 50 health
        AAIController* AIController = OwnerComp.GetAIOwner();
        APawn* Pawn = AIController->GetPawn();
        if (Pawn)
        {
            float Health = Pawn->GetHealth();
            return Health > 50.0f;
        }
        return false;
    }
};

How to Use a Decorator Node

  1. Open your Behavior Tree in the Behavior Tree Editor.
  2. Add a task or composite node (like a Selector or Sequence).
  3. Right-click on the task or composite node and add a Decorator Node.
  4. Configure the decorator in the Details Panel (e.g., select Blackboard keys, set conditions).

Example Scenario: Patrolling AI

  1. Selector Node:
    • Chooses between patrolling or chasing the player.
  2. Decorator (Blackboard-Based Condition):
    • On "Chase Player" task: Check if the Blackboard key CanSeePlayer is true.
  3. Cooldown Decorator:
    • On "Patrol" task: Limit how often the AI can switch waypoints.

By using decorator nodes, you can create AI that reacts dynamically to its environment and player actions.

관련글 더보기