- Home /
Start a Coroutine with a delegate that has a parameter
I'm trying to make a GameEvent system using delegates, where other objects will register with the GameEvent they want to listen for, and the GameEvent (when triggered) will run the delegate function that was passed to it by the other object, along with a parameter relevant to the GameEvent (most likely a Dictionary or some other container with the relevant details of the event).
The problem I'm running into is that the line I've commented below get's the following error from Visual Studio: Argument 1: Cannot convert from 'void' to 'string'
It works just fine if the delegate doesn't have any parameters, but as soon as I give it even a single parameter, StartCoroutine won't work with it. Is there a way to achieve what I'm trying to do?
public class GameEvent : MonoBehaviour
{
public delegate void EventDelegate(int m);
EventDelegate thisDelegate;
public static event EventDelegate delegateEvent;
public void SetListener(EventDelegate eventFunction)
{
thisDelegate = eventFunction;
delegateEvent += eventFunction;
}
public IEnumerator Trigger()
{
int m = 0;
yield return StartCoroutine(delegateEvent(m)); // This line has the error
}
}
Answer by thunderbuns · Jul 25, 2018 at 11:54 PM
The error says "cannot convert from 'void' to 'System.Collections.IEnumerator'". It looks your problem is that EventDelegate is a void and it needs to be an IEnumerator. Changing public delegate void EventDelegate(int m);
to public delegate IEnumerator EventDelegate(int m);
removed the error.
I totally forgot that IEnumerator was a return type. Thanks, this solved it!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Executing coroutines consecutively 0 Answers
Sharing delegate types across scripts 1 Answer
IEumerator not recognising when a bool has switched 1 Answer