- Home /
Coroutine called in different instances
hi everyone,
Just curious how coroutines work. I read some threads about the matter but there is still something it is not clear to me. I have multiples enemies, for example 4 enemies, each of them uses a script called EnemyAI and it is attached to the object (enemy), which has a coroutine for Idle, IdleInCombat, Move, MoveInCombat, Attack, etc. Now, the coroutine is called FSM. Question is, what happens really?;
Option 1) Each script of each enemy, calls for coroutine called FSM, it means 5 coroutines work at the same time.
Option 2) Somehow the 5 corutines can interfere each other?
I guess it is option 1),but I am not sure, I am having a lot of troubles and unity editor completely freezing. I even Split the coroutine to two coroutine (one InCombat states and another NonInCombat states), it helped to reduce the issues, but it ends freezing Unity. It worked perfectly when the coroutine was simple with only Idle, Move, ...Just checking if the same name of a coroutine could interfere other objects using instances of the same script.
Thank you in advance,
Answer by Dave-Carlile · Sep 11, 2015 at 08:20 PM
Coroutines are a form of cooperative multitasking. A coroutine will run until it reaches a yield statement, then the next one will run until it reaches a yield, and so on. They are not running at the same time, and they are all running on the main thread so there are no synchronization issues to worry about.
If a coroutine does too much work before yielding then it that can cause issues like what you're seeing. For example if you have something that loops forever and you never yield inside that loop then it will never finish, and since it's running on the main thread then the Unity editor will lock up.
Hi Dave, Thanks for the fast reply. Then if I have one script (EnemyAI) with a coroutine attached to 5 objects, there will be 5 coroutine called by the same name, working normally, but they wont interfere each other. is that Correct?
Thank you in advance
They won't interfere with each other in a threading sense because they all run on the same thread, one after another. But again, if once of them runs a long time without calling yield then it will interfere with everything else running on the main thread, including delaying the other coroutines.
@alexhapki:
Yes, those coroutines never run at the same time. The coroutines run after each other. At no point in time code from two coroutines will run at the same time. Parts of each coroutine will run after each other. For example if your coroutines all have a yield return null;
in a loop, each coroutine will execute one loop iteration each frame. They will be executed just like Update.
Thanks for the clarifation Bunny83. I have a much clearer idea now with all the comments from you and Dave.
Ps. I am trying removing Update() functions as much as possible from Scripts, coroutines is one of the tools seem to help with that.
Why do you want to remove Update functions? Coroutines, when used like update have slightly more overhead. They make sense if you want / need to interrupt the execution or if you need certain things to happen "in order".
Answer by alexhapki · Sep 14, 2015 at 10:08 AM
I dont have a lot of experience at improving scripting efficiencies, I just follow experienced people advice like yours and others, in general I got the idea of using "if statements" for simple checks that might not happen in order. Anything that is being called at all times but only one option can be active, coroutine could be better, for example I am using this corutine for enemy AI, there are a number of finite states, and only one can be active at once; Idle, Move, Attack, Flee, etc. So it does not use continously if, else if, else if, etc. As you mentioned, things happen in order in my AI Script.
on the other hand, for example, in my project I use " if statements" for input keyboard/mouse to control main carácter, . I consider it "simple", no things that happens in order and any imput can happen at anytime.
Regarding efficiency, I try keeping an eye on all comments experienced folk do and learn as much as I can. I also read with interested the following link even though it might be a bit up of date; http://wiki.unity3d.com/index.php?title=General_Performance_Tips
Thanks