AActor::TakeDamage

Override Hierarchy

Syntax

virtual float TakeDamage
(
    float DamageAmount,
    struct FDamageEvent const & DamageEvent,
    class AController * EventInstigator,
    AActor * DamageCauser
)

Remarks

Apply damage to this actor.

Returns

The amount of damage actually applied.

Parameters

Parameter

Description

DamageAmount

How much damage to apply

DamageEvent

Data package that fully describes the damage received.

EventInstigator

The Controller responsible for the damage.

DamageCauser

The Actor that directly caused the damage (e.g. the projectile that exploded, the rock that landed on you)

Example Code

MyActor.cpp at line 69:

// This is the actor damage handler.   
float AMyActor::TakeDamage(float Damage, struct FDamageEvent const& DamageEvent, class AController* EventInstigator, class AActor* DamageCauser)  
{  
    // Call the base class - this will tell us how much damage to apply  
    const float ActualDamage = Super::TakeDamage(Damage, DamageEvent, EventInstigator, DamageCauser);     
    if (ActualDamage > 0.f)  
    {  
        Health -= ActualDamage;  
        // If the damage depletes our health set our lifespan to zero - which will destroy the actor  
        if (Health <= 0.f)  
        {  
            SetLifeSpan(0.001f);  
        }     
    }  

    return ActualDamage;  
}  

MyCharacter.cpp at line 260:

// This is called to inflict the damage  
void AMyCharacter::InflictDamage()  
{  
    APlayerController* PlayerController = Cast<APlayerController>(GetController());  
    if (PlayerController != nullptr)  
    {  
        // Perform a trace @See LineTraceSingle  
        FHitResult TraceResult(ForceInit);  
        TraceHitForward(PlayerController, TraceResult);  

        // If the trace return an actor, inflict some damage to that actor  
        AActor* ImpactActor = TraceResult.GetActor();  
        if ((ImpactActor != nullptr) && (ImpactActor != this))  
        {  
            // Create a damage event  
            TSubclassOf<UDamageType> const ValidDamageTypeClass = TSubclassOf<UDamageType>(UDamageType::StaticClass());  
            FDamageEvent DamageEvent(ValidDamageTypeClass);  

            const float DamageAmount = 60.0f;  
            ImpactActor->TakeDamage(DamageAmount, DamageEvent, PlayerController, this);  
        }  
    }  
}  

See Also:

  1. [](LineTraceSingle)

See Also

References

Module

Engine

Header

Runtime/Engine/Classes/GameFramework/Actor.h

Source

Runtime/Engine/Private/Actor.cpp