Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Assets What do you hate about Coroutines?

Discussion in 'Works In Progress - Archive' started by faximegames, Jun 27, 2017.

  1. faximegames

    faximegames

    Joined:
    Jun 27, 2017
    Posts:
    10
    Hi guys,

    We're currently developing a project and decided to develop our own Coroutine system because of some of the limitations that the default Unity system has, such as:
    • no threading,
    • no return values,
    • garbage creation,
    • the need for a MonoBehaviour parent
    • etc.
    We're studying the possibility of publishing the kit on the Asset Store. But first, we'd like to know what bothers you the most about Unity's Coroutine system.

    So, with that said, what are your biggest hurdles with using Coroutines in Unity?
     
  2. Paxew

    Paxew

    Joined:
    Apr 8, 2014
    Posts:
    30
    I also found the coroutines lacking in many areas, mostly some of those you already mentioned.

    So lately I've been using the .net Thread Parallell Library (TPL) which seems to work fine in unity too, need to add it separately though. https://www.nuget.org/packages/TaskParallelLibrary/

    With this you can create tasks that run in separate threads, with return values etc.
     
    faximegames likes this.
  3. faximegames

    faximegames

    Joined:
    Jun 27, 2017
    Posts:
    10
    That's nice!
    I didn't know that there was a standalone version of the TPL (and compatible with the .NET 3.5 profile). Will definitely explore it.

    Just a question, though.
    How do you handle calling the Unity API in a task?
     
    Last edited: Jun 27, 2017
  4. Paxew

    Paxew

    Joined:
    Apr 8, 2014
    Posts:
    30
    I don't. I run the tasks on other threads, as far as I have understood Unity is not thread safe so you can't call its api on another thread and expect it to behave as it should.

    I use it for loading/saving data to databases, writing to files, process huge amounts of data etc. e.g slow operations that I don't want to slow down/hang the main thread for long.

    So once the task I created has processed/loaded the data it then returns its return value to the main thread again (using a synchronization context). Then I continue creating/updating the gameobject or call whatever Unity API call I need safely on the main thread based on the tasks returned data. This way the slow operations don't hang/block the game.
     
    faximegames likes this.
  5. faximegames

    faximegames

    Joined:
    Jun 27, 2017
    Posts:
    10
    Oh, ok.

    Do you by any change know how much overhead using Tasks has on the performance?
    Like, mainly in terms of garbage creation (which is the biggest problem, most of the times).
     
  6. Fabian-Haquin

    Fabian-Haquin

    Joined:
    Dec 3, 2012
    Posts:
    231
  7. Paxew

    Paxew

    Joined:
    Apr 8, 2014
    Posts:
    30
    I haven't really done any profiling in this area. In my use cases it went from small hiccups anytime I wanted to save/load anything during runtime to no hiccups at all :) I don't use Tasks/Coroutines in my games in many places mostly only when saving/loading stuff to avoid having to block/pause gameplay.
     
  8. faximegames

    faximegames

    Joined:
    Jun 27, 2017
    Posts:
    10
    That's really cool and solves that problem of accessing the Unity API in threads.
    Like, you still can't access them in another thread, but you have a "quick" and obvious way to switch contexts.

    The downside is that it inherits the same problems from Unity's own coroutines, I think.
     
    Fabian-Haquin likes this.
  9. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Must yield scaled and unscaled time.
     
  10. faximegames

    faximegames

    Joined:
    Jun 27, 2017
    Posts:
    10
    Do you mean that you'd like to see the ability to yield scaled and unscaled time?

    As far as I remember, Unity's WaitForSeconds yield instruction only works with scaled time, I think.
     
  11. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    It has scaled and unscaled.
     
  12. faximegames

    faximegames

    Joined:
    Jun 27, 2017
    Posts:
    10
    Oh, ok. We tend to use coroutines a lot in our projects, so overhead must be kept to a minimum.

    I'll investigate and profile the TPL package. When I have the data I'll post it over here :)
     
  13. Fabian-Haquin

    Fabian-Haquin

    Joined:
    Dec 3, 2012
    Posts:
    231
    Well it seems that Unity is working on "C# Jobs compiler" to natively support multi-threading.

    I don't know where I can find that online but they just spoke about that in the 2017 Keynote about one hour ago:


    Your package may become deprecated before even being released...
     
    faximegames likes this.
  14. shawnblais

    shawnblais

    Joined:
    Oct 11, 2012
    Posts:
    324
    Last edited: Jun 27, 2017
    faximegames likes this.
  15. faximegames

    faximegames

    Joined:
    Jun 27, 2017
    Posts:
    10
    Just had the opportunity to see it. Almost forgot it was today haha
    Thanks for the heads up. Was looking forward to the keynote, and oh boy was it worth it.

    What we saw regarding the new native Job system was amazing, just like the other tech demos they've showed off in the past.
    The new compiler technology thing was just amazing! It looks to be going in the right way.

    Now, I see what you mean when you say that the package might become deprecated before even being released.
    But the thing is, this new Job system is, at best, several months away from being released.
    And when it does, it might take some time for users to get used to it.
    So there might be an opportunity to release our kit still.

    Like I said in the OP, we're still just exploring the idea and gauging your interest :)
    There's nothing set in stone in this phase.
     
    Fabian-Haquin likes this.
  16. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    This asset solved a lot of the problems.
    https://www.assetstore.unity3d.com/en/#!/content/68480
     
    faximegames likes this.
  17. faximegames

    faximegames

    Joined:
    Jun 27, 2017
    Posts:
    10
    That asset is awesome!
    It really extends functionality and is really easy to use for someone used to using coroutines.
    But I think it's still missing some important features such as return values and optional multi-threading
    .
    The non-object oriented API also bugs me a little bit, but that might just be me :D haha
    Although, in the author's defense, Unity's coroutines are not really OO either.
     
  18. Zajoman

    Zajoman

    Joined:
    May 31, 2014
    Posts:
    15
    My biggest issue with Unity's coroutines is the inability to save and recreate their state. I've created a simple custom coroutine system myself just to see what's up, but I don't see a way to do this.

    It would be great if the coroutine or the wrapping object would somehow store the iterator's progress and then be able to jump to that at request. Like you have .MoveNext(), you would also have .MoveTo(int).
     
    faximegames likes this.
  19. faximegames

    faximegames

    Joined:
    Jun 27, 2017
    Posts:
    10
    Hmmm... That's interesting.
    Pausing a custom coroutine would certainly be straight forward to implement.
    However, it doesn't look like quite what your looking for.

    Could you please give an example on where it would be useful to recreate a coroutine's state?