- Home /
Character AI - Are Coroutines less heavy than using functions kept in update
I'm making some fairly basic AI functionality for a variety of entities in my 2D game. The AI itself is not very heavy, its fairly basic, alert status, proximity detector, random walking when idle, and non-visible health and attacking features.
I imagine in one scene I could have anywhere from up to twenty or even forty entities with individual AI. At which it may get a little bit clunky. I've tried two options, to make the AI function solely within Update and FixedUpdate, calling outside functions for specific actions, like turning, attacking, jumping. I've also tried making the same type of AI and keeping it almost entirely within a Coroutine. I found that a Coroutine was much easier, took less lines of code, and kept my code a bit more neat and tidy (also easier to read).
But my main concern is (Since I don't really know much about how C# reads), even though a coroutine is easier for me to use, is it more or less heavy on the game? If it is too minor to really matter, no problem, but I would be interested to know which would be better practice.
I prefer Coroutines for tasks that linked with time - Update and FixedUpdate based on frames, so FPS parameter will affect it, Coroutines and their WaitForSeconds works perfectly for tasks that need to wait some time, not frames.
I think each yield produces a tiny bit of garbage (9 bytes?), also running many coroutines at once can drive your framerate to the ground if you're not careful. Coroutines are very useful, especially for time driven sequences. As long as you're not going overboard and replacing every Update with a coroutine, and have 10000 coroutines running at once, then you should be fine.
Answer by ninja_gear · Jun 02, 2016 at 05:52 PM
Code isn't any less cheaper then code. Its not that Coroutines are 'better' then Update. BUT! You can make Coroutines run less often then Update via WaitForSeconds().
Answer by ericbegue · Jun 02, 2016 at 08:25 PM
There is not much difference between Update and couroutines. In fact, Coroutines are called at the same frequency as Update. Performance-wise, they are equivalent.
Thought, there is a known issue with coroutine about memory allocation, but that's being fixed: https://issuetracker.unity3d.com/issues/coroutines-generate-garbage-in-movenext
Excepted for this specific issue, it does matter whether you are using Update or Coroutine.
About keeping your code neat and tidy, you might be interested in Behaviour Tree, which is a great AI tool to describe AI behaviour and could be a good alternative to coroutines.
I'm the author of Panda BT (www.pandabehaviour.com), it's a script based behaviour tree engine.
Using this tool, defining an AI consists of breaking down your logic into simple tasks (implemented as C# function) which are then assemble into an hierarchical structure (a tree) by the mean of a BT script, which defines how and when these tasks are executed.
Furthermore, the execution of the tree can be visualized at runtime, giving you detailed information about what the AI is currently doing, which is extremely valuable for debugging.
If you have question about using this tool, you're welcome on this thread.