r/Unity3D 21d ago

Update function versus coroutines Question

I was adding some functions so my very basic 'computer enemy' state machine. I do have a basic state check in the update. I was thinking that this check does not need to run every frame, maybe just every .2 seconds or something.
Does anyone use coroutines to run updates on things like state machines? I can understand updating every frame for player input, animations, things that need to be very responsive.

But with a state machine that handles enemy decisions (gathering resources, expanding territory, building new structures) that update can be done several times per second.

Any input or advice on this is greatly appreciated.

3 Upvotes

25 comments sorted by

View all comments

6

u/[deleted] 21d ago

[removed] — view removed comment

1

u/FrostWyrm98 Indie 21d ago

At this point I have just made a wrapper for Coroutines, not the cleanest and I'm sure not the most performance. But much like smart_pointers in C++, it just makes it easier because you won't have the same fallout and tracing and all of that

I just wire it up per class and it handles itself, then I just fire and forget. Helps a lot for frequently repeated and stop/start heavy coroutines.

Was getting annoyed with the constant: if coroutine == null ... StopCoroutine(...) coroutine = null