- Home /
The question is answered, right answer was accepted
How to slow down large code?
Ive got a lot of code that Im trying to run in a coroutine function. I've tried placing yield wait for seconds at the ends of loops and that helps, but is there a way to just evenly pace out the code?
To be clear, I'm not just trying to slow the loops, but the execution of the entire code block over a variable amount of frames.
Answer by KitoCode · Apr 10, 2015 at 02:19 AM
Coroutines are built to do what I believe you are asking, they work as if you wish to loop code or until a statement is true, then proceeds with the coroutine.
IEnumerator YieldingPossibilities()
{
while (true)
{
print("DoSomething Loop");
yield return null;
}
while (true)
{
print("DoSomething Else Loop");
yield return null;
}
}
The above code will only execute the while loop when it is true, make the while loop false, and it will then proceed to the next while loop.. Rinse and repeat. understand what I am getting at?
On the other hand, if you do not wish to user Co routines, I suggest delegates. Delegates found at this link might help you understand, and it might create a solution to your problem.
...and the idea is that there isn't a way to simply assign a time-slice for the the system to pause your code. You have to manually give up control until next frame with yield return null;
.
With a huge loop, you might estimate that 20 times is enough work for one frame, so use if(i%20==19) yield return null;
IcarOtu: were you thinking the first loop used a break;
? Otherwise it won't reach the second one.