What role does this function play?

Hello!

I’m studying a book titled ‘Learning c++ by Creating Games with UE4’.
However, there is a lack of explanation, so I ask questions.

This is part of the problem.

void AEquipWeapon::OnProx_Implementation(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
{
if (OtherComp != OtherActor->GetRootComponent()){ return; }

if (Swinging && OtherActor != cha_WeaponHolder && !ThingsHit.Contains(OtherActor)){
	OtherActor->TakeDamage(AttackDamage + cha_WeaponHolder->BaseAttackDamage, FDamageEvent(), NULL, this);
	ThingsHit.Add(OtherActor);
}

}

I don’t know what it is. Can you explain it to me in detail?

Thank you for reading poor English

I don’t know the book, but as far as I can tell it seems to process the results from doing a sweep trace.

If the component hit by the trace is not the Root Component of that actor, we do nothing and exit the function:

if (OtherComp != OtherActor->GetRootComponent())
{ 
	return; 
}

Our weapon must be:

  • “Busy Swinging (assuming a boolean)”

  • AND “the actor hit with the trace must not be our owner/wielder (cha_WeaponHolder)” (we don’t want to hit ourself)

  • AND “we shouldn’t have already hit this actor before” (this is probably executed each tick so we don’t want to do damage on each tick - we will only do damage again when enemy gets within hitting distance again I assume)

    if (Swinging && OtherActor != cha_WeaponHolder && !ThingsHit.Contains(OtherActor))

If all the conditions above are met, we do damage to the actor we hit:

OtherActor->TakeDamage(AttackDamage + cha_WeaponHolder->BaseAttackDamage, FDamageEvent(), NULL, this);

And we keep track of the fact that we’ve now hit that OtherActor, so we don’t hit it again:

ThingsHit.Add(OtherActor);

Additionally, function names ending in “_Implementation” are used for BlueprintNative functions to define the actual implementation (you can read more about it here: https://www.epicgames.com/unrealtournament/forums/unreal-tournament-development/ut-development-programming/11600-what-s-the-deal-with-appending-_implementation-to-functions)