Configure AI sense before registering sense leads to no listener

Hi there,

I have an actor, let’s call it MyListener, that uses a UAIPerceptionComponent and configures a sense in PostInitializeComponents():

GetPerceptionComponent()->ConfigureSense(*this->SenseConfig_Ball);

Another actor, StimSource, uses UAIPerceptionStimuliSourceComponent and registers itself in PostInitializeComponents():

this->AIStimuliSource->RegisterForSense(UAISense_Ball::StaticClass());

Everything works so far. The only problem is that the configuring of the sense is called before the sense is registered. This leads to the UAIPerceptionSystem adding the MyListener to the Listeners. But creating the sense does not notify the sense of already existing listeners (UAIPerceptionSystem::RegisterSenseClass(TSubclassOf SenseClass).

Unfortunately, UAIPerceptionSystem::RegisterSenseClass(…) is not virtual, so I can’t add them myself upon sense creation…

Am I missing something?

I just found out that I can also configure the sense in the constructor. This will lead to the sense classes being automatically created when the UAIPerceptionComponent::OnRegister of MyListener is called.

However, I get a crash in UAIPerceptionSystem::RegisterSenseClass:

FAISenseID UAIPerceptionSystem::RegisterSenseClass(TSubclassOf<UAISense> SenseClass)
{
	check(SenseClass);
	FAISenseID SenseID = UAISense::GetSenseID(SenseClass);
	if (SenseID.IsValid() == false)
	{
		UAISense* SenseCDO = GetMutableDefault<UAISense>(SenseClass);
		SenseID = SenseCDO->UpdateSenseID();  // CRASH!!!

		if (SenseID.IsValid() == false)
		{
			// @todo log a message here
			return FAISenseID::InvalidID();
		}
	}
...

Works now:

Simply configure sense in constructor and don’t forget to set the GetSenseImplementation() in the sense config.