I’ve recently run into a little snag with a coroutine problem. My goal is to break up processing of a chunk of data over a series of frames if it can’t handle it all at once. To test this concept, I came up with a simple loop through a coroutine that just increments an integer to an arbitrary value too high for it to process all of the steps in under a second.
I test to see if the coroutine has taken up too much time, and if it has, I pass off the remainder of the processing to the next fixed update.
Now, for this particular problem, I could very easily put all the relevant code, including a WaitForFixedUpdate() inside the modulo check (in which case Time.time does update) and then just set the modulo value to a number low enough to consistently break it up across frames. But I can only configure that against my system. I can’t necessarily predict what the frame rate will be on another system, which is why I wanted to use Time.time in the first place. (In other words, putting WaitForFixedUpdate() inside the modulo block defeats the purpose of having the Time.time block at all.)
I’ve attached both the relevant code block (started with StartCoroutine(“FindNewGoal”) and the log output of this method.
TL;DR: Can anyone tell me why Time.time isn’t updating within the coroutine?
private IEnumerator FindNewGoal(){
long i = 0;
yield return new WaitForSeconds(1.0f);
float timeStarted = Time.time;
float currentTime = timeStarted;
float nextCycle = timeStarted + 0.01f;
Debug.Log("Time FindNewGoal Started: " + Time.time);
for( i = 0; i < 4294967 ; ++i ){
if( Time.time > nextCycle ){
currentTime = Time.time;
nextCycle = currentTime + 0.01f;
Debug.Log("Current Time: " + currentTime + "\nNext Cycle: " + nextCycle + "\nCurrent Value: " + i);
yield return new WaitForFixedUpdate();
}else
if( i % 1000000 == 0 ){
//Timer has failed
Debug.Log("Time for i = " + i + " is: " + Time.time );
}
}
Debug.Log( "FindNewGoal finished with value " + i + " at " + Time.time );
yield break;
}
The log output is:
Time FindNewGoal Started: 1.008454
Time for i = 0 is: 1.008454
Time for i = 1000000 is: 1.008454
Time for i = 2000000 is: 1.008454
Time for i = 3000000 is: 1.008454
Time for i = 4000000 is: 1.008454
FindNewGoal finished with value 4294967 at 1.008454