- Home /
Coroutines vs Update/FixedUpdate/LateUpdate
A friend on twitter, [@pixelplacement](www.twitter.com/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?
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.
Answer by Eric5h5 · May 20, 2011 at 04:22 PM
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.
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.any$$anonymous$$eyDown) {
timer = 1.0f;
}
}
@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.
Am I right in thinking that until a coroutine hits a: yield break;
Then it will continue indefinitely, hence why you can use it as a substitute for an Update function?
Answer by demize2010 · May 20, 2011 at 08:44 AM
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
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?
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 ;)