This is my code. The Blueprints version seems to work just fine.
#include “SpiderBTService.h”
#include “SpiderController.h”
#include “BehaviorTree/BlackboardComponent.h”
#include “BehaviorTree/Blackboard/BlackboardKeyAllTypes.h”
#include “BehaviorTree/BehaviorTree.h”
#include “Spider.h”
void USpiderBTService::TickNode(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds)
{
Super::TickNode(OwnerComp, NodeMemory, DeltaSeconds);
if (BlackboardComp == nullptr || ThisController == nullptr || ThisAICharacter == nullptr)
{
ThisController = Cast<ASpiderController>(OwnerComp.GetAIOwner());
BlackboardComp = ThisController->GetBlackBoard();
ThisAICharacter = Cast<ASpider>(ThisController->GetPawn());
if (BlackboardComp == nullptr || ThisController == nullptr || ThisAICharacter == nullptr)
{
UE_LOG(LogTemp, Warning, TEXT(“Warning Agro Service performed on invalid AI”));
return;
}
}
if (ThisAICharacter)
{
UE_LOG(LogTemp, Warning, TEXT(“Spider %s service is ticking”), (ThisAICharacter->GetName()));
AActor Target = ThisAICharacter->GetTargetActor();
if (Target)
{
UE_LOG(LogTemp, Warning, TEXT(“Target to follow: %s”), *(Target->GetName()));
ThisController->GetBlackBoard()->SetValueAsObject(TEXT(“TargetToFollow”), Target);
ThisController->GetBlackBoard()->SetValueAsVector(TEXT(“HomeLocation”), ThisAICharacter->GetActorLocation());
ThisController->GetBlackBoard()->SetValueAsVector(TEXT(“TargetLocation”), Target->GetActorLocation());
}
else
{
ThisController->GetBlackBoard()->SetValueAsObject(TEXT(“TargetToFollow”), nullptr);
}
}
}