Download

Need help getting OnActorHit to work

Hey, so I haven’t been able to get the OnActorHit function to fire. Currently I am trying to add it to the generated FPS C++ Character class.

A forum posting suggested to try something like: (in the .cpp file)




OnActorHit.AddDynamic(this, &MyCharacter::OnMyActorHit);


void MyCharacter::OnMyActorHit(AActor* SelfActor, AActor* OtherActor, FVector NormalImpulse, const FHitResult& Hit) {
	UE_LOG(LogTemp, Warning, TEXT("OnMyActorHit worked!"));
}


(This doesn’t work as OnActorHit isn’t defined [and I’m not sure where it should be defined]).

However looking at the API I feel like I should be able to override OnActorHit, but that didn’t work either (it said something along the lines of “there is no method to override”). If anyone has been able to get it working I would love to see how you implemented it. Another suggestion was to use begin/end overlap event functions, but I would prefer to use the blocking movement collision event provided by OnActorHit.

Thanks!

Shouldn’t it be ReceiveHit?



	virtual void ReceiveHit
	(
		class UPrimitiveComponent * MyComp,
		class AActor * Other,
		class UPrimitiveComponent * OtherComp,
		bool bSelfMoved,
		FVector HitLocation,
		FVector HitNormal,
		FVector NormalImpulse,
		const FHitResult & Hit
	) OVERRIDE;


And don’t forget SetActorEnableCollision(true);

That worked! Well, I guess I found OnActorHit first and ran down that rabbit hole without looking at other available functions. But while we are on the subject, what is the difference between OnActorHit and ReceiveHit and why was I not able to override OnActorHit? I’m still in the early phases of familiarizing myself with the codebase (and am fairly new to C++).

Thanks again!

OnActorHit is a ‘delegate’ (aka Event Dispatcher) which allows other objects to register with the actor for notification of that event. For example, the Level Blueprint might use that to be notified when that actor is hit. Delegates are also very useful for event notifications from components. If a subclass wants a notification, you override the virtual ReceiveHit function instead. Most events have both a virtual function and delegate, so that subclasses and other objects can be notified.

Update note: After 4.8+ DON’T use ReceiveHit but use NotifyHit. Also, since it is not VIRTUAL anymore, you need to call Super::NotifyHit(); inside the function so that Blueprints still work over your class.