|
|
|
 |
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