AActor::TakeDamage

Apply damage to this actor.

Windows
MacOS
Linux

Override Hierarchy

References

Module

Engine

Header

/Engine/Source/Runtime/Engine/Classes/GameFramework/Actor.h

Include

#include "GameFramework/Actor.h"

Source

/Engine/Source/Runtime/Engine/Private/Actor.cpp

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

Select Skin
Light
Dark

Welcome to the new Unreal Engine 4 Documentation site!

We're working on lots of new features including a feedback system so you can tell us how we are doing. It's not quite ready for use in the wild yet, so head over to the Documentation Feedback forum to tell us about this page or call out any issues you are encountering in the meantime.

We'll be sure to let you know when the new system is up and running.

Post Feedback