Hello,
So I have been working with the perception component but and I have configured a few senses, and then i try to use the OnPerceptionUpdated but the print statement does not work. This is what I have
EDIT
This is my custom controller class possess function called MobAI:
void AMobAI::Possess(APawn* InPawn) {
Super::Possess(InPawn);
Mob = Cast<AMobCharacter>(InPawn);
SetSenses();
GetPerceptionComponent()->OnPerceptionUpdated.AddDynamic(this, &AMobAI::UpdatePerception);
}
This is the function SetSenses():
void AMobAI::SetSenses() {
Cast<UMobAIPerceptionComponent>(GetPerceptionComponent())->SetSightConfig(Mob);
Cast<UMobAIPerceptionComponent>(GetPerceptionComponent())->SetHearingConfig(Mob);
Cast<UMobAIPerceptionComponent>(GetPerceptionComponent())->SetTouchConfig(Mob);
}
This is the custom PerceptionComponent called MobAIPerceptionComponent:
UMobAIPerceptionComponent::UMobAIPerceptionComponent(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
sightConfig = ObjectInitializer.CreateDefaultSubobject<UMobAISenseConfig_Sight>(this, TEXT("Sight Config"));
hearingConfig = ObjectInitializer.CreateDefaultSubobject<UMobAISenseConfig_Hearing>(this, TEXT("Hearing Config"));
touchConfig = ObjectInitializer.CreateDefaultSubobject<UMobAISenseConfig_Touch>(this, TEXT("Touch Config"));
}
void UMobAIPerceptionComponent::SetSightConfig(AMobCharacter* Mob) {
sightConfig->SetConfig(Mob->Sight, Mob->Sight + (Mob->Sight / 2), 65, true, true, true);
ConfigureSense(*sightConfig);
}
void UMobAIPerceptionComponent::SetHearingConfig(AMobCharacter* Mob) {
hearingConfig->SetConfig(Mob->Hear, true, true, true);
ConfigureSense(*hearingConfig);
}
void UMobAIPerceptionComponent::SetTouchConfig(AMobCharacter* Mob) {
ConfigureSense(*touchConfig);
SetDominantSense(touchConfig->GetSenseImplementation());
}
And finally the custom sense config class (it is essentially the same for all three):
void UMobAISenseConfig_Sight::SetConfig(float Radius, float loseRadius, float VisionAngle, bool detectEnemies, bool detectFreindly, bool detectNuetral) {
SightRadius = Radius;
LoseSightRadius = loseRadius;
PeripheralVisionAngleDegrees = VisionAngle;
DetectionByAffiliation.bDetectEnemies = detectEnemies;
DetectionByAffiliation.bDetectFriendlies = detectFreindly;
DetectionByAffiliation.bDetectNeutrals = detectNuetral;
}
When I use the gameplay debugger (I have actually never used it before, so bear with me) this is what happens:
EDIT
So with Perception enabled it clearly shows the character is within range of both sight and hearing.
EDIT
I tried using:
TArray<AActor*> actors;
GetPerceptionComponent()->GetPerceivedActors(Cast<UMobAIPerceptionComponent>(GetPerceptionComponent())->hearingConfig->GetSenseImplementation(), actors);
print(FString::FromInt(actors.Num()));
In order to see if anything was being seen, but it showed a empty array.
EDIT
Okay so here are the full files: Dropbox - AIPerception - Simplify your life
All of the variables are set via blueprint in the editor on the MobCharacter Class, everything else is derived from there.
Thank you,