# Configure PerceptionComponent in C++

**URL:** <https://forums.unrealengine.com/t/configure-perceptioncomponent-in-c/379598>\
**Category:** AI\
**Tags:** aiperception, sight, unreal-engine, UE4, question, CPP\
**Created:** [December 18, 2016, 6:58am UTC](https://forums.unrealengine.com/t/configure-perceptioncomponent-in-c/379598 "2016-12-18T06:58:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![carlboisvertdev](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/carlboisvertdev/32/987848_2.png) [@carlboisvertdev](https://forums.unrealengine.com/u/carlboisvertdev)\
**Post date:** [December 18, 2016, 6:58am UTC](https://forums.unrealengine.com/t/configure-perceptioncomponent-in-c/379598/1 "2016-12-18T06:58:30Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_a8366a311](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a8366a311](https://forums.unrealengine.com/u/anonymous_user_a8366a311)\
**Post date:** [December 18, 2016, 2:10pm UTC](https://forums.unrealengine.com/t/configure-perceptioncomponent-in-c/379598/2 "2016-12-18T14:10:05Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![carlboisvertdev](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/carlboisvertdev/32/987848_2.png) [@carlboisvertdev](https://forums.unrealengine.com/u/carlboisvertdev)\
**Post date:** [December 21, 2016, 6:10pm UTC](https://forums.unrealengine.com/t/configure-perceptioncomponent-in-c/379598/3 "2016-12-21T18:10:28Z")

</div>

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!
