Configure PerceptionComponent in C++

Hi,

I did something similar to that some time ago, but instead of put as property the SenseConfig, I put in my NPC Pawn only the properties that I want to set on the Perception and in the AIController I already have the perception component with the SightSense added. With that, the only thing that I do is after Possess the NPC, get the SightSense on the Controller/PerceptionComponent and set the config variables with the variables on my Pawn.

I have something like this in my AIController:

//Update UAISenseConfig_Sight properties based con possessed pawn
	AMyCharacter* Character = Cast<AMyCharacter>(GetPawn());
	if (Character)
	{
		// Setup AIController's Perception system values
		UAIPerceptionComponent* PerceptionComp = GetAIPerceptionComponent();
		if (PerceptionComp)
		{
			FAISenseID SenseID = UAISense::GetSenseID<UAISense_Sight>();
			UAISenseConfig_Sight* SenseConfig = Cast<UAISenseConfig_Sight>(PerceptionComp->GetSenseConfig(SenseID));
			if (SenseConfig)
			{
				// Noticie here, in AMyCharacter I have two properties AISenseSight_Radius, AISenseSight_LoseSightRadius that allow me to define
				// the perception setup for this specific character using the same AIController class.
				SenseConfig->SightRadius = Character->AISenseSight_Radius;
				SenseConfig->LoseSightRadius = Character->AISenseSight_LoseSightRadius;

				PerceptionComp->ConfigureSense(*SenseConfig);
			}
		}
	}

On the other hand, to bind the perceptionupdated function:

// AIController .h

UFUNCTION()
void OnTargetPerceptionUpdated(AActor* Actor, FAIStimulus Stimulus);

// MyAIController .cpp

void AMyAIController::BeginPlay()
{
	Super::BeginPlay();

	UAIPerceptionComponent* PerceptionComp = GetAIPerceptionComponent();
	if (PerceptionComp)
	{
		PerceptionComp->OnTargetPerceptionUpdated.AddDynamic(this, &AMyAIController::OnTargetPerceptionUpdated);
	}
}

void AMyAIController::OnTargetPerceptionUpdated(AActor* Actor, FAIStimulus Stimulus)
{
    // do something
}

Hope this help you.

Best regards,