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?