Health

Health holds an object’s health and applies damage and healing. It derives from Modifiable, so health modifiers on the same GameObject can change incoming damage.

Fields

FieldTypeDescription
_healthfloatStarting and maximum health.
_destroyOnDeathboolDestroy the GameObject when health reaches zero.
OnHealthChangedUnityEvent<float>Invoked when health changes, with health from 0 to 1.
OnDeathUnityEvent<DamageData>Invoked once when health reaches zero, with the killing blow (DamageData.Empty after Kill()).

Properties

PropertyTypeDescription
maxHealthfloatMaximum health, set from the starting health on Awake.
currentHealthfloatCurrent health.
isDeadboolTrue once health has reached zero.
isFullboolTrue when health is at its maximum.

Methods

MethodDescription
virtual void Hurt(DamageData damageData)Runs the health modifiers, then reduces health by the rounded damage left. Does nothing when dead or when the damage is blocked.
virtual float Heal(DamageData healData)Restores healData.damagePoints, up to the maximum. Skips modifiers. Returns the amount actually restored.
virtual float AffectHealth(float amount, DamageData data)Changes health by a signed amount (positive heals, negative hurts), clamped between zero and the maximum. Raises OnHealthChanged, and dies at zero. Skips modifiers. Returns the change actually applied.
virtual void Kill()Kills immediately.
virtual void ResetHealth()Restores health to its maximum and clears the dead state.
void Spawn(GameObject prefab)Instantiates a prefab at this object’s position and rotation.
protected virtual void Die(DamageData killingBlow)Raises OnDeath once, then destroys the object if _destroyOnDeath is on. Override for custom death handling.

Examples

using GlowingRifts.ModularHealthSystem;

// Heal 25 from a medkit
health.Heal(new DamageData { damagePoints = 25f, instigator = gameObject });

// Poison that ignores Invincibility and other modifiers
health.AffectHealth(-5f, new DamageData { instigator = gameObject });

// Respawn
health.ResetHealth();

Note

Hurt is the only method that runs modifiers. Use it for anything that should respect armor or invincibility.

See Also