[Bug]: Coroutines do not yield properly (Unity 2021.3.5f1 and newer)
extraes opened this issue ยท 1 comments
All of the following criteria must be met
- All Requirements must be installed.
- Full
Latest.log
file included. If no file exists then leave this unchecked and state so.
All of the following are optional to answer
- Tried reinstalling the Game.
- Tried reinstalling MelonLoader.
- Tried restarting PC.
- Was able to see the Start Screen.
Describe the issue.
Yielding YieldInstructions, like WaitForSeconds and WaitForSecondsRealtime does nothing except wait a single frame.
The only way to wait a specified number of seconds is to have a timer count up by incrementing using Time.deltaTime or Time.unscaledDeltaTime.
My log file is here
The code used to test is available here
Reimplementing WaitForSeconds and WaitForSecondsRealtime in the managed domain does nothing due to yielded coroutines not being executed.
Did you attach your log file?
- Yes, I attached my log file to the text box above.
- No, I could not find a log file at
{Game_Directory}\MelonLoader\Latest.log
I'm adding to this issue that it seems that yielding a MelonCoroutines.Start()
also returns in the next frame :
System.Collections.IEnumerator CustomWaitForSeconds(float time)
{
float t = 0;
while ((t += Time.deltaTime) < time) yield return null;
}
// Somewhere in another coroutine
Stopwatch stopwatch = new Stopwatch();
stopwatch.Restart();
yield return MelonCoroutines.Start(CustomWaitForSeconds(5f));
stopwatch.Stop();
float swTime = (float)System.Math.Round(stopwatch.ElapsedMilliseconds / 1000.0, 2);
Log($"Stopwatch = {swTime} sec; Stopwatch VS time = {swTime - 5} sec"); // expecting this log 5s after, but is triggered after next frame
Result :
[11:55:14.722] [CoroutineWaiter] Stopwatch = 2.15 sec; Stopwatch VS time = -2.85 sec
Edit: It took 2.15s because it's in the loading screen
I reproduced with the game engine version 2021.3.2f1