x


Coroutines vs Update/FixedUpdate/LateUpdate

A friend on twitter, @pixelplacement recently pointed out that he never uses Update anymore, instead preferring co-routines for everything, citing that it makes state management a dream (among other advantages).

Firstly, could someone explain how to replace "Update" with an equivalent coroutine, and variously how you sequence things under this paradigm, and secondly, are there any thoughts (Unity engine developers especially) as to whether this is a good way to go?

more ▼

asked May 20 '11 at 08:35 AM

runonthespot gravatar image

runonthespot
514 19 21 36

Think of something specific which shouldn't run 60 times/second, like AI, enemy spawning, flamethrower damage ticks... and mess around with converting it to a coroutine.

May 20 '11 at 06:26 PM Owen Reynolds
(comments are locked)
10|3000 characters needed characters left

2 answers: sort oldest

If you want to literally replace Update with a coroutine, then do this:

function Start () {
    while (true) {
        // do stuff here that you'd normally do in Update
        yield;
    }
}

If that's all you're doing, then it has no advantage over Update. I wouldn't say never use Update; it's good for things which just do the same thing every frame. Coroutines are good for when you want to schedule events. For example, if you have an object moving forward, but you want it to stop for a second when you hit a key, you would do something like this if you use Update:

private var wait = false;
private var timer : float;

function Update () {
    if (!wait) {
       transform.Translate(Vector3.forward * Time.deltaTime);
    }
    else if (Time.time >= timer) {
       wait = false;
    }
    if (Input.anyKeyDown) {
       wait = true;
       timer = Time.time + 1.0;
    }
}

The thing is, that code isn't doing the same thing every frame. Sometimes it's doing one thing, sometimes another. So let's rewrite that as a coroutine:

function Start () {
    while (true) {
        transform.Translate(Vector3.forward * Time.deltaTime);
        if (Input.anyKeyDown) {
            yield WaitForSeconds(1.0);
        }
        yield;
    }
}

As you can see, there are no more global variables and slightly convoluted if/then logic. The code is shorter and basically self-documenting: "If you press any key, wait for a second." And this is just a very simple example...as states get more complex, coroutines get more powerful. See here for a somewhat more complex example.

more ▼

answered May 20 '11 at 04:22 PM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

Sorry but your update code is not that good, you use too much things to achieve what you want.

private float timer = 0;

void Update() {
    timer -= Time.deltaTime;

    if (timer <= 0.0f) {
        transform.Translate(Vector3.forward * Time.deltaTime);
    }

    if (Input.anyKeyDown) {
        timer = 1.0f;
    }
}
Sep 07 '12 at 08:57 AM Cripple

@Cripple I think the "self documenting" aspect of Eric5h5's coroutine still beats it though. Subsequent to this question I've found I've used coroutines a lot more, but I still favour Update in a lot of situations as I find coroutines can do many strange and unexpected things that are hard to debug. An Update at least does what it says on the tin.

Sep 07 '12 at 09:06 AM runonthespot
(comments are locked)
10|3000 characters needed characters left

Co-routines are fantastic for performance. Sometimes you have to perform actions every frames, often you don't. Co-routines are functions you can call every few seconds to do stuff like AI. I've never really considered it for state management but I'm pretty new to coding and tend to use booleans for that ;)

Here is the overview for co-routines:

http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html

and the script ref:

http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.StartCoroutine.html

more ▼

answered May 20 '11 at 08:44 AM

demize2010 gravatar image

demize2010
945 19 22 35

Thanks for the answer @demize2010 I am vaguely familiar with the concept of coroutines, but am confused mostly as to how you'd get away with completely leaving Update() behind, without basically reinventing it? Isn't Update() effectively a coroutine?

May 20 '11 at 08:49 AM runonthespot

It totally depends on your game and the script your writing. In theory you could just kick off a whole load of co-routines from start. You will still need to use update for something though I suspect.

To be honest In the game we are working on at the moment I think there are less then 20 lines of code which sit in update in the entire project - most stuff is functions called by events or co-routines ;)

May 20 '11 at 09:07 AM demize2010
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x495
x74
x63
x21

asked: May 20 '11 at 08:35 AM

Seen: 5173 times

Last Updated: Sep 07 '12 at 09:06 AM