C++ TakeDamage / Inflict Damage

hi,

anyone can explain me, how my character can TakeDamage from another player and InflictDamage to another player in a FPS game, i’ve created a CharacterClass that can TakeDamage with this Function :

TakeDamage(float Damage, struct FDamageEvent const& DamageEvent, class AController* EventInstigator, class AActor* DamageCauser), but now how can i inflict damage to another player, i’ve also created a ProjectileClass and a WeaponClass.

I’m adding a snippet here from my Exploding Barrel implementation:

PWNExplodingBarrel.h

void Explode();

PWNExplodingBarrel.cpp

void APWNExplodingBarrel::Explode()
{
		TArray<AActor*> ignoredActors;
		// Add ignoredActors if required
		UGameplayStatics::ApplyRadialDamage(GetWorld(), ExplosionDamage, GetActorLocation(), ExplosionRadius, UPWNDamageType_Explosion::StaticClass(), ignoredActors, this, nullptr, false, ECollisionChannel::ECC_Visibility);
}

The params are as follows:

  • GetWorld(): An instance of the game world
  • ExplosionDamage: a float containing the damage amount
  • GetActorLocation(): The world location from which the damage event originates
  • ExplosionRadius: Radius of the explosion/Damage
  • UPWNDamageType_Explosion::StaticClass(): A class reference to a custom class derived from UDamageType, which contains specific params related to explosions
  • ignoredActors: References to any actors that must be ignored for damage is specified here
  • ECollisionChannel::ECC_Visibility: Specifies the collision detection to determine which actors “collide” with the explosion.

So, based on some event “Explosion” in your actor that wants to deal damage, I call a global function “ApplyRadialDamage” which broadcasts this damage event to all objects within a certain radius.
For each of the objects found, it will call their TakeDamage() function.

E.g. then in my Character class cpp:

float APWNPlayerCharacter::TakeDamage(float Damage, FDamageEvent const& DamageEvent, AController* EventInstigator, AActor* DamageCauser)
{
	/***********************/
	/*** Get Hit details ***/
	/***********************/
	FHitResult hitRes;
	FVector impulseDir;
	DamageEvent.GetBestHitInfo(this, DamageCauser, hitRes, impulseDir);
	impulseDir.Normalize();

	/*******************/
	/*** Camera Anim ***/
	/*******************/
	TakingDamageDirection = impulseDir;
	float takingDamagePct = FMath::Min(1.0f, Damage / (Stats->GetTakeDamageCameraAnim_MaxAdjustmentDamage()));
	TakingDamageCameraAnimInterpolator->Init(true, Stats->GetTakeDamageCameraAnim_GoUpDuration(), Stats->GetTakeDamageCameraAnim_GoDownDuration(), 0.0f, takingDamagePct * Stats->GetTakeDamageCameraAnim_MaxOffset(), false, true);
	TakingDamageCameraAnimInterpolator->GoUp();
	/*******************/

	return Super::TakeDamage(Damage, DamageEvent, EventInstigator, DamageCauser);
}

The first part is mainly relevant to you - here I get some details about the damage event that was received, and then you can use that to do what you require. In my case I play a camera shake animation.

Please let me know if anything is unclear and I’ll add some more details.

if somebody wonders what is the signature for TakeDamage(), here is what you put in your .h file.

protected:
	virtual float TakeDamage(float Damage, FDamageEvent const& DamageEvent, AController* EventInstigator, AActor* DamageCauser) override;

also for the sake of god and Tim.S , please don’t type “struct” and “class” before function variable types

like “struct FDamageEvent const”, it is legacy C code. it just makes it hard to read without any advantage in 2017.