Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity actions and events with parameters

Discussion in 'Scripting' started by El3et, Feb 15, 2017.

  1. El3et

    El3et

    Joined:
    Jan 19, 2011
    Posts:
    97
    Hi, I've recently followed this tutorial to get an understanding of how to create an event system for unity to decouple my scripts.

    https://unity3d.com/learn/tutorials/topics/scripting/events-creating-simple-messaging-system

    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
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Events;
    4. using System.Collections.Generic;
    5.  
    6. public class EventManager : MonoBehaviour {
    7.  
    8.     private Dictionary <string, UnityEvent> eventDictionary;
    9.  
    10.     private static EventManager eventManager;
    11.  
    12.     public static EventManager instance
    13.     {
    14.         get
    15.         {
    16.             if (!eventManager)
    17.             {
    18.                 eventManager = FindObjectOfType (typeof(EventManager)) as EventManager;
    19.  
    20.                 if (!eventManager)
    21.                 {
    22.                     Debug.LogError ("There needs to be one active EventManager script on a GameObject in your scene.");
    23.                 }
    24.                 else
    25.                 {
    26.                     eventManager.Init ();
    27.                 }
    28.             }
    29.  
    30.             return eventManager;
    31.         }
    32.  
    33.     }
    34.  
    35.     void Init ()
    36.     {
    37.         if (eventDictionary == null)
    38.         {
    39.             eventDictionary = new Dictionary<string, UnityEvent> ();
    40.         }
    41.     }
    42.  
    43.     public static void StartListening(string eventName, UnityAction listener)
    44.     {
    45.         UnityEvent thisEvent = null;
    46.  
    47.         if (instance.eventDictionary.TryGetValue (eventName, out thisEvent))
    48.         {
    49.             thisEvent.AddListener (listener);
    50.         }
    51.         else
    52.         {
    53.             thisEvent = new UnityEvent ();
    54.             thisEvent.AddListener(listener);
    55.             instance.eventDictionary.Add (eventName, thisEvent);
    56.         }
    57.     }
    58.  
    59.     public static void StopListening (string eventName, UnityAction listener)
    60.     {
    61.         if (eventManager == null)
    62.             return;
    63.  
    64.         UnityEvent thisEvent = null;
    65.  
    66.         if(instance.eventDictionary.TryGetValue (eventName, out thisEvent))
    67.         {
    68.             thisEvent.RemoveListener (listener);
    69.         }
    70.     }
    71.  
    72.     public static void TriggerEvent (string eventName)
    73.     {
    74.         UnityEvent thisEvent = null;
    75.         if (instance.eventDictionary.TryGetValue (eventName, out thisEvent))
    76.         {
    77.             thisEvent.Invoke ();
    78.         }
    79.     }
    80. }
    81.  

    LoginRequest
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Events;
    3. using System.Collections;
    4.  
    5. public class LoginRequest : MonoBehaviour
    6. {
    7.     private UnityAction successfullLoginListener;
    8.     private UnityAction failedLoginListener;
    9.  
    10.     #region Events
    11.     void Awake()
    12.     {
    13.         successfullLoginListener = new UnityAction(SuccessfullLogin);
    14.         failedLoginListener = new UnityAction(FailedLogin);
    15.     }
    16.  
    17.     void OnEnable ()
    18.     {
    19.         EventManager.StartListening("Successfull Login", successfullLoginListener);
    20.         EventManager.StartListening("Failed Login", successfullLoginListener);
    21.     }
    22.  
    23.     void OnDisable()
    24.     {
    25.         EventManager.StopListening("Successfull Login", successfullLoginListener);
    26.         EventManager.StopListening("Failed Login", successfullLoginListener);
    27.     }
    28.  
    29.     void SuccessfullLogin()
    30.     {
    31.         Debug.Log("Successfull Login");
    32.     }
    33.  
    34.     void FailedLogin(string serverFeedback)
    35.     {
    36.         Debug.Log("Failed Login");
    37.         Debug.Log(serverFeedback);
    38.     }
    39.     #endregion
    40.  
    41.     void Update () {
    42.         if (Input.GetKeyDown("q"))
    43.         {
    44.             string serverFeedback = "error message";
    45.             //I want to send the server feedback string with the trigger event to call the above method.
    46.             EventManager.TriggerEvent("Failed Login");
    47.         }
    48.  
    49.         if (Input.GetKeyDown("t"))
    50.         {
    51.             EventManager.TriggerEvent("Successfull Login");
    52.         }
    53.     }
    54. }
     
  2. El3et

    El3et

    Joined:
    Jan 19, 2011
    Posts:
    97