Health system as component, interface, or inheritance ?

Hello everyone.

I’m currently on a project and i’m at the point of creating the health system.
I chose to implement health as a component, which i associated with any playable character and the AI.

Problem is, i cannot find a solution when it comes to make (for example) the hero die.
Indeed, i created a boolean variable in the component (bIsDead), but i cannot find anyway to create a death event on call of this variable on the characters which have the component implemented.

So a few questions come to mind :

  • I could use the event tick to check on the state of the variable, but using the event tick on every single pawn posessing the component in the game is probably not a great idea, for obvious reasons.

  • I do not want to have to cast a death function in the component to a specific pawn (the blueprint would be enormous)…

  • I thought about making a function in each character, which would check the value of my variable everytime HP’s are updated, but i cannot find how to call it…

So my questions are : Am i doing something wrong ? Am i missing something ? Would interface be a better choice to manage health and damages ? Would it be wiser to go for inheritance ?

Thanks in advance.

Your setup is not wrong per se. Putting things like health and other stats and skills in components is perfectly fine. It seems to me that your issue is with how you update health.

Am I understanding you correctly that you are looking for a way to fire off an event when you change the Health variable on the component in order to, among other things, check if the character just died? If so, I suspect you currently set the variable directly, i.e.

// some code before
UHealthComponent->Health = 19;
// some code after

If all of that is true, fix it by turning it into a function. The function fires off an event and whenever you want to change the character’s health, you do it by calling this function. Like so:

// some code before
UHealthComponent->SetHealth(19);
// some code after

With the function being implemented like this:

UHealthComponent::SetHealth(int32 NewHealth)
{
    Health = NewHealth;
    OnHealthChanged.Broadcast(NewHealth);
}

With OnHealthChanged being a custom event you define on your component.

1 Like

Components work great for this. Dispatch it.

Problem is, i cannot find a solution when it comes to make (for example) the hero die.

  • component calculates health and broadcasts player’s death:

  • player receives the notification

2 Likes