Damage system based on components

Hello! I’m implementing a damage-system but I’m not sure about the structure that I’ve in mind.

Briefly, I’m creating a component (DamageComponent) that is responsible to process the damage received depending on some internal factors. The component is also handling a parameter called **Health **and when it process damage it decreases its value (I know this is a questionable choice… but at the end of the day **Health **and **Damages **are so strictly related that I can’t think of a different place for this param).

Now I can apply this component to any actor that needs to have health and receive damage. The function that processes damage is triggered by the** TakeDamage/ApplyDamage** functions provided by Unreal and here comes my doubt… I can’t find a way to have a completely independent component since to trigger the **ProcessDamage **function on my component I need to override the **TakeDamage **function on each actor that I want to attach my **DamageComponent **to. That means that I’m somehow constrained to an inheritance system (I will probably implement a base class with a default implementation for TakeDamage).

DamageComp.png

What do you think about this implementation? Have you got any suggestion to improve this architecture?

Since the internal application of damage will vary by object class, an alternative approach might be to simply use a defined interface to apply the damage.

Each class’s implementation of ReceiveDamage can be class specific, but there is no coupling between sender and receiver.

When sending damage, it is often useful to pass additional info such as damage type, amount, hit information, bone name, etc…

And while the built-in damage system covers many of these bases, I often find it better to roll your own.

Couldn’t you just not use the TakeDamage function provided by UE4? Create your own TakeDamage function on the component and call it when needed. I might be missing the point here, but I don’t understand why you need to use UE4’s TakeDamage. Are there any benefits for using UE4’s TakeDamage function instead of a “custom” one?

@ItsaMeTuni actually I’m just trying to not reinvent the wheel. This feature is there and it is well integrated with the engine. Why should I create the same logic again?

2 Likes

@OptimisticMonkey

Yes I do like the interface logic. I’ll review my current implementation. At the moment I’ve implemented inside the BeginPlay method an handler for the OnTAkeAnyDamage called on the component owner:



void UDamageComponent::BeginPlay()
{
    Super::BeginPlay();

    AActor *Owner = GetOwner();

    if (Owner != nullptr) {
        Owner->OnTakeAnyDamage.AddDynamic(this, &UDamageComponent::HandleTakeAnyDamage);
    }

}