Search Unity

Passing arguments with EventHandlers

Discussion in 'Scripting' started by flipwon, Nov 3, 2019.

  1. flipwon

    flipwon

    Joined:
    Dec 29, 2016
    Posts:
    179
    Is there a way to pass custom arguments with EventHandlers?

    IE:
    Code (CSharp):
    1. public event EventHandler OnDamagedEvent;
    2.  
    3. public void SomeTakeDamageMethodEtc()
    4. {
    5.     ////Calculate damage etc etc
    6.     OnDamagedEvent?.Invoke(this, EventArgs.Empty, damageTaken);
    7. }
    I've been struggling trying to figure this out the simplest way possible.
     
  2. Vectorbox

    Vectorbox

    Joined:
    Jan 27, 2014
    Posts:
    232
  3. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    To expand on what @Vectorbox said, if you create a class from a unity event, you can also populate a unity event in the inspector.

    Code (CSharp):
    1.     using UnityEngine.Events;
    2.  
    3.     //....
    4.  
    5.     [Serializable]
    6.     public class IntEvent : UnityEvent<int> { }
    7.  
    8.     public IntEvent OnDamagedEvent;
    9.  
    10.     public int DamageTaken;
    11.  
    12.     public void SomeTakeDamageMethod()
    13.     {
    14.         OnDamagedEvent.Invoke(DamageTaken);
    15.     }
     
    flipwon and Vectorbox like this.