I have following AIController.
AEnemyAIController::AEnemyAIController()
{
Blackboard = CreateDefaultSubobject<UBlackboardComponent>(TEXT("Blackboard"));
BehaviorTree = CreateDefaultSubobject<UBehaviorTreeComponent>(TEXT("BehaviorTreeComponent"));
PerceptionComponent = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("PerceptionComponent"));
PerceptionComponent->OnPerceptionUpdated.AddDynamic(this, &AEnemyAIController::OnPerceptionUpdated);
UAISenseConfig_Sight* SightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("AISightConfig"));
SightConfig->SightRadius = 500;
SightConfig->LoseSightRadius = 550;
SightConfig->PeripheralVisionAngleDegrees = 90.f;
SightConfig->DetectionByAffiliation.bDetectEnemies = true;
SightConfig->DetectionByAffiliation.bDetectNeutrals = false;
SightConfig->DetectionByAffiliation.bDetectFriendlies = false;
PerceptionComponent->ConfigureSense(*SightConfig);
SetGenericTeamId(FGenericTeamId(1));
}
void AEnemyAIController::Possess(APawn* InPawn)
{
Super::Possess(InPawn);
AEnemyCharacter* EnemyCharacter = Cast<AEnemyCharacter>(InPawn);
if (EnemyCharacter != nullptr && EnemyCharacter->BehaviorTree != nullptr)
{
Blackboard->InitializeBlackboard(*EnemyCharacter->BehaviorTree->BlackboardAsset);
EnemyKeyID = Blackboard->GetKeyID("Target");
FBlackboard::FKey HomeLocationKey = Blackboard->GetKeyID("HomeLocation");
Blackboard->SetValue<UBlackboardKeyType_Vector>(HomeLocationKey, InPawn->GetActorLocation());
BehaviorTree->StartTree(*EnemyCharacter->BehaviorTree);
}
}
For some reason blackboard instance is not unique and HomeLocation variable is the same for all enemies and equal to last enemy. As I understand I need to have unique blackboard for each enemy character. How to organize this better, I mean “in UE way”.