Unity actions and events with parameters

Hi, I’ve recently followed this tutorial to get an understanding of how to create an event system for unity to decouple my scripts.

I have it working fine when running methods via the event system that takes no parameters. However, how would I call these if I were to add parameters to the methods I want to call with the action.

This is the code I have at the moment. LoginRequest is a stripped back version because this usually handles the connection to the API with JSON and a lot of irrelevant code, so I have replaced it with simple key actions for this example.

Of course from the example, you can see that SuccessfullLogin would run fine (no parameters) but failed login gives me errors because I can’t handle the parameters.

Could someone please let me know what I would need to add or change to allow this.

Event Manager

using UnityEngine;
using System.Collections;
using UnityEngine.Events;
using System.Collections.Generic;

public class EventManager : MonoBehaviour {

    private Dictionary <string, UnityEvent> eventDictionary;

    private static EventManager eventManager;

    public static EventManager instance
    {
        get
        {
            if (!eventManager)
            {
                eventManager = FindObjectOfType (typeof(EventManager)) as EventManager;

                if (!eventManager)
                {
                    Debug.LogError ("There needs to be one active EventManager script on a GameObject in your scene.");
                }
                else
                {
                    eventManager.Init ();
                }
            }

            return eventManager;
        }

    }

    void Init ()
    {
        if (eventDictionary == null)
        {
            eventDictionary = new Dictionary<string, UnityEvent> ();
        }
    }

    public static void StartListening(string eventName, UnityAction listener)
    {
        UnityEvent thisEvent = null;

        if (instance.eventDictionary.TryGetValue (eventName, out thisEvent))
        {
            thisEvent.AddListener (listener);
        }
        else
        {
            thisEvent = new UnityEvent ();
            thisEvent.AddListener(listener);
            instance.eventDictionary.Add (eventName, thisEvent);
        }
    }

    public static void StopListening (string eventName, UnityAction listener)
    {
        if (eventManager == null)
            return;

        UnityEvent thisEvent = null;

        if(instance.eventDictionary.TryGetValue (eventName, out thisEvent))
        {
            thisEvent.RemoveListener (listener);
        }
    }

    public static void TriggerEvent (string eventName)
    {
        UnityEvent thisEvent = null;
        if (instance.eventDictionary.TryGetValue (eventName, out thisEvent))
        {
            thisEvent.Invoke ();
        }
    }
}

LoginRequest

using UnityEngine;
using UnityEngine.Events;
using System.Collections;

public class LoginRequest : MonoBehaviour
{
    private UnityAction successfullLoginListener;
    private UnityAction failedLoginListener;

    #region Events
    void Awake()
    {
        successfullLoginListener = new UnityAction(SuccessfullLogin);
        failedLoginListener = new UnityAction(FailedLogin);
    }

    void OnEnable ()
    {
        EventManager.StartListening("Successfull Login", successfullLoginListener);
        EventManager.StartListening("Failed Login", successfullLoginListener);
    }

    void OnDisable()
    {
        EventManager.StopListening("Successfull Login", successfullLoginListener);
        EventManager.StopListening("Failed Login", successfullLoginListener);
    }

    void SuccessfullLogin()
    {
        Debug.Log("Successfull Login");
    }

    void FailedLogin(string serverFeedback)
    {
        Debug.Log("Failed Login");
        Debug.Log(serverFeedback);
    }
    #endregion

    void Update () {
        if (Input.GetKeyDown("q"))
        {
            string serverFeedback = "error message";
            //I want to send the server feedback string with the trigger event to call the above method.
            EventManager.TriggerEvent("Failed Login");
        }

        if (Input.GetKeyDown("t"))
        {
            EventManager.TriggerEvent("Successfull Login");
        }
    }
}

Fixed it.

If anyone else is thinking of creating this themselves save some time and buy this Unity Asset Store - The Best Assets for Game Making

Works perfectly.