Skip to content

Commit

Permalink
fix multiple FixedUpdate invokes to work
Browse files Browse the repository at this point in the history
  • Loading branch information
Eddio0141 committed Aug 27, 2024
1 parent db3f831 commit efd1c60
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
10 changes: 7 additions & 3 deletions UniTAS/Patcher/Implementations/UnityEvents/UnityEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,6 @@ public void InvokeUpdate()
if (_updated) return;
_updated = true;

_calledFixedUpdate = false;

#if TRACE
StaticLogger.Trace($"InvokeUpdate, time: {_patchReverseInvoker.Invoke(() => Time.time)}");
#endif
Expand Down Expand Up @@ -503,6 +501,12 @@ public void InvokeLateUpdate()
}
}

// isn't called at the very first yield WaitForFixedUpdate, but this is enough
public void CoroutineFixedUpdate()
{
_calledFixedUpdate = false;
}

public void InvokeFixedUpdate()
{
if (_calledFixedUpdate) return;
Expand Down Expand Up @@ -557,4 +561,4 @@ private void InvokeCallOnPreUpdate()
preUpdate();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private void Awake()

_monoBehEventInvoker.InvokeAwake();

StartCoroutine(EndOfFrame());
StartCoroutine(WhileCoroutine());
}

private void OnDestroy()
Expand Down Expand Up @@ -62,11 +62,14 @@ private void OnEnable()

// stupid optimization since object alloc
private readonly WaitForEndOfFrame _waitForEndOfFrame = new();
private readonly WaitForFixedUpdate _waitForFixedUpdate = new();

private IEnumerator EndOfFrame()
private IEnumerator WhileCoroutine()
{
while (true)
{
yield return _waitForFixedUpdate;
_monoBehEventInvoker.CoroutineFixedUpdate();
yield return _waitForEndOfFrame;
_monoBehEventInvoker.InvokeLastUpdate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public interface IMonoBehEventInvoker
void InvokeLastUpdate();
void InvokeOnGUI();
void InvokeOnEnable();
void CoroutineFixedUpdate();
}

0 comments on commit efd1c60

Please sign in to comment.