x


Adding a coroutine to an event?

Is it possible to add a coroutine to an event? I'm experimenting with events, and it seems like it would be extremely useful. However, I've got to run a coroutine for one of the functions that will be assigned to the event. Don't know if I'm explaining this correctly so here's some code:

Event declaration:

public delegate void BookSelectEventHandler();
public static event BookSelectEventHandler BookSelect;
public static void OnBookSelect()
{
    BookSelectEventHandler bookSelect = BookSelect;
    if (bookSelect != null)
        bookSelect();
}

Basically what I want to do (but not a solution):

BookSelect += StartCoroutine(RenderWholeBook);

My function:

IEnumerator RenderWholeBook()
    {
                    //snap
        yield return null;  //return next frame
                    //change text
        yield return null;  //return next frame
                    //snap next page
        yield return null;  //return next frame
                    //turn off
        yield break;
    }

Is this even a viable approach? Or should I just do the long work around of reference and call the function?

Also thought about this:

public delegate void BookSelectEventHandler();
public static event BookSelectEventHandler BookSelect;
public static void OnBookSelect()
{
    BookSelectEventHandler bookSelect = BookSelect;
    if (bookSelect != null)
        StartCoroutine(bookSelect());
}

But it doesn't work either.

more ▼

asked Mar 07 '12 at 05:39 PM

SirGive gravatar image

SirGive
1.2k 25 32 50

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Surprising simple:

public delegate IEnumerator BookSelectEventHandler();
public static event BookSelectEventHandler BookSelect;
public void OnBookSelect()
{
    BookSelectEventHandler bookSelect = BookSelect;
    if (bookSelect != null)
        StartCoroutine(bookSelect());
}

And you can only add functions with the return type of IEnumerator:

  • Only the last event handler to be added will be called -- Luiz
  • I only use one, so it works for me

And this is how you would add the function to the event:

BookSelect += RenderWholeBook;

Updated my answer. Everything works perfectly.

more ▼

answered Mar 07 '12 at 07:35 PM

SirGive gravatar image

SirGive
1.2k 25 32 50

(comments are locked)
10|3000 characters needed characters left

I'm not sure what your intentions are, but you can't start a directly coroutine from a static method since StartCoroutine is a member method from MonoBehaviour. Another problem is that the event handlers must be a coroutine, so they must return IEnumerator.

public delegate IEnumerator BookSelectEventHandler();
public static event BookSelectEventHandler BookSelect;
public void OnBookSelect()
{
    if (BookSelect != null)
        StartCoroutine(BookSelect());
}

static IEnumerator RenderWholeBook()
{
                //snap
    yield return null;  //return next frame
                //change text
    yield return null;  //return next frame
                //snap next page
    yield return null;  //return next frame
                //turn off
    yield break;
}

(...)

BookSelect += RenderWholeBook;

However there's a problem: only the last handler is called.

more ▼

answered Mar 07 '12 at 07:41 PM

luizgpa gravatar image

luizgpa
866 4 9

So the last function added would be the only one to ever be called? That would defeat the purpose of using events wouldn't it?

Mar 07 '12 at 08:15 PM SirGive
(comments are locked)
10|3000 characters needed characters left

Your delegate needs to return an IEnumerator, as well as BookSelect's function, which is probably assigned from elsewhere. Then, you call StartCoroutine(bookSelect());

more ▼

answered Mar 07 '12 at 07:30 PM

Berenger gravatar image

Berenger
11.2k 12 19 54

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x336
x171

asked: Mar 07 '12 at 05:39 PM

Seen: 840 times

Last Updated: Mar 08 '12 at 03:08 PM