Hello, i’m trying to implement logic when entering a sequence and when exiting a sequence. I thought that decorators where the thing to do. I tried writing the code below which are my .h and .cpp respectively. Is there a way to hook code when entering a composite node and when exiting a composite node (and that the code run EVEN when the tree is aborted? kind of like an OnExit(). Right now, my functions are never called
.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "BehaviorTree/BTDecorator.h"
#include "AIGroupManagerDecorator.generated.h"
/**
*
*/
UCLASS()
class SOFTDESIGNTRAINING_API UAIGroupManagerDecorator : public UBTDecorator
{
GENERATED_BODY()
virtual void OnNodeActivation(FBehaviorTreeSearchData& SearchData) override;
virtual void OnNodeDeactivation(FBehaviorTreeSearchData& SearchData, EBTNodeResult::Type NodeResult) override;
virtual void OnNodeProcessed(FBehaviorTreeSearchData& SearchData, EBTNodeResult::Type& NodeResult) override;
virtual void OnBecomeRelevant(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) override;
virtual void OnCeaseRelevant(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) override;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "SoftDesignTraining.h"
#include "AIGroupManagerDecorator.h"
#include "SDTAIController.h"
#include "AIAgentGroupManager.h"
#include "Runtime/Engine/Classes/Engine/Engine.h"
void UAIGroupManagerDecorator::OnNodeActivation(FBehaviorTreeSearchData & SearchData)
{
if (GEngine)
{
const int32 AlwaysAddKey = -1; // Passing -1 means that we will not try and overwrite an
// existing message, just add a new one
GEngine->AddOnScreenDebugMessage(AlwaysAddKey, 3.f, FColor::Yellow, "TEST");
}
if (ASDTAIController* aiController = Cast<ASDTAIController>(SearchData.OwnerComp.GetAIOwner()))
{
AIAgentGroupManager::GetInstance()->RegisterAIAgent(aiController);
}
}
void UAIGroupManagerDecorator::OnNodeDeactivation(FBehaviorTreeSearchData & SearchData, EBTNodeResult::Type NodeResult)
{
if (ASDTAIController* aiController = Cast<ASDTAIController>(SearchData.OwnerComp.GetAIOwner()))
{
AIAgentGroupManager::GetInstance()->UnregisterAIAgent(aiController);
}
}
void UAIGroupManagerDecorator::OnNodeProcessed(FBehaviorTreeSearchData & SearchData, EBTNodeResult::Type & NodeResult)
{
if (ASDTAIController* aiController = Cast<ASDTAIController>(SearchData.OwnerComp.GetAIOwner()))
{
AIAgentGroupManager::GetInstance()->UnregisterAIAgent(aiController);
}
}