Can't register UAISense_Sight with UAIPerceptionStimuliSourceComponent

I’m trying to register a sight sense with the perception stimuli source component and compiles fine but when I go to my Character and look at the PlayerStimuliSource component, there’s 0 senses registered.

PlayerCharacter.h



UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Player)
class UAIPerceptionStimuliSourceComponent* PlayerStimuliSource;


PlayerCharacter.cpp



PlayerStimuliSource = CreateDefaultSubobject<UAIPerceptionStimuliSourceComponent>(TEXT("PlayerStimuliSource"));
PlayerStimuliSource->RegisterForSense(TSubclassOf<UAISense_Sight>());


Capture.PNG

1 Like

Did you find a solution to this - I have the same problem!

I still have this issue with Unreal 5.0.

Looking at the source code, RegisterForSense does extra-check for things that are not ready in the constructor of the character. Hence it exit the method before registering the Sense.

In the mean time, it seems that the editor (blueprint) has a direct access to the fields, that are protected for the C+±users:

UPROPERTY(EditAnywhere, Category = "AI Perception", BlueprintReadOnly, config)
uint32 bAutoRegisterAsSource : 1;

UPROPERTY(EditAnywhere, Category = "AI Perception", BlueprintReadOnly)
TArray<TSubclassOf<UAISense> > RegisterAsSourceForSenses;

Thus, the only way to workaround this is to create an inherited class:

In UMyAIP30t.h

UCLASS()
class THEPATHTOL0_API UMyAIP30t : public UAIPerceptionStimuliSourceComponent
{
	GENERATED_BODY()

public:
	UMyAIP30t (const FObjectInitializer& ObjectInitializer);

	void RegisterSense(TSubclassOf<UAISense> SenseClass);
};

In UMyAIP30t.cpp

UMyAIP30t::UMyAIP30t(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
	// Was set to false in the UAIPerceptionStimuliSourceComponent constructor.
	bAutoRegisterAsSource = true;
}

void UMyAIP30t::RegisterSense(TSubclassOf<UAISense> SenseClass)
{
	RegisterAsSourceForSenses.AddUnique(SenseClass);
}

YouCharacter constructor:

AIPerceptionStimuliSourceComponent = CreateDefaultSubobject<UMyAI30t>(TEXT("AIPerceptionStimuliSource"));
	AIPerceptionStimuliSourceComponent->RegisterSense(UAISense_Team::StaticClass());
4 Likes