How do you create a delegate to add to PerceptionComponent->OnPerceptionUpdated

Hi,

I looked around and found a couple really useful threads about how to set up AI Perception in c++. The one thing im getting stuck on however is creating my own function to get called with OnPerceptionUpdated. What I have now has no errors it just isn’t calling my debug message when I enter into my AI’s sight perception. So I assume that My SenseUpdated function is not being called when you would expect it should be.

Heres the .h

UCLASS()
class PW_GAME_API APWAIController : public AAIController
{
	GENERATED_BODY()

	APWAIController(const FObjectInitializer& ObjectInitializer);

	UAISenseConfig_Sight* sightConfig;

	TScriptDelegate<> sense;

public:
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AI")
	float agroRange;

	UFUNCTION(Category = "Perception")
	void SenseUpdated(TArray<AActor*>& testActors);
};

and heres the .cpp

APWAIController::APWAIController(const FObjectInitializer& ObjectInitializer)
	:Super(ObjectInitializer.SetDefaultSubobjectClass<UPWPathFollowingComponent>(TEXT("PathFollowingComponent")))
{
	agroRange = 500;

	PerceptionComponent = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("Perception Component"));
	sightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("Sight Config"));

	PerceptionComponent->ConfigureSense(*sightConfig);
	PerceptionComponent->SetDominantSense(sightConfig->GetSenseImplementation());

	sightConfig->SightRadius = agroRange;
	sightConfig->LoseSightRadius = agroRange + 50;
	sightConfig->DetectionByAffiliation.bDetectEnemies = true;
	sightConfig->DetectionByAffiliation.bDetectNeutrals = true;
	sightConfig->DetectionByAffiliation.bDetectFriendlies = true;

	sense.BindUFunction(this,"SenseUpdated");

	PerceptionComponent->OnPerceptionUpdated.Add(sense);
}

void APWAIController::SenseUpdated(TArray<AActor*>& Actors)
{
	GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Blue, "Perception Sense Updated");
}

I Fixed it, kinda silly however I shall post the answer all the same. I did not realize that AddDynamic was a macro and my Intellesense was not showing it therefore I did not think It existed. Once I figured out I could use it, it made things really easy. here’s the updated code (Just the cpp):

APWAIController::APWAIController(const FObjectInitializer& ObjectInitializer)
	:Super(ObjectInitializer.SetDefaultSubobjectClass<UPWPathFollowingComponent>(TEXT("PW PathFollowingComponent")))
{
	PerceptionComponent = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("Perception Component"));
	sightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("Sight Config"));

	PerceptionComponent->ConfigureSense(*sightConfig);
	PerceptionComponent->SetDominantSense(sightConfig->GetSenseImplementation());

	sightConfig->SightRadius = agroRange;
	sightConfig->LoseSightRadius = agroRange + 50;
	sightConfig->DetectionByAffiliation.bDetectEnemies = true;
	sightConfig->DetectionByAffiliation.bDetectNeutrals = true;
	sightConfig->DetectionByAffiliation.bDetectFriendlies = true;

	PerceptionComponent->OnPerceptionUpdated.AddDynamic(this, &APWAIController::SenseUpdated);
}

void APWAIController::SenseUpdated(TArray<AActor*> Actors)
{
	GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Blue, "Perception Sense Updated");
}
1 Like