_physics_process Being Called Twice Per Frame

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bribat123

I have been continuing to debug into a problem with moving the player in my game and discovered that the _physics_process() function seems to being called twice for each frame.

var current_frame = 0
func _physics_process(delta)
    current_frame += 1
    print(current_frame)

Output:
1
1
2
2
3
3

:bust_in_silhouette: Reply From: Zylann

Assuming your code is the only contents of your script, and that it has only one instance in the game, then what you are getting (several times 1, several times 2 etc) is impossible.

Make sure you don’t have two instances of the node in your scene, and make sure nothing else adds +1 to current_frame.

Edit: the printed output aside, it is totally possible _physics_process is called multiple times per frame, or sometimes even 0 times. niceeffort’s answer explains this in detail.

I took a look at the engine source code. In main.cpp in the Main::iteration() function you can see this:

for (int iters = 0; iters < advance.physics_steps; ++iters)

Based on my understanding this is the engine looping through the number of physics steps it needs to in order to catch up when it falls behind. The max steps it will take is 8.

static const int max_physics_steps = 8;
if (fixed_fps == -1 && advance.physics_steps > max_physics_steps) {
	step -= (advance.physics_steps - max_physics_steps) * frame_slice;
	advance.physics_steps = max_physics_steps;
}

If your game starts running slow enough to need more than one step you will see _physics_process() called multiple times per frame. At 30 FPS you should see two calls to it per frame if the step is 15ms. I verified this happening in my project as well and saw up to 4 calls when it fell too far behind.

I added the reasoning for this implementation in my answer.

niceeffort | 2020-04-23 00:35

My answer might be misleading, I was referring to the printed output: it doesn’t matter how many times per frame _physics_process is being called, because in the posted code, +1 is added each time the function is called regardless, just before being printed. That’s why I think getting 1 printed several times is impossible.

However, I agree that _physics_process can be called 0 times, or more than 1 time per frame.

Zylann | 2020-04-23 00:40

Ah. I see your point now and totally agree. Well, now we have a very thorough combined answer. I appreciate your follow up.

niceeffort | 2020-04-23 03:37

:bust_in_silhouette: Reply From: niceeffort

In order for the physics calculations to run properly, _physics_process() must be called with a consistent delta. If your game slows down to the point that a single frame takes twice as long as a physics step, then the engine will need to call _physics_process() twice in one frame to catch up. Physics runs at 15ms per step (60FPS), if your game’s frame time is running at 30ms (30 FPS) I would expect _physics_process to get called twice.

It can actually get called even more than that if your game runs slower. The reason for this is that you can’t just throw away the time when doing physics calculations or you will see time slow down in the game. Conversely, if you take inconsistent step sizes you might start missing collisions.

You can use the built-in profiler to help see what is causing your game to drop below 60 FPS, or you can just live with two calls to _physics_process if your current framerate is acceptable.

Hope this helps.