UAIPerceptionComponent added in AIController via C++ won't allow me to add senses

Hi folks,

I have an issue where I’m adding an UAIPerceptionComponent (code will come soon) to my AIController…
I created a BP from it and I see it as inherited as expected… But… When I want to add senses it won’t let me… It will let me to select the dominant sense but not add senses…

I looked at https://answers.unrealengine.com/questions/602376/perception-component-added-in-c-wont-allow-senses.html which is the most relevant thing I could find and unfortunately it didn’t help…

This is the header file:

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

#pragma once

#include "CoreMinimal.h"
#include "AIController.h"
#include "EnemyAIControllerBase.generated.h"

/**
 * 
 */
UCLASS()
class CODENAMEUNKNOWN_API AEnemyAIControllerBase : public AAIController
{
	GENERATED_BODY()

public:
	AEnemyAIControllerBase();

	UFUNCTION(BlueprintCallable)
	class UBlackboardComponent* GetBlackboardComponent() { return BlackboardComponent; }

protected:
	virtual void BeginPlay() override;
	virtual void OnPossess(APawn* InPawn) override;

private:
	UPROPERTY(Category = "AI", EditDefaultsOnly, BlueprintReadWrite, meta = (AllowPrivateAccess = "true"))
  class UBehaviorTreeComponent* BehaviorTreeComponent;

	UPROPERTY(Category = "AI", EditDefaultsOnly, BlueprintReadWrite, meta = (AllowPrivateAccess = "true"))
	class UBlackboardComponent* BlackboardComponent;

	UPROPERTY(Category = "AI", EditDefaultsOnly, BlueprintReadWrite, meta = (AllowPrivateAccess = "true"))
	class UBehaviorTree* BehaviorTree;

	UPROPERTY(Category = "AI", EditDefaultsOnly, BlueprintReadWrite, meta = (AllowPrivateAccess = "true"))
	class UAIPerceptionComponent* AIPerceptionComponent;
};

This is the CPP file:

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


#include "EnemyAIControllerBase.h"
#include "BehaviorTree/BehaviorTree.h"
#include "BehaviorTree/BehaviorTreeComponent.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "Perception/AIPerceptionComponent.h"

AEnemyAIControllerBase::AEnemyAIControllerBase()
{
  // Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  PrimaryActorTick.bCanEverTick = false;

  BehaviorTree = CreateDefaultSubobject<UBehaviorTree>(TEXT("Behavior Tree"));
  BlackboardComponent = CreateDefaultSubobject<UBlackboardComponent>(TEXT("Blackboard Component"));
  BehaviorTreeComponent = CreateDefaultSubobject<UBehaviorTreeComponent>(TEXT("Behavior Tree Component"));
  AIPerceptionComponent = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("Perception Component"));  

  SetPerceptionComponent(*AIPerceptionComponent);

  AIPerceptionComponent->bEditableWhenInherited = true;
}

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

  if (BehaviorTree == nullptr) {
    return;
  }

  RunBehaviorTree(BehaviorTree);
  BehaviorTreeComponent->StartTree(*BehaviorTree, EBTExecutionMode::Looped);
}

void AEnemyAIControllerBase::OnPossess(APawn* OnPossess)
{
  Super::OnPossess(OnPossess);

  if (BlackboardComponent == nullptr || BehaviorTree == nullptr) {
    return;
  }
  
  UBlackboardData* BlackboardAsset = BehaviorTree->GetBlackboardAsset();

  if (BlackboardAsset != nullptr) {
    BlackboardComponent->InitializeBlackboard(*BlackboardAsset);
  }
}

Would appreciate any help here please, I’m sure I’m missing something dumb, but what? or is it a bug?
I get the feeling that Epic Games are hinting me that I’m not using this as they wanted me to use it :slight_smile:

My goal in the end is to be able to add the AIPerceptionComponent in the code and configure it in BP…

EDIT:
I also tried providing UPROPERTY for the sight sense config and assigning it to the AIPerceptionComponent but it also didn’t show it in the array…

EDIT #2:
As discussed in the comments with TheKaosSpectrum, there is a workaround here, it is just to add the AIPerceptionComponent directly in the BP and then when using GetPerceptionComponent() function which is part of AIController it will find it and return it… Or… Go full blown C++ like in the link above…

The Main Question:
Why is it working when I add it in the BP but not when it’s inherited because I add it in the C++ code? :
This is more of me being curious now than actually blocked…

Thanks in advance.

I’m not happy about it… But the workaround here is just to to:

AIPerceptionComponent = FindComponentByClass<UAIPerceptionComponent>();

  if (AIPerceptionComponent != nullptr) {
    SetPerceptionComponent(*AIPerceptionComponent);

    GetAIPerceptionComponent()->OnTargetPerceptionUpdated.AddDynamic(this, &AEnemyAIControllerBase::TargetPerceptionUpdated);
  }

In begin play and add the perception component in the BP and that way linking them… BUT… I still don’t understand why I have to do that to use the component? It’s the only component that acted up so far… What am I doing wrong? Am I going against what Epic Games planned for that component? Is it because it’s in AIController that already sort of has perception component in it that gets updated separately? HELP :slight_smile:

you do know it already does that right? there is a PerceptionComponent pointer in AAIController, which gets set using FindComponent.

Yeah, that’s a good point…
that code is not needed as well…

Still don’t understand why when adding from C++ I can’t use the component in BP the same way as if I’m adding it through BP…

I had the same behaviour, i had created it in C++ orginally, and had issues with senses, so just did it in blueprint in the end.

Yup I’m doing the same now because it started to be a waste of time to figure it out… But… this is now more curiosity since I can’t find an answer to it… But oh well, this can be another dead thread that never gets an answer but has a good workaround :slight_smile: