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

Поділитися
Вставка
  • Опубліковано 4 жов 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 Рік тому +1

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

    • @OneWheelStudio
      @OneWheelStudio  Рік тому +7

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

  • @hoptrix80
    @hoptrix80 Рік тому +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  Рік тому +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.

  • @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.

  • @RogerValor
    @RogerValor Рік тому +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.

  • @VikkersOlfsson
    @VikkersOlfsson Рік тому +1

    This saved my bacon, thanks man.

  • @JamesWjRose
    @JamesWjRose Рік тому +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.

  • @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!

  • @ZiplawDev
    @ZiplawDev Рік тому +3

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

  • @goehlergamedev
    @goehlergamedev Рік тому

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

    • @OneWheelStudio
      @OneWheelStudio  Рік тому

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

  • @mrvastayan
    @mrvastayan 2 роки тому

    Such a good video thanks!

  • @jimmmybacon9043
    @jimmmybacon9043 2 місяці тому

    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.

  • @joscpe
    @joscpe 2 роки тому

    Neat.

  • @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.

  • @palanolho82
    @palanolho82 4 місяці тому

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

    • @OneWheelStudio
      @OneWheelStudio  4 місяці тому

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

    • @palanolho82
      @palanolho82 4 місяці тому

      @@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  4 місяці тому +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.

  • @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 ;)