Perception component for enemy AI not showing on C++

I’ve been trying to set up a simple perception system for a hack ‘n’ slash enemy AI, in which the enemy simply attacks the player if he sees him. In my base enemy class constructor, inheriting from ACharacter, I have:

perception = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("Perception Component"));
sight = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("Sight Config"));
perception->ConfigureSense(*sight);
perception->SetDominantSense(sight->GetSenseImplementation());

However, when creating an enemy inheriting from this class, the perception component isn’t there and there’s no sense config options or any way to register the stimuli. I’ve also tried changing the perception component to a UPawnSensing class, but it still doesn’t appear.

How do I make this perception component work?

Hey Herreteman,

I think the perception component should mostly be used on the AIController rather than the character.

Then you can use the functions SetPerceptionComponent() & GetAIPerceptionComponent()

It will become something like this in MyCustomAIController.cpp :

SetPerceptionComponent(*ObjectInitializer.CreateDefaultSubobject<UAIPerceptionComponent>(this, TEXT("AIPerception Component")));
UAISenseConfig_Sight* sightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("Sight Config"));
GetAIPerceptionComponent()->ConfigureSense(*sightConfig);
GetAIPerceptionComponent()->SetDominantSense(sightConfig->GetSenseImplementation());

Hope this helps :slight_smile:
Elias

This solved it, thanks! I should study the base C++ classes a bit more, since I didn’t know that AI Controller was a separate class.