1
\$\begingroup\$

In Unity events are used as enhanced delegates. This is extremely helpful in developing clean concise event management scripts.

However, when debugging said scripts and complex arrangements, is there a way I can see which methods are in an event at any one time?

Simplified Example:

Game manager

using UnityEngine;
using System.Collections;

public class EventManager : MonoBehaviour 
{
    public delegate void ClickAction();
    public static event ClickAction OnClicked;

    void someMethod()
    {
        if(whatever = true && OnClicked != null)
        {
            //this is the event delegate:
            OnClicked();
        }
    }
}

gameObject script
(one of several scripts that can add methods to an event )

using UnityEngine;
using System.Collections;

public class TeleportScript : MonoBehaviour 
{
    void SomeMethod()
    {
        EventManager.OnClicked += Teleport;
    }

    void SomeOtherMethod(){
        EventManager.OnClicked += Horses; 
    }

    void OnDisable()
    {
        EventManager.OnClicked -= Teleport;
        EventManager.OnClicked -= Horses;
    }


    void Teleport()
    {
        Vector3 pos = transform.position;
        pos.y = Random.Range(1.0f, 3.0f);
        transform.position = pos;
    }

    void Horses(){
         instantiate(horses, new Vector3.zero, Quaternion.identity);
    }
}

etc. etc.,

I have used Debug.Log(OnClicked.Method); but this only returns one of multiple methods associated with an event.

So my issue is, that for debugging and general error resolution, how can I find out which method(s) are in the OnClicked() event delegate without/before running this delegate?

For example:

Debug.Log(OnClicked(listMethods));

//Output to log:
void Teleport();
void Horses();

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

You want to get the InvocationList of the event. Here is a brief code example :

 public delegate void Test();
 System.Delegate[] invokeList;
 public static event Test OnTest;

  OnTest += TestMethod1;
  OnTest += TestMethod2;
  invokeList = OnTest.GetInvocationList();
  foreach(System.Delegate d in invokeList)
  {
    Debug.Log(d.Method);
  }
\$\endgroup\$
4
  • \$\begingroup\$ That's fantastic, that's exactly what I was looking for. \$\endgroup\$
    – Martin
    Commented Dec 9, 2016 at 13:10
  • \$\begingroup\$ As a side note, how did you discover this? I was looking all over for this type of solution but wasn't finding anything tha fitted the bill. Altough I was also unaware of GetInvocationList \$\endgroup\$
    – Martin
    Commented Dec 9, 2016 at 13:20
  • \$\begingroup\$ Well I just created the event and used intellisense to scroll trough all the available properties and methods and GetInvocationList seemed to do what you want from its name alone. After that i was just a matter of writing this small test to be sure and voila. \$\endgroup\$
    – Uri Popov
    Commented Dec 9, 2016 at 13:27
  • \$\begingroup\$ oh ffs, I had looked through the invoke list expecting to find something akin to this but I'd clearly missed that function,... now I feel stupid :-( hah. \$\endgroup\$
    – Martin
    Commented Dec 9, 2016 at 13:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .