- Home /
Can you run a coroutine more often than Update?
I have a diffusion simulation that runs locally in Unity. I use a coroutine to simulate a simulation step, so that I can run it less often than Update to help performance if I need to:
private IEnumerator CalculateSimulationStep()
{
while(true)
{
...simulate diffusion...
yield return new WaitForSeconds(1 / stepsPerSec)
}
}
But I want to calculate as many steps as reasonable performance will allow, since more data is better. So I tried making stepsPerSec responsive to a separate FPS counter's readings:
private void AdaptStepsPerSec()
{
if (curFPS.AverageFPS < 30)
{ // If we're running too slowly, do fewer simulation steps per second
stepsPerSec--;
}
else
{ // If we're above our ideal FPS, we can afford to do more simulation steps
stepsPerSec++;
}
if(stepsPerSec < 5)
{ // Limit our steps per second to 5
stepsPerSec = 5;
}
}
After writing this code, my stepsPerSec started skyrocketing up into the 1000's without any performance drop, and with my simulation loop still seeming to only be running 30ish times per second.
It makes me think that coroutines can't update more often than the Update function. I believe this is because the game logic loop will only check if a coroutine is due to run once per frame.
It would be fine to only run this once per frame; You literally could not see changes in the data more often than once per frame, and if I run WaitForSeconds(0) then I essentially have an Update function. But is it possible to run a coroutine more often than once per frame?
Answer by troien · Aug 21, 2019 at 07:41 PM
The docs state that WaitForSeconds resumes on the first frame after 't' seconds has passed. Meaning WaitForSeconds will always wait at least one frame, never continue the same frame and thus (provided you yield every frame) never 'run more often than update'. But if you want to run the content of your coroutine more then once per frame/update, you can just do that ofcourse by simply not yielding after 1 iteration. As a simple example that should prove my point:
private IEnumerator CalculateSimulationStep()
{
while(true)
{
for(int i = 0; i < 3; i++)
{
...simulate diffusion...
// Now this will run 3 times each frame...
}
yield return null; // continue next frame (Same as new WaitForSeconds(0) but more efficient)
}
}
You can then write your own logic that calculates how often (if at all) you can run that simulation this frame while still reaching your target fps.
Your answer
Follow this Question
Related Questions
Climbing a block in 2d 0 Answers
How to make functions async? 2 Answers
Flash image while ammo is low - coroutines? 3 Answers
MoveTowards inside Coroutine 2 Answers