All skills
gamedev-skills avatar

/unreal-behavior-trees

@e4e9569

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.

Use this Skill: https://skilld.dev/gh/gamedev-skills/awesome-gamedev-agent-skills/unreal-behavior-trees

This session only. Nothing lands on disk.

referencescustom-bttask.md

≈921 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Custom BTTask in C++ (UE 5.8)

Depth for unreal-behavior-trees: writing a custom Behavior Tree task node in C++. Verified against the Behavior Trees documentation and the UBTTaskNode API. Requires the AIModule dependency in *.Build.cs.

Instant task (returns immediately)

// BTTask_ClearTarget.h
#pragma once
#include "CoreMinimal.h"
#include "BehaviorTree/BTTaskNode.h"
#include "BTTask_ClearTarget.generated.h"

UCLASS()
class MYGAME_API UBTTask_ClearTarget : public UBTTaskNode
{
    GENERATED_BODY()
public:
    UBTTask_ClearTarget();
    virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent& OwnerComp,
                                            uint8* NodeMemory) override;

    // Exposes a Blackboard key picker in the BT editor.
    UPROPERTY(EditAnywhere, Category = "Blackboard")
    FBlackboardKeySelector TargetKey;
};
// BTTask_ClearTarget.cpp
#include "BTTask_ClearTarget.h"
#include "BehaviorTree/BlackboardComponent.h"

UBTTask_ClearTarget::UBTTask_ClearTarget()
{
    NodeName = "Clear Target";
}

EBTNodeResult::Type UBTTask_ClearTarget::ExecuteTask(UBehaviorTreeComponent& OwnerComp,
                                                     uint8* NodeMemory)
{
    if (UBlackboardComponent* BB = OwnerComp.GetBlackboardComponent())
    {
        BB->ClearValue(TargetKey.SelectedKeyName);
        return EBTNodeResult::Succeeded;     // done now
    }
    return EBTNodeResult::Failed;
}

EBTNodeResult::Type values: Succeeded, Failed, Aborted, InProgress.

Latent task (finishes over time)

Return InProgress, then call FinishLatentTask when the work completes (e.g. a timer, an async query, or a delegate callback).

EBTNodeResult::Type UBTTask_WaitRandom::ExecuteTask(UBehaviorTreeComponent& OwnerComp,
                                                    uint8* NodeMemory)
{
    AAIController* AICon = OwnerComp.GetAIOwner();
    if (!AICon) return EBTNodeResult::Failed;

    const float Delay = FMath::FRandRange(MinTime, MaxTime);

    // Schedule completion; capture the component so we can finish the right task instance.
    FTimerHandle Handle;
    AICon->GetWorldTimerManager().SetTimer(Handle, FTimerDelegate::CreateLambda(
        [&OwnerComp, this]()
        {
            FinishLatentTask(OwnerComp, EBTNodeResult::Succeeded);   // resumes the tree
        }), Delay, /*loop*/ false);

    return EBTNodeResult::InProgress;        // tree waits on this node until FinishLatentTask
}

If a latent task can be aborted by the tree (e.g. a higher-priority branch takes over), override AbortTask to cancel timers/queries and return EBTNodeResult::Aborted.

Reading and writing typed Blackboard values

UBlackboardComponent* BB = OwnerComp.GetBlackboardComponent();
AActor* Target  = Cast<AActor>(BB->GetValueAsObject(TargetKey.SelectedKeyName));
FVector Loc     = BB->GetValueAsVector(TEXT("LastKnownLocation"));
BB->SetValueAsBool(TEXT("bIsInvestigating"), true);

Notes

  • Set NodeName in the constructor so the node reads clearly in the BT graph.
  • OwnerComp.GetAIOwner() gives the AAIController; GetAIOwner()->GetPawn() the controlled pawn — use these instead of caching world pointers.
  • For nodes that need per-instance memory (counters, handles), override GetInstanceMemorySize and use the NodeMemory block; for simple cases, member UPROPERTYs on a non-instanced node are shared across all uses, so prefer instance memory or instanced nodes when state matters.
  • A custom Service subclasses UBTService and overrides TickNode; a custom Decorator subclasses UBTDecorator and overrides CalculateRawConditionValue.

Source: SKILL.md on GitHub

No alerts5d3 checks · Risk SAFE
  • Gen Agent Trust Hub5d

    The skill provides technical documentation and C++ code examples for Unreal Engine 5 Behavior Trees. All instructions and references are standard for game development and no malicious patterns were detected.

  • Socket5d

    No alerts

  • Snyk5d

    Risk: LOW · No issues

Signed by skilld at e4e9569. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 3 days ago.

Activeupdated last week

README badge

README badge for gamedev-skills/awesome-gamedev-agent-skills/unreal-behavior-trees