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
- 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.
- Behavior Modification:
- Example: Retry a task multiple times or invert the success/failure status.
- Decorators like Retry or Invert Result help refine AI logic.
- 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:
- 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.
- Cooldown:
- Prevents a node from being executed too frequently by enforcing a cooldown period.
- Force Success/Failure:
- Overrides the result of a child node to always succeed or fail.
- 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
- Go to the Content Browser.
- Create a new Blueprint Class and select BTDecorator.
- 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
- Open your Behavior Tree in the Behavior Tree Editor.
- Add a task or composite node (like a Selector or Sequence).
- Right-click on the task or composite node and add a Decorator Node.
- Configure the decorator in the Details Panel (e.g., select Blackboard keys, set conditions).
Example Scenario: Patrolling AI
- Selector Node:
- Chooses between patrolling or chasing the player.
- Decorator (Blackboard-Based Condition):
- On "Chase Player" task: Check if the Blackboard key CanSeePlayer is true.
- 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.