UAIPerception OnPerceptionUpdated not working

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,

Hey ,

Could it be that your affiliation settings are wrong?
Try:

sightConfig->DetectionByAffiliation.bDetectEnemies = true;
sightConfig->DetectionByAffiliation.bDetectNeutrals = true;
sightConfig->DetectionByAffiliation.bDetectFriendlies = true;

Hope this helps,
Elias

all affiliations are set to true, and it still does not work.
I will put the code for the config set up for clarification.

Hey -

OnPerceptionUpdated is called from UAIPerceptionComponent::ProcessStimuli
if the UpdatedActors array is not empty and the AIOwner is not null. ProcessStimuli is called from UAIPerceptionSystem::Tick if the following conditions are met:

if (bNeedsUpdate || bStimuliDelivered || bSomeListenersNeedUpdateDueToStimuliAging)
		{
			for (AIPerception::FListenerMap::TIterator ListenerIt(ListenerContainer); ListenerIt; ++ListenerIt)
			{
				check(ListenerIt->Value.Listener.IsValid())
					if (ListenerIt->Value.HasAnyNewStimuli())
					{
						ListenerIt->Value.ProcessStimuli();
					}
			}
		}

Let me know if you can run the project in debug mode and ensure that both if() statements are begin hit in UAIPerceptionSystem::Tick or if UpdatedActors / AIOwner is empty/null (respectively).

If these events are being triggered and you are still not getting OnPerceptionUpdated called, please include the full changes to your AIPerception class and how you’re adding it to your AI in the editor.

Out of curiosity, does print("Something"); work in UE4 at all? :smiley:

Also, what’s the UMobAISenseConfig_Sight? What sense does it represent? Did you implement it on your own? Is this an extension of any of the other senses? Do you have any sources registered for it?

Okay I shall try this when I get home

No it does not, but I use a #define print(text) {…} on top of all of my .cpp files just to make my life easier.
UMobAISenseConfig_Sight is a custom class that I implemented as a child of AISenseConfig_Sight (I have one for touch and hearing also just decided to put this one as an example). Obviously it represents the sense sight. And Sources? I do not believe so, maybe that is where the issue is? How would I go about doing that?

I use a #define print(text) {…} on top of all of my .cpp files just to make my life easier.

Bad practice, reduces code portability. Also, brings in confusion when showing the code to other people and you need to explain code that shouldn’t need explaining.

Regarding sources, every sense can specify that it wants every pawn to get registered as its sources (UAISense.bAutoRegisterAllPawnsAsSources). If you set it to false you’ll have to manually call UAIPerceptionSystem::RegisterSourceForSenseClass.

Have you tried using GameplayDebugger to visualize your AI’s perception?

I just got home and I tried out the Gameplay Debugger, I edited my answer, please have a look. I also tried the register the source but it did not work.

When you use the gameplay debugger, keep in mind you need to use Numpad 1-4 not regular 1-4
Even with nothing there you should get the heading on screen.

yes i tried both, thank you though, and I did not get a heading

Actually it appears I had num lock off, it is working now

When i was running the project in debug mode I was unable to set breakpoints for this the Perception System file due to there being no debug symbols. I am also unable to access these variables because they are local (except the last one which is private)

Actually I rebuilt the engine and then I found out that they all return false as far as I can tell.

This is still causing problems, any help would be appreciated

Hey -

I am attempting to reproduce the behavior you’ve described and I want to ensure that I’m setting up my AI as closely to yours as possible. Could you provide the steps used when setting up your AI class / actor to help me test this issue locally?

A Blueprint of MobCharacter class (extension of Pawn) is is placed manually. My AI is set up pretty simply in the constructor of this class using:

AIControllerClass = AMobAI::StaticClass();

And that is all that I have done in my MobCharacter class except for a couple of extra variables and a mesh. In the AI, I put everything in the question already.

Are the additional variables in your character class Sight and Hear shown in the SetSightConfig and SetHearingConfig functions? How are the values of these variables being set? Do you have three separate classes for UAISenseConfig_Sight / Hearing / Touch? If possible could you provide the full classes used for your AI setup.

Okay, I put a link to the files on drop box, it includes the ai, perception component, character, and all 3 configs. That should have all of the information you need.

Hey Joel-

Can you provide a link to the dropbox to access the files? You can send me a PM on the forums with the download link if privacy is a concern.