OnHearNoise Delegate Use?

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!

Hi ,

It seems your OnHearNoise function has different signature than the one expected by UPawnSensingComponent::OnHearNoise, namely the second parameter should be passed by reference, not by value. I checked it in UE4’s latest sources so let me know if it’s not the case in your sources.

Cheers,

–mieszko

Yep. That was it. One little ampersand.

Thanks so much. I probably would’ve stared at that for hours without realizing.