Before-Events
Intercept, modify, or cancel actions before they execute.
Overview
Before-events fire before the action executes, giving subscribers a chance to modify or cancel it. Two before-events exist: BeforeDamageEvent and BeforeHealEvent.
These are classes (not structs) so modifications persist. When a subscriber changes Amount or sets Cancel = true, those changes are visible to the Battle method that fired the event and to all subsequent subscribers.
BeforeDamageEvent
Fired by Battle.DealDamage() before any damage is applied.
| Property | Type | Settable | Description |
|---|---|---|---|
Source |
ICombatant |
No | The combatant dealing damage. |
Target |
ICombatant |
No | The combatant receiving damage. |
Amount |
int |
Yes | The damage amount. Modify this to increase or reduce damage. |
DamageType |
string |
No | Pass-through string for the type of damage (e.g. "Physical", "Magic"). |
Element |
string |
No | Pass-through string for the element (e.g. "Fire", "Ice"). |
IsCrit |
bool |
No | Whether this hit is a critical. |
Cancel |
bool |
Yes | Set to true to cancel the damage entirely. OnDamageDealt will not fire. |
When Cancel is set to true, the damage is not applied and OnDamageDealt does not fire.
BeforeHealEvent
Fired by Battle.Heal() before any healing is applied.
| Property | Type | Settable | Description |
|---|---|---|---|
Source |
ICombatant |
No | The combatant performing the heal. |
Target |
ICombatant |
No | The combatant being healed. |
Amount |
int |
Yes | The heal amount. Modify this to boost or reduce healing. |
Cancel |
bool |
Yes | Set to true to cancel the heal entirely. OnHealed will not fire. |
Same cancellation behavior as BeforeDamageEvent — when Cancel is true, the heal is not applied and OnHealed does not fire.
Example: Dodge System
Subscribe to OnBeforeDamage and cancel the event to implement a dodge mechanic:
battle.OnBeforeDamage += e =>
{
// 20% dodge chance
if (Random.value < 0.2f)
{
e.Cancel = true;
Debug.Log($"{e.Target.Name} dodged the attack!");
}
};
Example: Damage Reduction
Modify Amount to reduce incoming damage when a combatant is defending:
battle.OnBeforeDamage += e =>
{
// Halve damage if target is defending
if (IsDefending(e.Target))
e.Amount /= 2;
};
Example: Healing Boost
Modify Amount on OnBeforeHeal to amplify healing:
battle.OnBeforeHeal += e =>
{
// Double healing if healer has "Amplify" buff
if (HasBuff(e.Source, "Amplify"))
e.Amount *= 2;
};
Amount and Cancel must persist to the Battle method that fired the event. Multiple subscribers can all modify the same event.