x


How to store coroutines inside a List?

I want to create a List of coroutines to start if future. How to store such functions inside a list?

Say we have a function like:

IEnumerator Example() {
    while (true) {
        yield return  new WaitForFixedUpdate();
    }
}

We cant store it inside something like `List> list` but we get compiler error like:

Error   1 The best overloaded method match for 'System.Collections.Generic.List<System.Func<System.Collections.IEnumerator>>.Add(System.Func<System.Collections.IEnumerator>)' has some invalid arguments

when we call

           list.Add(Example);

or if we use `List list` we get no compile errors but on

            if (disposeActions.Any()) {
                yield return StartCoroutine(list[0]); //here we get null exception
                disposeActions.RemoveAt(0);
            }

So I wonder how to create a list to store IEnumerator functions and be capable to send its items to coutines?

more ▼

asked Aug 17 '12 at 06:37 PM

name123 gravatar image

name123
1 2 6 10

Aug 17 '12 at 07:03 PM Khada

@khada I don't see the point.

Aug 17 '12 at 07:32 PM Kryptos

You can use delegates to hold a reference to a method. That method can contain your coroutine code and the delegates can go into a list. *Haven't done exactly this before but makes sense to me at a glance at least.

Aug 17 '12 at 07:38 PM Khada

Coroutines are not exaclty methods since the compiler generate an anonymous class, so I'm not sure you can call them using delegates.

You could create a method corresponding to each coroutine that call StartCoroutine. But that sounds a bit heavy.

Aug 18 '12 at 08:47 AM Kryptos

@Kryptos: the point is to `yield return StartCorutine(function)` s from that list one by one (so to say destroy only one asset bundle at a time)

Aug 18 '12 at 10:59 AM name123
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Store strings!

public List<String> coroutinesList = new List<String>();

void CallStoredRoutine(int index)
{
    StartCoroutine(coroutineList[index]);
}
more ▼

answered Aug 17 '12 at 07:37 PM

Paulo Henrique gravatar image

Paulo Henrique
746 4 8 16

Wow, nice to know.

Aug 17 '12 at 07:40 PM Khada
(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:

x4172
x97

asked: Aug 17 '12 at 06:37 PM

Seen: 291 times

Last Updated: Aug 18 '12 at 05:48 PM