I have already read the Quick AI Perception Guide and surfed through all the tutorials regarding how to set up the AI Perception in C++. Nothing seems to work.
I have managed to only make it work on Blueprint but NOT in C++.
Below is my code:
BaseCharacter.h // Custom Class
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = Senses)
class UAIPerceptionComponent* PerceptionComponent;
UPROPERTY(BlueprintReadWrite, Category = Senses)
class UAISenseConfig_Sight* SightConfig;
UFUNCTION()
void UpdatePerception(TArray<AActor*>ActorsInSight);
BaseCharacter.cpp
ABaseCharacter::ABaseCharacter()
{
// ....
PerceptionComponent = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("AI Perception Component"));
SightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("AI Sight Config"));
PerceptionComponent->OnPerceptionUpdated.AddDynamic(this, &ABaseCharacter::UpdatePerception);
SightConfig->SightRadius = 2000.f;
SightConfig->LoseSightRadius = 2000.f + 20.f;
SightConfig->PeripheralVisionAngleDegrees = 90.0f;
SightConfig->DetectionByAffiliation.bDetectEnemies = true;
SightConfig->DetectionByAffiliation.bDetectFriendlies = true;
SightConfig->DetectionByAffiliation.bDetectNeutrals = true;
PerceptionComponent->ConfigureSense(*SightConfig);
PerceptionComponent->SetDominantSense(SightConfig->GetSenseImplementation());
}
void ABaseCharacter::UpdatePerception(TArray<AActor*>ActorsInSight)
{
GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Red, TEXT("UPDATE PERCEPTION"));
}