Extra checks in SetCurrentHealth

This commit is contained in:
baz 2025-08-06 21:35:25 +01:00
parent 49fab1bf38
commit d93127a74e
2 changed files with 21 additions and 4 deletions

View File

@ -59,12 +59,26 @@ float UHealthComponent::GetCurrentHealth()
void UHealthComponent::SetCurrentHealth(float Value)
{
CurrentHealth = Value;
if (CurrentHealth > MaxHealth)
if (Value > MaxHealth)
{
CurrentHealth = MaxHealth;
}
else if (Value <= 0.0f)
{
IsDead = true;
OnDeath.Broadcast({GetOwner(), CurrentHealth - Value, nullptr, nullptr, nullptr});
CurrentHealth = 0.0f;
}
else
{
if (Value < CurrentHealth)
{
OnDamaged.Broadcast({GetOwner(), CurrentHealth - Value, nullptr, nullptr, nullptr});
}
CurrentHealth = Value;
}
}
void UHealthComponent::ResetHealth()
@ -75,8 +89,10 @@ void UHealthComponent::ResetHealth()
void UHealthComponent::RecoverHealth(float value)
{
// TODO: We might want to add some extra checking here
if (value > 0)
{
IncrementHealth(value);
}
}
bool UHealthComponent::GetIsDead()

View File

@ -47,6 +47,7 @@ protected:
UPROPERTY(VisibleAnywhere)
float CurrentHealth;
private:
bool IsDead = false;
bool CanDamage = true;