AddDynamic not working with OnPerceptionUpdated

Add dynamic is not working with OnPerceptionUpdated, it throws weird build error I can’t figure out


Kindly point out problem and suggest solution

The .h File:


#pragma once

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

class UAISenseConfig_Sight;
class UAISenseConfig_Hearing;
class UAIPerceptionComponent;

/**
 * 
 */
UCLASS()
class MYPROJECT_API AZombieAIController : public AAIController
{
	GENERATED_BODY()

	AZombieAIController();
	void BeginPlay() override;

public:
	UAIPerceptionComponent* AIPerceptioComponent;
	UAISenseConfig_Sight* Sense_Sight;
	UAISenseConfig_Hearing* Sense_Hearing;

	UFUNCTION()
	void OnPlayerDetected(TArray<AActor*>DetectedPawn);
};

The .cpp file


#include "ZombieAIController.h"
#include "Perception/AIPerceptionComponent.h"
#include "Perception/AISenseConfig_Sight.h"
#include "Perception/AISenseConfig_Hearing.h"

AZombieAIController::AZombieAIController()
{
	PrimaryActorTick.bCanEverTick = false;

	Sense_Sight = CreateDefaultSubobject<UAISenseConfig_Sight>(FName("Sense_Sight"));
	Sense_Hearing = CreateDefaultSubobject<UAISenseConfig_Hearing>(FName("Sense_Hearing"));
	AIPerceptioComponent = CreateDefaultSubobject<UAIPerceptionComponent>(FName("AIPerceptioComponent"));

	Sense_Sight->SightRadius = 3000;
	Sense_Sight->LoseSightRadius = 3500;
	Sense_Sight->PeripheralVisionAngleDegrees = 180;
	Sense_Sight->DetectionByAffiliation.bDetectEnemies = true;
	Sense_Sight->DetectionByAffiliation.bDetectFriendlies = true;
	Sense_Sight->DetectionByAffiliation.bDetectNeutrals = true;
	Sense_Sight->AutoSuccessRangeFromLastSeenLocation = 1500;

	Sense_Hearing->HearingRange = 3000;
	AIPerceptioComponent->ConfigureSense(*Sense_Sight);
	AIPerceptioComponent->ConfigureSense(*Sense_Hearing);

	AIPerceptioComponent->OnPerceptionUpdated.AddDynamic(this, &AZombieAIController::OnPlayerDetected);
}

void AZombieAIController::BeginPlay() {
	Super::BeginPlay();
}

void AZombieAIController::OnPlayerDetected(TArray<AActor*>DetectedPawn)
{
}

VS error Log

>F:\New folder\MyProject\Source\MyProject\ZombieAIController.cpp(29): error C2664: 'void TBaseDynamicMulticastDelegate<FWeakObjectPtr,void,const TArray<AActor *,FDefaultAllocator> &>::__Internal_AddDynamic<AZombieAIController>(UserClass *,void (__cdecl AZombieAIController::* )(const TArray<AActor *,FDefaultAllocator> &),FName)': cannot convert argument 2 from 'void (__cdecl AZombieAIController::* )(TArray<AActor *,FDefaultAllocator>)' to 'void (__cdecl AZombieAIController::* )(const TArray<AActor *,FDefaultAllocator> &)'
1>          with
1>          [
1>              UserClass=AZombieAIController
1>          ]
1>  F:\New folder\MyProject\Source\MyProject\ZombieAIController.cpp(29): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>  C:\Program Files\Epic Games\UE_4.26\Engine\Source\Runtime\Core\Public\Delegates/DelegateSignatureImpl.inl(1101): note: see declaration of 'TBaseDynamicMulticastDelegate<FWeakObjectPtr,void,const TArray<AActor *,FDefaultAllocator> &>::__Internal_AddDynamic'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.MakeFile.Targets(46,5): error MSB3073: The command ""C:\Program Files\Epic Games\UE_4.26\Engine\Build\BatchFiles\Build.bat" MyProjectEditor Win64 Development -Project="F:\New folder\MyProject\MyProject.uproject" -WaitMutex -FromMsBuild" exited with code 6.
1>Done building project "MyProject.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

UE4 Error log

   F:\New folder\MyProject\Source\MyProject\ZombieAIController.cpp(36) : error C2664: 'void TBaseDynamicMulticastDelegate<FWeakObjectPtr,void,const TArray<AActor *,FDefaultAllocator> &>::__Internal_AddDynamic<AZombieAIController>(UserClass *,void (__cdecl AZombieAIController::* )(const TArray<AActor *,FDefaultAllocator> &),FName)': cannot convert argument 2 from 'void (__cdecl AZombieAIController::* )(TArray<AActor *,FDefaultAllocator>)' to 'void (__cdecl AZombieAIController::* )(const TArray<AActor *,FDefaultAllocator> &)'
          with
          [
          ]
              UserClass=AZombieAIController

Hello! Your signature for method is incorrect, just take a look at defenition

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FPerceptionUpdatedDelegate, const TArray<AActor*>&, UpdatedActors);

The problem was with function parameters that was to be called OnPerceptionUpdated, they should be as follows:

UFUNCTION()
	void OnPlayerDetected(const TArray<AActor*>&DetectedPawn);
3 Likes