Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Is it good or bad to use System.Timers.Timer instead of a Coroutine ?

Discussion in 'Scripting' started by Kiupe, Sep 21, 2017.

  1. Xype

    Xype

    Joined:
    Apr 10, 2017
    Posts:
    339
    Having 2 files is easier to read than 1...weird. Maybe you should learn to comment. Any code is easy to read if you comment the code.
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    This is pretty hilarious.
     
  3. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Yes, that's the reason we make functions. Sure, you can write code that's readable in that fashion since you are just calling functions anyway, but the average person trying to write a game is going to write a long string of bad code that way. All you are doing is reintroducing the flags that are built into coroutines. Flags are just another place to have a logic error. We could all just put our own hooks in a main game loop but for most of us, it would result in more logic errors. That's why Unity is popular and the old engines that did that are dead.
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    /thread

    Can we get a thread lock? This has gone so far off the deep end.
     
    KelsoMRK likes this.
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Unless @Xype has tangible test results with sample code that we can all look at. But we're firmly in the "put up or shut up" zone at this point.
     
    Suddoha likes this.
  6. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I like these types of discussions because it makes me listen and learn. I did a little searching and found that coroutines can cause extra garbage collection and there are a few practices that make it more efficient. The one I found interesting and easy to do is this:
    Code (csharp):
    1.  
    2. WaitForSeconds delay = new WaitForSeconds(1f);
    3.  
    4. while (!isComplete)
    5. {
    6.    yield return delay;
    7. }
    8.  
    also returning null is better than returning 0, even though they do the same thing.
    There are a lot of tips for using the garbage collector less here:
    https://unity3d.com/learn/tutorials...ion/optimizing-garbage-collection-unity-games
     
    chelnok and KelsoMRK like this.