Skip to content

WaitForFixedUpdate incorrect finishFrame #18

@cfoulston

Description

@cfoulston

When calling await Await.FixedUpdates(1) inside a while loop. The iterator will not continue until the fixedCount reaches updateCount.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityAsync;

public class TestAsync : MonoBehaviour {

	#region Private Methods

	public void DoRoutineAtSomeTime() { 

		DoRoutine();
	}

	private async void DoRoutine() {

		while (isActiveAndEnabled) {

			Debug.Log("Before " + Time.inFixedTimeStep);

			await Await.FixedUpdates(1).ConfigureAwait(this);

			//This will not occur until a long time later, depending on when "DoRoutine" was called

			Debug.Log("After " + Time.inFixedTimeStep);
		}

		Debug.Log("Finished");
	}

	#endregion
}

This is because in the constructor of "WaitForFrames" the finishFrame is set to CurrentFrameCount + count

#if UNITY_EDITOR
using UnityEngine;
#endif

namespace UnityAsync
{
	public struct WaitForFrames : IAwaitInstruction
	{
		readonly int finishFrame;

		bool IAwaitInstruction.IsCompleted() => finishFrame <= AsyncManager.CurrentFrameCount;
		
		/// <summary>
		/// Waits for the specified number of frames to pass before continuing.
		/// </summary>
		public WaitForFrames(int count)
		{
			#if UNITY_EDITOR
			if(count <= 0)
			{
				count = 1;
				Debug.LogError($"{nameof(count)} should be greater than 0. This check will only appear in edit mode.");
			}
			#endif

			finishFrame = AsyncManager.CurrentFrameCount + count;
		}
	}
}

So if you call Await.FixedUpdates(1) when not inside a FixedUpdate callback, the finishFrame is set to the AsyncManager.updateCount instead of AsyncManager.fixedCount

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions