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.