Unity Coroutines - Neat and tidy ways to know when they're done!

Поделиться
HTML-код
  • Опубликовано: 25 дек 2024

Комментарии • 32

  • @vazzius
    @vazzius 2 года назад +21

    I think you don't need to explicitly StartCoroutine when yielding another coroutine, just 'yield return SomeOtherRoutine()' should behaviour as intended in the video.

    • @soverain
      @soverain 2 года назад +1

      You got it, no need to call StartCoroutine() when yielding another coroutine.

    • @OneWheelStudio
      @OneWheelStudio  2 года назад +7

      Yep. Totally missed that. Keeps it even cleaner. Thanks for pointing this out.

  • @JackFoz454
    @JackFoz454 2 года назад

    Great to see another one of these! Found it really useful, too. I confess that I wasn't quite seeing the point until you connected it to the silo launch sequence, and then it clicked for me. Thank you!

  • @hoptrix80
    @hoptrix80 2 года назад +2

    Recently I have moved away from Coroutines and have started using async, await, and Tasks. Makes things a lot easier from a readability standpoint.

    • @OneWheelStudio
      @OneWheelStudio  2 года назад +3

      Yeah. I've used them occasionally and need to learn more about them. Definitely more complex with some pitfalls, but a better overally solution in many or most cases.

  • @VikkersOlfsson
    @VikkersOlfsson Год назад +1

    This saved my bacon, thanks man.

  • @JamesWjRose
    @JamesWjRose 2 года назад +1

    That was very help. Clear and good details. Thanks. Have a wonderful day

  • @SimplyPhoenix
    @SimplyPhoenix 2 года назад

    Thanks that was really useful. Didnt know you could also yield return other coroutines. This is also helpful to break large coroutines into smaller pieces.

  • @RogerValor
    @RogerValor 2 года назад +2

    The method you show with Actions passed to Coroutines as a pattern is called a *Callback*
    It is used in many languages, but most notably it was used in javascript, where it got used to such excess, that they invented the term "callback hell", because once you nest it too deep, the program can get really hard to read.
    I liked your suggestion to create wrapping coroutines that calls many other coroutines very much.

  • @Diinify
    @Diinify 2 года назад +1

    Incredibly useful. Though after hearing the word so many times, I don't think "coroutine" means anything to me anymore, lol.

  • @goehlergamedev
    @goehlergamedev 2 года назад

    Wow! This is crazy useful, and much cleaner :D

    • @OneWheelStudio
      @OneWheelStudio  2 года назад

      Thanks for the all the kind comments. I'm glad you're finding some of the videos useful!

  • @ZiplawDev
    @ZiplawDev 2 года назад +3

    you dont need to do ```yield return StartCoroutine(MyMethod())```, you can just do ```yield return MyMethod()```

  • @mrvastayan
    @mrvastayan 2 года назад

    Such a good video thanks!

  • @jimmmybacon9043
    @jimmmybacon9043 5 месяцев назад

    Problem is when I put my main coroutine into update, it keeps repeating them, and when i use a boolean to run it once, it only runs each coroutine for a single frame and then stops without actually completing the coroutine despite me putting the return value to a boolean that only turns true once the coroutine has reached its destination.

  • @palanolho82
    @palanolho82 7 месяцев назад

    @OneWheelStudion how would you go about checking if multiple coroutines that are running in parallel has all finished?
    thanks in advance

    • @OneWheelStudio
      @OneWheelStudio  7 месяцев назад

      To be honest I’d probably fall back on using Booleans. I’d love to know if there is a better way.

    • @palanolho82
      @palanolho82 7 месяцев назад

      @@OneWheelStudio Booleans are tricky when you calling coroutines across components.
      I did some more digging and found an easier way that worked well.
      Since I only wanted to proceed when all the coroutines were complete, I just `yield return` them one after the other
      Coroutine cA = StartCoroutine(whatever.DoThis());
      Coroutine cB = StartCoroutine(banana.DoThat());
      yield return cA;
      yield return cB;
      This works great because no matter which one will end first, it will always take as much time as the longest one to execute.
      What do you think?

    • @OneWheelStudio
      @OneWheelStudio  7 месяцев назад +1

      ​@@palanolho82 Somehow in my head I was imagining something more complex, or I was just being dumb. 🤷‍♂
      Your solution totally makes sense. Its clean and simple. Looks good to me.

  • @aurora686
    @aurora686 2 года назад +1

    Actually theres Async Await systems for that if i am not wrong

    • @OneWheelStudio
      @OneWheelStudio  2 года назад

      I definitely think you can use Async functions for this type of functionality, but that can open a new can of worms for users. I want to cover Async functions, but it is a large topic and one that's easy to mess up.

  • @fleity
    @fleity 2 года назад +1

    The real question is though, how do you start 3 coroutines at the same time with varying durations which you can not know beforehand and then yield until all are finished.

    • @OneWheelStudio
      @OneWheelStudio  2 года назад +2

      You could have a wrapper coroutine for each coroutine. The wrapper takes in an action that invokes a function that counts how many coroutines have finished. This might be a bit Jacky, but it could give a handle on tracking how many have completed.

    • @nikescar
      @nikescar 2 года назад

      This kind of use case for coroutines never really came up for me in my projects but it got me wondering how to accomplish it.
      Yeesh! There has to be a better way than what I'm doing in this Gist: gist.github.com/nikescar1/65255cacc6450c2c464802f413a87a83
      By the time I added OnFinish messaging to everything, it's not that pretty. Does seem to work though.

    • @nikescar
      @nikescar 2 года назад +1

      I also gave an example of how to "return" values from coroutines using an Action. The inability for coroutines to return values is always on lists of why to avoid coroutines.

    • @OneWheelStudio
      @OneWheelStudio  2 года назад

      Oh! I like that idea. Hadn’t thought of that use.

    • @Vastlee
      @Vastlee 2 года назад

      @@nikescar Agreed. Coroutines produce some serious spaghetti and callback hell. UniTask handles this well & has been my goto for a long time now.

  • @joscpe
    @joscpe 2 года назад

    Neat.

  • @lmao01
    @lmao01 Год назад

    I prefer UniTask

  • @d1kiywolf
    @d1kiywolf 2 года назад

    Bro your voice sounds a lot like WackyJacky101's

    • @OneWheelStudio
      @OneWheelStudio  2 года назад

      Now if only our sub counts were more alike ;)