Modifiers

Modifiers are components that hook into another component on the same GameObject while enabled. Health uses them to let other components change incoming damage. The same base classes are reused by other Glowing Rifts packages, for example the Modular Weapon System’s barrel modifiers.

Modifiable

Base for components that modifiers can hook into. It only keeps the list; the derived class decides when to consult it.

MemberDescription
protected IReadOnlyList<Modifier> modifiersThe hooked modifiers, in the order they were added.
void AddModifier(Modifier modifier)Hooks a modifier. Ignores duplicates.
void RemoveModifier(Modifier modifier)Unhooks a modifier.

Modifier

Base for components that hook into a Modifiable on their GameObject. It finds its host on Awake, hooks itself in OnEnable and unhooks in OnDisable.

MemberDescription
protected Modifiable hostThe component this modifier is hooked to.
protected virtual Modifiable FindHost()Finds the host. Override to pick a specific one when an object has several.

Warning

If you override Awake, OnEnable or OnDisable, call the base method so the modifier still hooks and unhooks.

HealthModifier

Base for modifiers that change damage before Health applies it. Requires a Health on the same GameObject.

MemberDescription
abstract float ApplyModifier(DamageData damage)Returns the damage to apply instead. Return 0 or less to block it.

Health.Hurt runs each enabled health modifier in order, passing the damage left by the previous one. Heal and AffectHealth skip modifiers.

Example: Armor

using UnityEngine;
using GlowingRifts.ModularHealthSystem;

public class Armor : HealthModifier
{
    [Range(0, 1)][SerializeField] private float _reduction = 0.5f;

    public override float ApplyModifier(DamageData damage)
    {
        return damage.damagePoints * (1f - _reduction);
    }
}

Example: Shield

using UnityEngine;
using GlowingRifts.ModularHealthSystem;

public class Shield : HealthModifier
{
    [SerializeField] private float _shield = 50f;

    public override float ApplyModifier(DamageData damage)
    {
        float absorbed = Mathf.Min(_shield, damage.damagePoints);
        _shield -= absorbed;
        return damage.damagePoints - absorbed;
    }
}

Invincibility is the built-in example: it returns 0 while enabled.