x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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

eader:

  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?

Product Version: UE 4.10
Tags:
more ▼

asked May 05 '16 at 01:40 PM in C++ Programming

avatar image

onewho
1

avatar image haimat Jun 01 '16 at 11:12 PM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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);
more ▼

answered Jun 02 '16 at 12:04 AM

avatar image

Nachtmahr
7k 143 28 172

avatar image haimat Jun 02 '16 at 12:07 AM

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

avatar image Nachtmahr Jun 02 '16 at 12:15 AM

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 ;P if you look closly it tells you there is another Parameter missing. Or in his case a total mismatch.

avatar image haimat Jun 02 '16 at 12:17 AM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question