Where does OnHit function getting all the data?

I can’t figure out how OtherActor, OtherComp which is custom made variables contain exact data we need.

Is OnComponentHit passing it to OnHit?
Where can I find the details?

From where the actor detects a hit and broadcasts that delegate.

https://github.com/EpicGames/UnrealEngine/blob/release/Engine/Source/Runtime/Engine/Private/Actor.cpp#L2927

void AActor::InternalDispatchBlockingHit(UPrimitiveComponent* MyComp, UPrimitiveComponent* OtherComp, bool bSelfMoved, FHitResult const& Hit)
{
	check(MyComp);

	if (OtherComp != nullptr)
	{
		AActor* OtherActor = OtherComp->GetOwner();

		// Call virtual
		if(IsActorValidToNotify(this))
		{
			NotifyHit(MyComp, OtherActor, OtherComp, bSelfMoved, Hit.ImpactPoint, Hit.ImpactNormal, FVector(0,0,0), Hit);
		}

		// If we are still ok, call delegate on actor
		if(IsActorValidToNotify(this))
		{
			OnActorHit.Broadcast(this, OtherActor, FVector(0,0,0), Hit);
		}

		// If component is still alive, call delegate on component
		if(IsValidChecked(MyComp))
		{
			MyComp->OnComponentHit.Broadcast(MyComp, OtherActor, OtherComp, FVector(0,0,0), Hit);
		}
	}
}

I’m also trying to figure out what is going on. I get event listeners some and I’m going to try and explain it. Dan, if you would, help me out a little.

So we have this code,

BulletMesh->OnComponentHit.AddDynamic(this, &AProjectile::OnHit);

void AProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)

As Dan pointed out, OnComponentHit is defined in Actor.cpp as

MyComp->OnComponentHit.Broadcast(MyComp, OtherActor, OtherComp, FVector(0,0,0), Hit);

Which explains where our AProjectile::OnHit got it’s parameters from. Stephen just matched up what was in Actor.cpp. Also note that there is a Broadcast.


we can see in this image that the arguments from OnComponentHit were passed into OnHit


AddDynamic from Delegate.h:

/**
 * Helper macro to bind a UObject instance and a member UFUNCTION to a dynamic multi-cast delegate.
 *
 * @param	UserObject		UObject instance
 * @param	FuncName		Function pointer to member UFUNCTION, usually in form &UClassName::FunctionName
 */
#define AddDynamic( UserObject, FuncName ) __Internal_AddDynamic( UserObject, FuncName, STATIC_FUNCTION_FNAME( TEXT( #FuncName ) ) )

This is the part I’m most unsure about: AddDynamic is a macro that allows us to bind a “dynamic delegate” to an event such as OnComponentHit. OnComponentHit is raised during a collision. AddDynamic takes 2 params: 1.the object listening for the event, ProjectileMesh and 2.a method, OnHit that should be called when the event is raised. The second param seems to be called a dynamic function pointer. Runtime dynamic as opposed to a compile time static delegates like binding axis input. OnComponentHit is a delegate, it is broadcasting, and is bound to our OnHit method with the AddDynamic macro.
for example:

ButtonWidget->OnClicked.AddDynamic(this, &AMyActor::ButtonClicked);

ButtonClicked will get all the same arguments as OnClicked.

I’m not sure if all that’s 100% true.

If I was making plain cpp objects, I don’t understand how to make my own custom delegates and AddDynamic macro. Not that I expect anyone to explain it to me.

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms