C# Magic, dispelling the illusion, illuminating the fun

edited in General
Hi all!

Got a lot of positive comments after that talk, thank you all I'm so glad many of you enjoyed the whirlwind tour!

As promised I'm posting the slides up here (pdf) for people to look over and discuss, also available here as a GoogleDoc (fixed that closure thing and added a warning :D thx for the correction!)

If there are any glaring errors I missed I've still got the Google doc set up so I can make changes.

Otherwise if people have questions on usage, exact details, more on how Coroutines work in Unity (my model was *very* simplified, after all) then here would be a lovely place for us to chat and get those details sorted out for us to nerdgasm about. If I could, I'd suggest not posting "help me with my specific bug in my game" type questions here, you can always make another post... unless it's about how you fixed the bug with some interesting detail about X that everyone should be aware of, then hells yeah! It'd be nice to have a post where people can talk about general programming magic. But hey! what do I know, let's just nerd and see what happens :p


I'm not an expert, but I'll answer what I can. People with strong code-fu around here will probably be willing to share their knowledge as well :)

Love all 'y all ^_^
-rax


p.s: I'm working on a fuller model of how Unity runs its game-loop that I'll post up once I've done a bit or research, I think it might be a enlightening endevour.
pdf
pdf
C# Magic.pdf
4M

Comments

  • edited
    btw, here is my progress on the model of how the Unity loop works (will work on it in the coming days, it's not complete right now! suggestions welcome :) )

    [edit: will fix the colours asap :p can't right now]
  • I have NO idea what you're talking about here XD what is the context here? Sounds exciting :P
  • edited
    I never studied comsci. I can write things that work, and that aren't necessarily performance-hungry, but there are plenty of things that I seldom deal with and therefore simply don't know about that could be really convenient. Delegates, anonymous functions... just need to figure out when would be good times to use them I guess.

    So this was really useful to me. Thanks! :) Wish I could've seen it live.

    [edit] Oh, I have struggled with Coroutines before. Especially if someone were to pause/unpause the game, or resume the game. It seems like quite a mission to find all the coroutines, save how long they've been running or what iteration they're on, stop them... and then start them again with whatever the correct timings are. I've mostly just ended up avoiding them. :(
  • Those slides are AWESOME! Like @Elyaradine I avoid coroutines and lambdas because I always get the feeling I'm doing them wrong :/
  • Great talk tonight! I'm all for having some more programming/down-in-the-trenches talks (as long as they're balanced with other content of course).

    I've seen some cool graphs of how Unity's game loop is constructed - so go out there and find them! :)

    One thing that I would like to elaborate on is WHY to use coroutines, and to provide a small example. Let's say you have a complicated robot that does a million things, one of which is that it has a light on its head that switches between a bunch of different colours every once in a while. Without coroutines, you'd need a light switch timer/counter that decrements in Update and then switches colour, adding bloat to your already massive Update function and adding unnecessary variables EG:

    Update ()
    {
    // do roboty things for about 300 lines
    
      if ((switchLightCounter -= Time.DeltaTime) < 0f)
      {
        switchLightCounter = switchLightDelay;
        if (Light.Color == Color.Red)
        {
          Light.Color = Color.Blue;
        }
        else if (Light.Color == Color.Blue)
        {
          Light.Color = Color.Green;
        }
        else if (Light.Color == Color.Green)
        {
          Light.Color = Color.Red;
        }
    
    // do some more roboty things for another 300 lines
    
    }


    Which could be replaced by a coroutine to do it for you:

    IEnumerable LightSwitchCoroutine()
    {
      while (true)
      {
         Light.Color = Color.Red;
         yield return new WaitForSeconds (switchLightDelay);
         Light.Color = Color.Blue;
         yield return new WaitForSeconds (switchLightDelay);
         Light.Color = Color.Green;
         yield return new WaitForSeconds(switchLightDelay);
      }
    }


    My example is quite silly but I hope it demonstrates imo the biggest advantage of using coroutines: you can keep all the logic for an independent process in its own function AND you maintain the scope you had in that function. I.E. note how in the coroutine I don't have to check what the current state of the light is, as the function continues from the last point I yielded at.

  • edited
    @Elyaradine I have had similar issues with coroutines. Pausing is one culprit; another is when you have nested and branched coroutines (imagine a real-world robot with more complicated cycles that depends on time of day, error conditions, etc.). Although they seem like the text-book solution, I find it very difficult to keep track of the logic. I avoid them too [sardonic smile].

    (This is a shame, because I love the ideas behind it, and indeed use LINQ and enumerators for implementing algorithms all the time).
  • I was discussing about coroutine lifetimes at the meetup and I was under the impression that a coroutine would run even after the MonoBehaviour is destroyed... did some research and this is not the case, a coroutine will stop once the calling Monobehaviour is destroyed (Unity 4.1)... doesn't make sense that it would run afterwards come to think of it
  • @raxter thanks for the talk. Was really interesting and entertaining! My girlfriend enjoyed it too :)
  • I just realised something. If you deactivate a gameobject all coroutines running on that object are stopped and have to be manually restarted. This really, really sucks as we pause all our objects by deactivating them.
  • edited
    @raithza you could create a gameobject that is a singleton that is always active? So AlwaysOnMonoBehaviour.instance.StartCoroutine(...) ?

    [edit] google provides http://wiki.unity3d.com/index.php?title=Singleton ^_^
Sign In or Register to comment.