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.
Unreal Engine provides several built-in services, such as:
You can create a custom Service Node using either Blueprints or C++.
Example:
Event Receive Tick AI:
- Get AI Pawn and Player Pawn
- Calculate Distance
- Set Blackboard Key "PlayerDistance"
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);
}
}
}
}
웨어 이즈 마이 UCharacterMovementComponent?? (0) | 2025.03.04 |
---|---|
Behavior Tree // Environment Query System(EQS) (0) | 2025.01.14 |
Behavior Tree (4. Task Nodes) (1) | 2025.01.13 |
Behavior Tree (2. Decorator Nodes) (1) | 2025.01.13 |
Behavior Tree (1. Composite nodes (0) | 2025.01.13 |