C++ Collision detection UE4.7

I was wondering how to deal with collisions in UE4.7. I looked around and found a lot about using OnActorHit.AddDynamic() to add a delegate function.

This does not seem to exist now unless I am really wrong.

this other answer gives a idea of what I have tried.

Hello, Xenetics

You can bind AddDynamic() function like this (inside Custom Actor’s constructor):

this->OnActorHit.AddDynamic(this, &ACustomActor::OnHit);

Please don’t forget to define the function in .h file of your Actor:

UFUNCTION(/*custom parameters*/)
void OnHit(AActor* SelfActor, AActor* OtherActor, FVector NormalImpulse, const FHitResult& Hit);

Hope this helped!

Good luck!

1 Like

Thanks. this sheds some light on it

Hi.
Is delegate binding in constructors standard practice? The reason I ask is that the first time I made use of delegates (binding to collision OnOverlap) I ran into a very awkward bug that was due to binding in the constructor. The issue, as far as I could work out, was as follows.

Delegate bindings are, it seems, serialized. I created a blueprint based off my class, therefore since the binding was done in the constructor (as opposed to, for example, BeginPlay) this was serialized with the blueprint’s CDO. I subsequently modified the binding in my c++ class, changing it to bind to a new method and deleting the initial method from the class entirely. My overlap event never fired, and stepping through the code I found that the delegate invocation list for spawned objects of my blueprint type still retained the original binding to the now-removed method. I recreated the blueprint and the issue disappeared.

So, I’m now thinking that if binding in constructors is intended usage, then there is perhaps a bug in the engine code dealing with serialized delegates, since it seems to me that changing delegate bindings is not an unreasonable thing to expect to be able to do.

Cheers.

1 Like