Configure PerceptionComponent in C++

Hi,

So I’m trying to set some basic AI perception sense config in C++, but for some reason, it crashes. Here’s what I have right now.

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "AIController.h"
#include "Perception/AIPerceptionComponent.h"
#include "CitizenAIController.generated.h"


/**
 * 
 */
UCLASS()
class FPS_MMO_API ACitizenAIController : public AAIController
{
	GENERATED_BODY()

	ACitizenAIController();

	UPROPERTY(VisibleAnywhere, Category = "Perception")
	class UAISenseConfig_Sight* config;

	UFUNCTION()
	void OnPerception(AActor* Actor, FAIStimulus Stimulus);
	
};

and my .cpp is quite simple:

// Fill out your copyright notice in the Description page of Project Settings.

#include "FPS_MMO.h"
#include "Perception/AISenseConfig_Sight.h"
#include "Perception/AISense.h"
#include "CitizenAIController.h"


ACitizenAIController::ACitizenAIController() 
{

	config = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("Sight sense"));
	config->SightRadius = 300.f;
	config->LoseSightRadius = 400.f;
	config->PeripheralVisionAngleDegrees = 180.f;
	config->DetectionByAffiliation.DetectAllFlags();
	
	GetPerceptionComponent()->ConfigureSense(*config);
	GetPerceptionComponent()->OnTargetPerceptionUpdated.AddDynamic(this, &ACitizenAIController::OnPerception);
}

void ACitizenAIController::OnPerception(AActor* Actor, FAIStimulus Stimulus) 
{
	if (Stimulus.IsValid()) {
		UE_LOG(LogTemp, Warning, TEXT("Perception valide"));
	}
	else {
		UE_LOG(LogTemp, Warning, TEXT("LOGGING"));
	}
}

I can’t find anything about it in recent versions. Has anybody achieved this?

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,

Thanks a lot for your answer! I finally made it work. I’ve move the code in the begin play to have a better understanding of the problem and realize that the

GetPerceptionComponent();

function was only a shortcut, but you actually had to set the perception component before yourself. Which is weird since if you create a local variable and give me a getter, I expect the variable to be already set. Anyway, big thanks for your time!