- Home /
While loop VS WaitWhile in coroutines
Hi, everyone! I`m currently working on a game, where I have lots of coroutines and sometimes I need to pause execution of some coroutines. I know two ways to solve this: 1) with while loop and "yield return null" statement 2) to use class WaitWhile (or WaitUntil) What solution is better to use? P.S. sorry for such stupid question:)
i would never use a while with a yield return null, they are basically the same but it is "uglier" i would leave while loops in coroutine for other type of yield like waitforseconds, or if youo need to take into account the number of trys or something like that.
Ok, thank you very much for answer. Then I`ll use WaitUntil or WaitWhile
If your while loop is going to be empty, i.e. containing nothing but the yield, then you may as well use a yield Wait. But if you need to do something in the loop, then a yield Wait simply won't work.
thats true, but he was asking how to pause the corroutine, and thats the exact porpuse of waituntil, but obviosly if you need something to ocurr repe$$anonymous$$dly the WaitUntil wont fit. thats why i said "youo need to take into account the number of trys or something like that use the other one" but probably i didnt explain myself right, i am not native speacker.
Answer by Ampharel · Mar 18, 2019 at 12:43 PM
WaitUntil will hold the execution of the code until whatever value you provide is true. WaitWhile will hold the execution of the code until whatever value you provide is false.
A while loop with yield return null will hold the execution untill it breaks out of the while loop, so it's the same as the WaitWhile call.
In my opinion using the WaitUntil and WaitWhile leads to better readable code, but it's up to your preference really. There don't seem to be any real differences in execution.
Answer by dargonknight · Mar 18, 2019 at 08:55 AM
I would simply create a bool, if the bool is true u do ur actions else u simply keep going normally else you can stop the coroutine save your progress and (depending on what ur doing in ur coroutine) and relaunch it after.
bool running; IEnumerator myCoroutine(float time) { if(running) { yourFunction(); } yield return new WaitForSeconds(time); StartCoroutine(myCoroutine); }
i think it's 1 of the best ways of using coroutines in your case.
that wont work, without a loop the routine will just end. thats the same sscript as @Bluzora but missing the loop part.
@xxmariofer nope unlike @Bluzora my function dosn't lock you in a loop it ll simply not do your action but keep running normally until you want it to do ur action again, while a loop will force it the main difference is a loop will keep running while waitforseconds will significantly reduce the execution (complexity). you (i modified the code so it can run endlessly)
after some test for "looping routines" i ended up with this results: Update event was the fastest way of doing this, restarting over and over the corroutine gave big spikes and waituntil performance was similar than restarting the corroutine but without the spikes.
yes depending on what your doing and how frequent update might be a better option.
Same as with previous answer. Thank you, but that`s not what i`m asking about
Answer by akashdarshan99 · Nov 13, 2020 at 11:49 AM
I'm sorry for bumping such an old thread, but i couldnt stop myself from commenting in here @dargonknight
Your method of recursively starting a coroutine is extremely bad. What you're in your case by removing the loop (supposedly to reduce the time complexity) is a grave mistake because starting a coroutine has its own overhead for calling the native code functions from te managed code, some memory allocations which means more work for the GC aswell. Instead of looping (in which you dont create any additional coroutines) when you recursively start a new coroutine, it creates unnecessary garbage, and you arent improving the time complexity because its still technically a recursion but with additional unnecessary overheads
So please don't do that, its a bad practice
Your answer
Follow this Question
Related Questions
start coroutine everytime 2 Answers
Alternative for semaphores in Unity? 2 Answers
Move a gameobject smoothly without update, without coroutine 2 Answers
Wierd issue with Coroutines? 2 Answers