Firing Events from code

4_ScriptableEvent

Events are useful to trigger things like VFX, menus, or even gameplay elements. Let’s look at the events that are fired from Health.cs on the prefab_player.

  • OnPlayerDamaged -> ScriptableEventInt

  • OnPlayerHealed -> ScriptableEventInt

  • OnPlayerDeath -> ScriptableEventNoParam

It is simple to fire these events from code, you simply call the Raise() method. Here is an example in Health.cs:

private void OnDamaged(float value)
{
    if (_currentHealth <= 0f && !_isDead)
        OnDeath();
    else
        _onPlayerDamaged.Raise(Mathf.RoundToInt(value));
}

private void OnHealed(float value)
{
    _onPlayerHealed.Raise(Mathf.RoundToInt(value));
}

private void OnDeath()
{
    _onPlayerDeath.Raise();
    _isDead = true;
}

Last updated