Simple CoroutineChaining Idea.

Just remain gist. hf :slight_smile:

Example.

void Start(){
    CoroutineChain.Start
        .Play(Test1())
        .Call(()=>Debug.Log("Middle of chaining"))//non coroutine
        .Wait(1f)
        .Sequential(Test1, Test2, Test3) //runs one after another
        .Parallel(Test1(), Test2(), Test3()) //runs all at the same time
        .Call(()=>Debug.Log("Complete!")); //all parallel routine complete.
}

}

*17-04-20

  • rename Plll to Parallel
  • add Call
  • add Start

*17-04-20 (2)

  • remove unirx dependency
  • add Play(Func next)
  • add sequencial
  • rename Parrallel to PlayWithUpper temporary and change Parrallel.

*17-04-21

  • remove PlayWithUpper
  • make Parallel completely
  • release 0.1

Thanks for everybody!
At first this is only 30 line small file, but now raise to 150 line.
I leave a while but if find a bug or a suggestion leave message on this thread or gist page.
Thanks for ideas again. happy coding!

2 Likes

What’s the difference between that and this?

    IEnumerator Start()
    {
        yield return Test1();
        yield return new WaitForSeconds(1f);
        yield return Test2();
        StartCoroutine(Test3()); //I'm assuming Plll is parrallel, so we don't wait on it
    }

I believe the idea is using objectchaining. Similar to how one uses Linq or how one programs in jQuery.

Functionally they should behave the same, Its just code sugar mostly which isn’t really a bad thing. Though persionally I would rename that Plll(), its not a very sugary name.

On behaviour, same. but less character.

Thanks reply.

Is the MainThreadDispatcher a version of this? That made the code work, but since you’ve seemed to change the back-end some, the code won’t work right out of the box.

Anyway, this is nice if you need some kind of sequential workflow often. It might be a case of “they were so preoccupied with whether or not they could, they didn’t stop to think if they should”

I’ll suggest adding overloads for Play, both a Play(Func):

public Chain Play(Func<IEnumerator> next)
{
    return Chaining(next(), this);
}

//allows doing this:
CoroutineChain.Start
              .Play(Test1)
              .Wait(5)
              .Play(Test2);

And a Play(params Func):

public Chain Play(params Func<IEnumerator>[] next)
{
    Chain current = this;
    for (int i = 0; i < next.Length; i++)
    {
        current = Chaining(next(), current);
    }
    return current;
}

//allows doing this:

CoroutineChain.Start
          .Play(Test1, Test2)
          .Wait(5)
          .Play(Test3);
}

It might be that that overload should be named “Sequential” instead? Then if you do a similar trick with Parallel, it would be possible to make the code a lot easier to read:

* *CoroutineChain.Start .Sequential(Test1, Test2, Test3) //runs one after another .Wait(5) .Parallel(Test1, Test2, Test3); //runs all at the same time }* *
idk, though.

1 Like

this MainTheadDispatcher is in UniRx. I’ll change it working without UniRx.
In my case each Coroutines have so many parameter.
like below…

CoroutineChain.Start
      .Play(XPositionTween(trans, x, time))
      .Parralled(ColorTween(grahpic, Color.clear, time));
      ...

If I implement Sequential and Parallel with list parameter, there is so many characters in single line or overhead line.

Though it seems to good idea. Once I’ll make it.
Sorry for poor English. English is so hard… Thanks for reply

1 Like

it depends on how people will want to use it, but if it bothers them so much (the overcrowding parameters that is…) then they could just simply package up the coroutine calls into lambada closures.

var closureA = () => XPositionTween(trans,x,time);
var closureB = () => ColorTween(graphic,Color.clear, time);

CoroutineChain.Start
    .Sequential(closureA())
    .Parallel(closureB());
    ...

I like it than using lambda.(closure?)

var colorTween = CC.ColorTween(graphic, Color.cleaer, 2f);
var colorTween2 = CC.ColorTween(graphic, Color.white , 2f);

CoroutineChain.Start
        .Sequential(colorTween, colorTween2);