CPP On Component Hit doesn't fire

As in title, my code is:
In construcor:

Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
Mesh->SetCollisionResponseToAllChannels(ECR_Ignore);
Mesh->SetCollisionObjectType(ECC_Vehicle);
Mesh->SetCollisionResponseToChannel(ECC_WorldStatic, ECR_Block);
Mesh->SetCollisionResponseToChannel(ECC_WorldDynamic, ECR_Block);
Mesh->SetCollisionResponseToChannel(ECC_Pawn, ECR_Block);
Mesh->SetNotifyRigidBodyCollision(true);
Mesh->OnComponentHit.AddDynamic(this, &AThrowableProjectile::OnHit);
RootComponent = Mesh;
Mesh->SetSimulatePhysics(true);

In OnHit:

void AThrowableProjectile::OnHit(AActor * OtherActor, UPrimitiveComponent * OtherComp, FVector NormalImpulse, const FHitResult & Hit)
{
	GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Red, FString::FString("BOP"));
}

Also physics simulation doesn’t work if I spawn it from abse class, must spawn blueprint derivative

Hey -

This was a known issue in 4.11 that appears to have been fixed in 4.12. If your bound function is still not being triggered in the latest version, feel free to comment here and we will investigate further.

Cheers

I have converted my project o 4.12 but now every OnComponentHit/Overlap throws error when building

error C2664: 'void TBaseDynamicMulticastDelegate<FWeakObjectPtr,void,UPrimitiveComponent *,AActor *,UPrimitiveComponent *,FVector,const FHitResult &>::__Internal_AddDynamic<AGameProjectile>(UserClass *,void (__cdecl AGameProjectile::* )(UPrimitiveComponent *,AActor *,UPrimitiveComponent *,FVector,const FHitResult &),FName)': cannot convert argument 2 from 'void (__cdecl AGameProjectile::* )(AActor *,UPrimitiveComponent *,FVector,const FHitResult &)' to 'void (__cdecl AGameProjectile::* )(UPrimitiveComponent *,AActor *,UPrimitiveComponent *,FVector,const FHitResult &)'
2>          with
2>          [
2>              UserClass=AGameProjectile
2>          ]

Ok, I found the changed signature (add UPrimitiveComponent* OverlappedComponent as first parameter)

Hey, I finished moving my project to 4.12 but the event still doesn’t fire

Can you check that both the component with the dynamic binding and the actor/component that is hitting it both have “Generate Hit Events” set to true? I just created an actor class with this code in the Third Person Template. When I test in editor, running into the cube mesh with the default character triggered the bound function as expected.

They both have, the event is firing when added through blueprints, just not for cpp. Tried also adding OnActorHit in cpp, doesn’t work either

Just want to add something you may have overlooked,

but your mesh collision should be set to OverlapAll (or other overlaps) and the other components should be BlockAll (or other blocks) with both generating overlap events.

If both have the overlap or both have block collisions, it won’t work.

It works for block/block for blueprint, is it a thing with cpp specifically? Or are you talking about overlap events?

Are you able to reproduce the issue in a new project and, if so, could you provide the project for me to see your setup and the issue locally? Otherwise could you provide a copy of your current project where the issue does exists? If you can zip the project, you can either attach it to a comment here or upload it and send me a download link on the forums for privacy.

Looking at the source code, I think you want this in 4.12…

UFUNCTION()
void OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit);

and then…

OnComponentHit.AddDynamic(this, &UTankTrack::OnHit);

with the delegate (note the parameters have changes since 4.11)…

void UTankTrack::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit)
{
	UE_LOG(LogTemp, Warning, TEXT("I'm hit, I'm hit"))
}

Here’s the slide I produced for it if it’s clearer…

1 Like

Hey, I have updated the signature , it just doesn’t fire. I moved it to Blueprints since, but thanks!

Also there might be a mistake in either slide of the code ( you have UPROPERTY in slide and UFUNCTION in code) :slight_smile:

Hi,
This is just an add on to the awesome answer from Ben. I was also struggling with OnHit function not responding even after I had set generated Hit events true. The one thing missing was UFUNCTION() before the OnHit function… Again thanks Ben. I am a student at your Udemy course… Keep up the good work…