상세 컨텐츠

본문 제목

Behavior Tree (3. Service Nodes)

Unreal Engine

by geminanolja 2025. 1. 13. 03:27

본문

Service Nodes are a crucial part of the Behavior Tree system. They allow you to execute logic on a recurring basis while a Behavior Tree branch is active.

This makes them ideal for tasks that require constant monitoring or periodic updates, such as refreshing AI perceptions, updating blackboard values, or triggering timed actions.

 Service Nodes in Behavior Trees?

  • Purpose: Service Nodes attach to composite nodes (like Selectors or Sequences) and run logic at regular intervals while the composite node (and its children) is active.
  • Execution Frequency: Services execute their logic on a user-defined tick interval.
  • Scope: Services affect all child nodes of the composite node they are attached to.
  • Customization: You can use prebuilt services or create custom services in Blueprints or C++.

Key Characteristics

  1. Periodic Updates:
    • Services execute at intervals defined by the Interval and Random Deviation settings.
  2. Non-blocking:
    • Unlike tasks, services don't block the Behavior Tree's flow; they run asynchronously.
  3. Attached to Composite Nodes:
    • Services are attached to composite nodes and impact all children of that composite.
  4. Dynamic Logic:
    • Frequently used for updating blackboard keys, checking environmental conditions, or running AI sensing logic.

Built-in Service Nodes

Unreal Engine provides several built-in services, such as:

  1. Run EQS Query:
    • Executes an Environmental Query System (EQS) query and updates blackboard keys with the results.
  2. Custom Blackboard Update:
    • Can modify blackboard values, often used to track AI states or perceptions.

Creating Custom Service Nodes

You can create a custom Service Node using either Blueprints or C++.


Blueprint-Based Custom Service

  1. Open the Content Browser.
  2. Create a new Blueprint Class and select BTService.
  3. Open the Blueprint and implement the following:
    • Event Receive Tick AI: Define logic that runs periodically while the service is active.
    • Event Receive Activation AI: Optional logic when the service is activated.
    • Event Receive Deactivation AI: Optional logic when the service is deactivated.
  4. Set the Interval and Random Deviation in the Details Panel.

Example:

  • Update a Blackboard Key with the player's distance: 
  • Event Receive Tick AI: - Get AI Pawn and Player Pawn - Calculate Distance - Set Blackboard Key "PlayerDistance"

C++ Custom Service

To create a service in C++:

#include "BehaviorTree/Services/BTService.h"
#include "CustomBTService.generated.h"

UCLASS()
class YOURGAME_API UCustomBTService : public UBTService
{
    GENERATED_BODY()

public:
    UCustomBTService();

protected:
    virtual void TickNode(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds) override;
};

#include "CustomBTService.h"
#include "AIController.h"
#include "YourGameAICharacter.h"
#include "BehaviorTree/BlackboardComponent.h"

UCustomBTService::UCustomBTService()
{
    NodeName = "Custom Service";
    Interval = 1.0f; // Tick every second
    RandomDeviation = 0.5f;
}

void UCustomBTService::TickNode(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds)
{
    Super::TickNode(OwnerComp, NodeMemory, DeltaSeconds);

    AAIController* AIController = OwnerComp.GetAIOwner();
    if (AIController)
    {
        AYourGameAICharacter* AICharacter = Cast<AYourGameAICharacter>(AIController->GetPawn());
        if (AICharacter)
        {
            // Example: Update the player's distance in the Blackboard
            UBlackboardComponent* BlackboardComp = AIController->GetBlackboardComponent();
            if (BlackboardComp)
            {
                FVector PlayerLocation = AICharacter->GetPlayerLocation();
                FVector AILocation = AICharacter->GetActorLocation();
                float Distance = FVector::Dist(PlayerLocation, AILocation);
                BlackboardComp->SetValueAsFloat("PlayerDistance", Distance);
            }
        }
    }
}

How to Use Service Nodes

  1. Add a Composite Node:
    • Open the Behavior Tree Editor and add a composite node (e.g., Selector or Sequence).
  2. Attach a Service Node:
    • Right-click on the composite node and select "Add Service."
  3. Configure the Service:
    • Set properties like Interval, Random Deviation, or specific logic in the Details Panel.

Example Scenario: Enemy AI Patrol and Chase

  1. Selector Node:
    • Decides whether to patrol or chase the player.
  2. Service Node (Custom):
    • Periodically updates the CanSeePlayer blackboard key by checking if the player is in line of sight.
  3. Sequence Node (Chase Player):
    • Executes the chase logic if CanSeePlayer is true.
  4. Sequence Node (Patrol):
    • Executes patrol logic if CanSeePlayer is false.

 

관련글 더보기