0

I'm looking for a C# EventManager like this one that supports coroutine execution. In other words, I would like to be able to do something like:

EventManager.StartListening("AString", ACoroutine);

I know I could wrap my coroutine in a function and use the current version of the code, but it would be better if the code itself supported this to avoid dirty code.

3
  • Short Answer: I don't know of anything built-in for this but there's nothing stopping you from writing one yourself. However, I would suggest just working with what @Everts suggests; there are a lot of potential issues when having an external system starting coroutines for other objects.
    – Foggzie
    Nov 3, 2017 at 20:05
  • Could you give me some example, so I can understand what I could run into with an external system like this?
    – Giovarco
    Nov 4, 2017 at 9:37
  • Sure. Imange you had a class, Foo, deriving from MonoBehaviour that had an IEnumerator defined, IEnumerator Bar(), that could be run as a coroutine. If anything inside of Foo.Bar() accesses something related to the GameObject it's attached to (like its Transform), then you'll get exceptions if that GameObject is destroyed while Bar() is still running (which is possible if an external system started the coroutine). If the Foo class itself calls StartCoroutine(Bar()), that coroutine will be automatically destroyed when the object is destroyed.
    – Foggzie
    Nov 6, 2017 at 16:38

1 Answer 1

1

You can wrap your coroutine in a normal method so you can use the existing code:

private IEnumerator coroutine = null;
void AMethod()
{
     if(this.coroutine != null){ return; } // Already running
     this.coroutine = ACoroutine();
     StartCoroutine(this.coroutine);
}

private IEnumerator ACoroutine()
{
      yield return null;
      this.coroutine = null;
}

void Start()
{
     EventManager.StartListening("AString", AMethod);
}

EDIT: Here is the system supporting coroutine. It has to adopt a slight different process(or at least I did not dig into it) so instead of calling an event of the type, you create a list of the type. This is because your StartCoroutine cannot call multiple delegates and requires iterations.

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

public class EventManager : MonoBehaviour
{
    private Dictionary<string, List<Func<IEnumerator>>> 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 EventManger script on a GameObject in your scene.");
                }
                else
                {
                    eventManager.Init();
                }
            }
            return eventManager;
        }
    }
    void Init()
    {
        if (eventDictionary == null)
        {
            eventDictionary = new Dictionary<string, List<Func<IEnumerator>>>();
        }
    }
    public void StartListening(string eventName, Func<IEnumerator> listener)
    {
        List<Func<IEnumerator>> thisEvent;
        if (instance.eventDictionary.TryGetValue(eventName, out thisEvent))
        {
            thisEvent.Add(listener);
        }
        else
        {
            instance.eventDictionary.Add(eventName, new List<Func<IEnumerator>>() { listener });
        }
    }

    public void StopListening(string eventName, Func<IEnumerator> listener)
    {
        if (eventManager == null) return;
        List<Func<IEnumerator>> thisEvent;
        if (instance.eventDictionary.TryGetValue(eventName, out thisEvent))
        {
            thisEvent.Remove(listener);
        }
    }

    public void TriggerEvent(string eventName)
    {

        List<Func<IEnumerator>> thisEvent = null;
        if (instance.eventDictionary.TryGetValue(eventName, out thisEvent))
        {
            for (int i = thisEvent.Count -1 ; i >= 0; i--)
            {
                if(thisEvent[i] == null)
                { 
                     thisEvent.RemoveAt(i);
                     continue; 
                }
                StartCoroutine(thisEvent[i]());
            }
        }
    }
}
1
  • As I've written in my post, I know I can use this approach. My question is if it's possibile to implement support for coroutine given the existing code.
    – Giovarco
    Nov 3, 2017 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.