I wrote the following code to realize an exactly-once FixedUpdate function call in an every frame. Is there a better implementation for Unity?
// get current player loop.
var playerLoop = PlayerLoop.GetCurrentPlayerLoop();
var currrentFixedUpdateLoopSystem = playerLoop.subSystemList[2];
var currentPreUpdateLoopSystem = playerLoop.subSystemList[3];
// move all function in FixedUpdate to PreUpdate.
playerLoop.subSystemList[2].subSystemList =
(PlayerLoopSystem[])null;
var newPreUpdateSubSystem =
new List<PlayerLoopSystem>(
fixedUpdateSubSystem.Count + preUpdateSubSystem.Count);
newPreUpdateSubSystem.AddRange(fixedUpdateLoopSystem.subSystemList);
newPreUpdateSubSystem.AddRange(preUpdateLoopSystem.subSystemList);
playerLoop.subSystemList[3].subSystemList
= newPreUpdateSubSystem.ToArray();
// register my own player loop.
PlayerLoop.SetPlayerLoop(playerLoop);
The above code does the following.
Get the current player loop by calling PlayerLoop.GetCurrentPlayerLoop
Move all subSystemList in FixedUpdate to PreUpdate.
Because the functions in PreUpdate are called every frame, I moved all subSystemList in FixedUpdate to PreUpdate. To keep the execution order same, all function are inserted in front of each other.
The reason why this implementation may be correct that, Only GetCurrentPlayerLoop().subSystemList[2].loopConditionFunction has valid pointer.
I guess, I assume that this determines how many times the function is executed per frame. (Not determine it on each functions.)
This implementation seems to be working as expected.
Maybe reading this will help - I’m not sure what you are trying to accomplish. FixedUpdate() is already guaranteed to be called exactly once per frame - Update() isn’t. Be aware that although FixedUpdate() is called once every frame, it is NOT guaranteed to be called at a fixed time interval. If your frame rate is below the desired fps, it is NOT going to be called at the theoretical time for the targeted fps, merely when the CPU gets around to call it for that particular frame.
Honestly I don’t understand what you are doing in your code. But as far as I know the unity callbacks cannot be called manually. Or at least they should not be called manually. But if your code works tell the guy in the other post about it.
The entire point of having a FixedUpdate is to run in sync with the fixed timestep. So you’ve essentially broken FixedUpdate in order to turn it into another Update.
If you need FixedUpdate to run every frame, you’re doing something wrong. If you’re using FixedUpdate for anything other than preparing for the physics update, you’re doing something wrong.