Registering Actor Colliding with landscape

Hello, I’ve been working on creating NPC Ai that randomly moves around the environment but avoids obstacles. I’ve set up object avoidance to use an interaction sphere around the actor, when the sphere overlaps with another static mesh it triggers an “OnComponentBeginOverlap” event. This works great for most things, but Landscapes don’t have the option to generate component overlap events. My solution was to get my actor to generate an “OnComponentHit” event to register when the actor’s interaction sphere collides with a landscape.

My code for my overlap events work:

Header:

UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = Interaction, meta = (AllowPrivateAccess = "true"))
	class USphereComponent* InteractionSphere;

UFUNCTION()
void OnBeginOverlap(AActor* otherActor, UPrimitiveComponent* otherComp, int32 otherBodyIndex, bool bFromSweep, const FHitResult& sweepResult);

 UFUNCTION()
 void OnEndOverlap(AActor* otherActor, UPrimitiveComponent* otherComp, int32 otherBodyIndex);

.cpp:

ANPC::ANPC(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
base = ObjectInitializer.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("ActorMesh"));
	RootComponent = base;

	// Interaction sphere
	InteractionSphere = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("InteractionSphere"));
	InteractionSphere->SetSphereRadius(10);
	InteractionSphere->AttachTo(RootComponent);
	InteractionSphere->OnComponentBeginOverlap.AddDynamic(this, &ANPC::OnBeginOverlap);
    InteractionSphere->OnComponentEndOverlap.AddDynamic(this, &ANPC::OnEndOverlap);
}

void ANPC::OnBeginOverlap(AActor* otherActor, UPrimitiveComponent* otherComp, int32 otherBodyIndex, bool bFromSweep, const FHitResult& sweepResult)
{
//Start Avoiding
}

void ANPC::OnEndOverlap(AActor* otherActor, UPrimitiveComponent* otherComp, int32 otherBodyIndex)
{
//Stop Avoiding
}

However when I try to write an OnComponentHit event using the same logic, it fails to compile. I’ve tried two different methods shown below:

#1
Header:

 UFUNCTION()
 void OnHit(AActor* otherActor, UPrimitiveComponent* otherComp, int32 otherBodyIndex, bool bFromSweep, const FHitResult& sweepResult);

.cpp

 //Same initialization logic for interaction sphere as before
InteractionSphere->OnComponentHit.AddDynamic(this, &ANPC::OnHit);

void ANPC::OnHit(AActor* otherActor, UPrimitiveComponent* otherComp, int32 otherBodyIndex, bool bFromSweep, const FHitResult& sweepResult)
{
//Hit event logic
}

#2

Header:

UFUNCTION()
		void OnHit(AActor* otherActor, UPrimitiveComponent* otherComp, int32 otherBodyIndex);

.cpp

//Same initialization logic for interaction sphere as before
InteractionSphere->OnComponentHit.AddDynamic(this, &ANPC::OnHit);

void ANPC::OnHit(AActor* otherActor, UPrimitiveComponent* otherComp, int32 otherBodyIndex)
{
//Hit event logic
}

I always end up with the same error:

Error C:\Users\NCY\Documents\UnrealProject\Source\Private\NPC.cpp(39) : error C2664: ‘void TBaseDynamicMulticastDelegate::__Internal_AddDynamic(UserClass ,void (__cdecl ANPC:: )(AActor *,UPrimitiveComponent *,FVector,const FHitResult &),const TCHAR )’: cannot convert argument 2 from 'void (__cdecl ANPC:: )(AActor *,UPrimitiveComponent ,int32,bool,const FHitResult &)’ to 'void (__cdecl ANPC:: )(AActor *,UPrimitiveComponent *,FVector,const FHitResult &)’
Info with
Info [
Info UserClass=ANPC
Info ]
Error C:\Users\NCY\Documents\UnrealProject\Source\Private\NPC.cpp(39) : note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

What am I doing wrong?

I have the same issue. Have you found a solution yet?

Your OnHit does not have the same Signature as OnOverlap!

for versions bellow 4.12 use

UFUNCTION()
void OnOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
    
UFUNCTION()
void OnHit(class AActor* act, class UPrimitiveComponent* Other, FVector Impulse, const FHitResult & HitResult);

for 4.12 and above

UFUNCTION()
void OnOverlap(class UPrimitiveComponent* OverlappingComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
    
UFUNCTION()
void OnHit(class UPrimitiveComponent* HitComp, class AActor* Actor, class UPrimitiveComponent* Other, FVector Impulse, const FHitResult & HitResult);

Exactly, thanks. I have missed that 4.12 change. Is it mentioned somewhere in the release notes?

Uhm I did not read it fully yet so not sure. Im the kind of guy who looks up the sourcecode or reads the error mesaages :stuck_out_tongue_winking_eye: if you look closly it tells you there is another Parameter missing. Or in his case a total mismatch.

You’re right. Thanks, that solved the issue for me.