- Home /
How often does a for loop loop?
Do for loops iterate once per frame? Until the condition is false all in a single frame? Or simply as fast as it can, taking as many frames as it needs?
Answer by fafase · Oct 25, 2013 at 05:25 AM
A basic for loop or foreach or while loop goes within the frame
So if you iterate a large amount of objects you may see a little stop in your game or even a big stop.
You can cut your loop into smaller parts using a coroutine.
For instance if you know you are about to look into a large collection:
IEnumerator IterateLargeCollection(object[] arr)
{
float frequency = 1000;
for (int i = 0;i<arr.Lenght;i++){
//Do your stuff
if(i == frequency)yield return null;
}
}
Every 1000 objects, the loop goes out and comes back where it left off. You can also pack the 1000 objects and return them so that you can use them somewhere else. Well it all depends on what you want to do.
Maybe you did not need that much information...
Wouldn't that yield only the first time it reaches 1000? Also, not good idea to compare int and float. We know how much float likes to turn from 1000 into 1.0000000096e03 and stuff like that.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Shorten code with FOR loop 1 Answer
Play Looped Sound? 4 Answers
Is this loop infinite? 2 Answers
How to correctly shorten this script using arrays and iterations 2 Answers