Checking for Health at 0 twice

So in the healthSystem script, you are checking if the health is less than 0 or at 0 multiple times.


if (health < 0)
{
    health = 0;
}

if (health == 0)
{
   Die();
}

We are already checking if we are below 0 in the first if statement (as in unit is dead) so the second if statement is not necessary. the functionality can be placed in the first if statement

Yes true, but the unit is also dead when health == 0 so you’d need to make it health <= 0

Also, at a later stage you’ll add another event (OnDamage) that need to always run when damage is taken, but it needs to run after health < 0 (because we don’t want to notify listeners that damage was taken and present a health value that’s less than 0 so we want to set the value to 0 first), but before health == 0 (because we want listeners to respond to the damage before listeners that respond to the death). So, rather than

health -= damageAmount;
if (health <= 0)
{
    health = 0;
    OnDamage.Invoke();
    Die();
}
else
{
    OnDamage.Invoke();
}

we go with just splitting the health check in 2

health -= damageAmount;
if (health < 0)
{
    health = 0;
}
OnDamage.Invoke();
if (health == 0)
{
    Die();
}

You are welcome to change it if you want (I have exactly 0 projects that match the lecture code), just keep the above in mind.

Privacy & Terms