|
So I have done a Tone of research into Coroutines, finding very little on thier limitations or dynamics when it comes to actually using them so please Correct me if any of this is even remotely incorrect. A Coroutine is a function that is starts Compatible functions (those that are IEnumerable and contain a yield). I have also heard that you can't (or there is little point to) start a Coroutine inside the update function (simply because it will update every frame anyways). though i soon realized that Update(); is also a special type of Coroutine (as is fixed update and late update). so I haven't heard my questions asked yet so I thought I would give them a try.
For any of you who can post an answer I thank you in advance
(comments are locked)
|
|
1: Yes, you can start a coroutine inside Update, and in fact this is a perfectly valid form of control flow (for example, if you want to perform some coroutined action over time after recieving a key-press). So long as you use 'StartCoroutine(nameOfCoroutine(paramaters));', you should be fine. 2: The yield statement can be used like any other line of code, and as such can be controlled by conditionals and loops just like anything else. There is aboslutely no reason why you can't do this. 3: If you want to yield for a function in a different script, first make sure you have a reference to that object (obviously!), and then you can call it just like any other public function, so long as you couch it inside the StartCoroutine method call. So: will work perfectly. 4: Unfortunately, it is not possible to return data from a coroutine (because of the way the scheduler works internally). If you need to read from data that is modified inside the coroutine, you'll have to just refer to members variables (rather than scope variables), since these will exist outside the scope of the coroutine. Outside this, you can read off 'incrementMeOverTime' to find the state of the coroutine, although I admit that this doesn't really feel like an 'elegant' solution. The best way to get around this would be to declare a class to hold your output data, and then pass a reference to that class into the coroutine. Then, when the corutine is finished, you can read the data contained in that class. 5: Every time you use the 'yield' statement, the coroutine will pause execution for at least one frame. If you use 'yield return null', it will still pause for a frame- which allows you to use it a little like a temporary 'Update' function (that only gets executed up until the coroutine finishes). If you use something like 'WaitForSeconds', of course, execution will wait for longer than just one frame. (Code examples are in C# because the syntax for coroutines is clearer in that language, and makes it more obvious exactly what is going on. I have had any number of questions here about JS couroutine, mostly stemming from justified confusion about exactly how they work. My apologies if it's not your preferred language) Used correctly, Coroutines can almost completely supplant the Update loop. They tend to be more efficient, because if you code them right they will only be called when they actually need to do something, not just every frame like Update, and it's extremely easy to insert timing information into them (where you would have to keep and increment a member variable if you were to do that kind of thing in Update). Lately I've been reimplementing a lot of my older code to use them more extensively, because they make control flow more generally intuitive. While it takes a little bit of practice to get your head around exactly what can and cannot be done with them (for example, you can't pass references to value types into a coroutine- however, you can pass a reference to a reference type), coroutines are a very powerful tool in your shed, and you would do well to familiarise yourself with them.
(comments are locked)
|
If you want to "demystify" coroutines, just start learning about IEnumerator or IEnumerable, yield in general and you will understand them in much more depth.
Mar 26 '12 at 08:59 AM
Statement ♦♦
(comments are locked)
|
|
I am using coroutines for many tasks but it is exceptionally useful for clearing scene after operation is performed. It shouldn't be however used in all cases. In post Auto destroying particle system in unity3d on my blog I am explaining behavior difference between using update and coroutine when autodestroying particle system. Have a look you might find it usefull.
(comments are locked)
|

yield just means WAIT UNTIL THE NEXT FRAME - it's that simple man.
a concept I love is scheduling on the next frame ... investigation here ...
http://answers.unity3d.com/questions/347468/how-to-invoke-one-frame.html
Also exactly as Sycla says, you can be your own better run loop in life if you use yield the right way.
Simplistically, "yield return null" means "wait for next frame". It is very possible to yield on other things too. (See syclamoth's answer).
indeed, in US you can simply say "yield;" to yield to the next frame