Heal Action

Hey how would i go about creating a Heal action? ive tried to refactor the code for this sword action to where it does negative damage amounts to friendly units but that doesnt seem to work for.

I added a simple Heal method to Health:

    public void Heal(int healAmount)
    {
        health += healAmount;
        if (health > healthMax) health = healthMax;
        OnDamaged?.Invoke(this, EventArgs.Empty);
    }

The HealAction then simply calls Heal(amount);

Passing a negative amount to Damage will also do the trick, but without refactoring, the health could exceed healthMax. Calling OnDamage?Invoke should trigger updates to the interface like the health bar.

1 Like

My boiler-plate health components (like you’ve mentioned in another post, @Brian_Trotter) are usually not very specific. It’s usually as simple as

public event EventHandler<HealthUpdatedEventArgs> HealthUpdated;
public event EventHandler<EventArgs> Dead;

[SerializeField] int _maxHealth = 100;
private int _currentHealth;
private void Awake()
{
    _currentHealth = _maxHealth;
}
public void AdjustHealth(int amount)
{
    var oldHealth = _currentHealth;
    _currentHealth = Mathf.Clamp(_currentHealth + amount, 0, _maxHealth);

    HealthUpdated?.Invoke(this, new HealthUpdatedEventArgs(oldHealth, _currentHealth));

    if (_currentHealth == 0) Die();
}
public void Die()
{
    // Do death stuff
    Dead?.Invoke(this, EventArgs.Empty);
}
public class HealthUpdatedEventArgs : EventArgs
{
    public int OldHealth { get; }
    public int NewHealth { get; }
    public HealthUpdatedEventArgs(int oldHealth, int newHealth)
    {
        OldHealth = oldHealth;
        NewHealth = newHealth;
    }
}

I’ll pass negative amounts for damage and positive for healing. I could, if I wanted, have specific methods to make it a little clearer

public void Heal(int amount) => AdjustHealth(Mathf.Abs(amount));
public void Damage(int amount) => AdjustHealth(-Mathf.Abs(amount));
3 Likes

It depends on how you are trying to accomplish it, it would be the exact same as a shoot or sword action except you add instead of subtract HP, and when check for available units where


check the if (!targetUnit.IsEnemy(GetUnit())) to if (targetUnit.IsEnemy(GetUnit())) and also check if the target unit is null, that way it will only highlight your units.
I have it implemented in mine but its not done through units so it would be complex to share but this is essentially what i have done

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms