I’ve added a UPawnSensingComponent
to my ACharacter
subclass:
/** UPawnSensingComponent provides built-in functionality for detecting other pawns by sight and sound */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Awareness)
TSubobjectPtr<class UPawnSensingComponent> PawnSensor;
and I initialize it in my constructor, like so:
PawnSensor = PCIP.CreateDefaultSubobject<UPawnSensingComponent>(this, TEXT("Pawn Sensor"));
PawnSensor->SensingInterval = .1f; // 10 a second
PawnSensor->bOnlySensePlayers = false;
I created two methods for the component’s two delegates:
void AEnemyCharacter::OnHearNoise(APawn *OtherPawn, const FVector Location, float Volume)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Heard an Actor"));
}
void AEnemyCharacter::OnSeePawn(APawn *OtherPawn)
{
FString name = TEXT("Saw Actor ") + OtherPawn->GetName();
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, name);
}
And then, in PostInitializeComponents(), I try to register for the OnHearNoise and OnSeePawn delegate methods
void AEnemyCharacter::PostInitializeComponents()
{
Super::PostInitializeComponents();
PawnSensor->OnSeePawn.AddDynamic(this, &AEnemyCharacter::OnSeePawn);
//PawnSensor->OnHearNoise.Add(&AEnemyCharacter::OnHearNoise);
//PawnSensor->OnHearNoise.AddDynamic(this, &AEnemyCharacter::OnHearNoise);
}
The OnSeePawn
delegate accepts AddDynamic()
and everything is brilliant. But, I can’t for the life of me figure out how to register my OnHearNoise()
function with the OnHearNoise
delegate. The compiler won’t accept Add or AddDynamic calls against it.
What is the correct way to do this? Thanks!