I have some hard question for me... I have a nice EventManager, who handle multiple arguments. (no argument, one int, one bool, a GameObject and a bool... but for each sort, I have to create a special dictionnary for this...
public class EventManager : SingletonMono<EventManager>
{
//here I have many, many class who handle all sorte of different arguments...
private class UnityEventInt : UnityEvent<int> { }
//here definition of all dictionnnary for each class...
private Dictionary<GameData.Event, UnityEvent> eventDictionary;
private Dictionary<GameData.Event, UnityEventInt> eventDictionaryInt;
private void Awake()
{
Init();
}
void Init()
{
//here initialise all dictionnary
if (eventDictionary == null)
eventDictionary = new Dictionary<GameData.Event, UnityEvent>();
if (eventDictionaryInt == null)
eventDictionaryInt = new Dictionary<GameData.Event, UnityEventInt>();
//...
}
//One of the startListening
public static void StartListening(GameData.Event eventName, UnityAction<int> listener)
{
if (!Instance)
return;
UnityEventInt thisEvent = null;
if (Instance.eventDictionaryInt.TryGetValue(eventName, out thisEvent))
{
thisEvent.AddListener(listener);
}
else
{
thisEvent = new UnityEventInt();
thisEvent.AddListener(listener);
Instance.eventDictionaryInt.Add(eventName, thisEvent);
}
}
//one of the stop
public static void StopListening(GameData.Event eventName, UnityAction listener)
{
if (EventManager.Instance == null) //au cas ou on a déja supprimé l'eventManager
return;
UnityEvent thisEvent = null;
//si on veut unregister et que la clé existe dans le dico..
if (Instance.eventDictionary.TryGetValue(eventName, out thisEvent))
{
thisEvent.RemoveListener(listener);
}
}
//and one of the trigger
public static void TriggerEvent(GameData.Event eventName, int param1)
{
if (EventManager.Instance == null) //au cas ou on a déja supprimé l'eventManager
return;
UnityEventInt thisEvent = null;
if (Instance.eventDictionaryInt.TryGetValue(eventName, out thisEvent))
{
thisEvent.Invoke(param1);
}
}
And... when I want, in some script I do:
EventManager.StopListening(GameData.Event.PlayerAddScore, SomeFuncntion);
EventManager.StartListening(GameData.Event.PlayerAddScore, SomeFuncntion);
EventManager.TriggerEvent(GameData.Event.PlayerAddScore, 1);
here SomeFunction take 1 argument, but if i wanted to have 2... i have to hard code the case in my EventManager
So... My question is... how can I make that dynamic ? with only one Dictionary ? with only one list ? with good OOP or something ?
And secondly, how can I handle order or the call ? Let's say 2 functions are listening for PlayerAddScore from 2 different script, how can I handle an order between the 2 function ?
Thanks you for help.