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"

Why is OnActorHit not calling my function?

I have looked at all the most common answers of how to setup to use OnActorHit and tried all of them. I can't seem to get OnActorHit to fire off correctly. I don't get any errors with adding a delegate through the .Add function or using the .AddDynamic function. However, I have been following this tutorial where he uses Event Begin Overlap in a Blueprint and set the Collision Preset to Trigger. I was able to replicate that functionality in C++. However, I decided I want to use OnActorHit instead of overlapping. I can't seem to get OnActorHit to work but I can get OnActorBeginOverlap. Does anyone know enough about OnActorHit as to why this happens? Here is my code below that I have tried for attempting to use OnActorHit.

APongActor Constructor

 APongActor::APongActor()
 {
      ...
     BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("RootComponent"));
     RootComponent = BoxComponent;
     BoxComponent->InitBoxExtent(FVector(100.0f, 100.0f, 100.0f));
     BoxComponent->SetCollisionProfileName(TEXT("PhysicsActor"));
     ...
     BoxComponent->SetSimulatePhysics(true);
     BoxComponent->SetEnableGravity(false);
     BoxComponent->SetNotifyRigidBodyCollision(true);
 
     FScriptDelegate Delegate;
     Delegate.BindUFunction(this, "OnPongActorHit");
     OnActorHit.Add(Delegate);
 }

My Hit Function:

 void APongActor::OnPongActorHit() {
     GEngine->AddOnScreenDebugMessage(-1, 5.0, FColor::Green, "OnHit() called");
     ...
     
 }

Header file declaration:

 UFUNCTION()
         void OnPongActorHit();


I tried the Add Dynamic function too, but couldn't get it too work

 .H FILE
 
 UFUNCTION(AActor * SelfActor, AActor * OtherActor, FVector NormalImpulse, struct FHitResult Hit)
         void OnPongActorHit();
 
 .CPP FILE
 
 APongActor::APongActor() {
 ... code ...
 OnActorHit.AddDynamic(this, &APongActor::OnPongActorHit);
 }
 
 APongActor::OnPongActorHit(AActor * SelfActor, class AActor * OtherActor, FVector NormalImpulse, struct FHitResult Hit)
 { ... code ... }


Product Version: UE 4.9
Tags:
more ▼

asked Nov 11 '15 at 04:11 AM in C++ Programming

avatar image

ddtapia
43 5 8 11

avatar image ddtapia Nov 12 '15 at 04:34 AM

Okay wait nevermind, my editor was not updating correctly to my code changes.

I am back to the scenario where only OnComponentBeginOverlap works in the constructor and the collision profile name is Trigger.

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

2 answers: sort voted first

The collision is likely set to ECollisionEnabled::QueryOnly. In your constructor, try setting it to ECollisionEnabled::PhysicsOnly or ECollisionEnabled::QueryAndPhysics. To do this:

 BoxComponent->SetCollisionEnabled(ECollisionEnabled::PhysicsOnly);

or

 BoxComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);

Here is a link to ECollisionEnabled::Type

Either that or you may not have your collision response setup correctly. Here is a link to that: Collision Response Reference.

more ▼

answered Nov 11 '15 at 02:37 PM

avatar image

MJ_01
822 38 39 241

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

Ok then i repost it, you said overlap worked for you so i was scared that i might be wrong and deleted my anwser, sorry:

Don't bind in constructor, what you actully did is binding of class default object (CDO) to event. Everything you do in constructor in UObject based class is used exclusivly to create master copy of object of specific class (previuesly mentioned CDO), thats why if you do something wrong in constructor it crashes engine on start when CDO is created, not where object is spawned. Insted of constructor use BeginPlay() event (or PostInitProperties() in non-Actor UObjects), you are 100% sure at that point that your actor is ready to go. In constructor you only set default varables and setup components, everything related to your class not future object that gonna be created from it.

more ▼

answered Nov 12 '15 at 03:27 AM

avatar image

Shadowriver
37.2k 936 172 1116

avatar image ddtapia Nov 15 '15 at 02:15 AM

Sorry I was mistaken, i am back to only having OnActorBeginOverlap bind to my function in the constructor.

avatar image ddtapia Mar 25 '16 at 07:36 PM

After some research, I was able to use this in my constructor. In fact using this in the constructor is the only recommended way I found that actually works.

 BoxComponent->OnComponentHit.AddDynamic(this, &APongActor::OnPongActorHit);
(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