What is a good way to to ensure that FixedUpdate is performed every frame?

Hello.

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.

  • Register it’s changed playerLoop calling PlayerLoop.SetPlayerLoop

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.

Thanks!

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.

1 Like

Is it possible that you mixed them both up? FixedUpdate can be called zero, one or multiple times per frame.

There has been a discussion about this recently.

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.

3 Likes

indeed - thanks for correcting my misunderstanding, @exiguous

1 Like

Why the hell would you want to do this?

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.

3 Likes

Don’t try to reshape FixedUpdate() to do what it doesn’t do.

Here is some timing diagram help:

1 Like

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.

If you’re listening to and paying attention to Uncle Joe, you’re doing something right.

3 Likes

This seems like another one of those crazy-insane-solutions to probably-very-simple-problems.

I am also very curious as to why you would want to do such a thing. Why not just use Update, or LateUpdate?

If 2020 has taught me anything, it’s that some people want to see the world burn.

3 Likes