No-Frame-Rate / Delta-Time Solution

Hey. I recall MrPutuLips asking about disabling frame rate and having the game’s logic and physics run at the same speed awhile back, and this morning I thought of a solution (that I don’t think was presented before…?). Basically, one would like to have the game run as fast as possible, but there’s a couple of problems with the two main methods to do this.

  1. Using logic.setLogicTicRate() with logic.getAverageFrameRate() can be problematic, as the average frame rate obviously depends on how fast the game can run, and can fluctuate up or down. If you set the logic tic rate (or frame rate) to 0, the BGE will freeze. So, this isn’t really ideal.

  2. Disabling the frame rate through the GUI works, but the problem with this method is that physics runs faster.

Well, since setLogicTicRate() sets the logic and physics frame-rates, a simple solution is to disable the frame-rate and use a script to set the physics tic rate to the average frame rate.

Basically:

No “Use Frame Rate” in the GUI > The game runs as fast as possible.

A script setting the logic tic rate > Sets the physics tic rate to run fast as possible.

Here’s a simple example. The right cube is the “System” cube that runs a function to set the physics’ “frame-rate” and keep track of a “delta_time” variable. The example cube falls correctly and moves left using the System module’s delta_time value.

DeltaTimeExample.blend (476 KB)

This is what I had resulted in creating, eventually. The problem that I face is that logic.setPhysicsTicRate() is dependent on a base rate of 60fps, so the logic was fine, but physics was absolutely wild.

Here’s a fixed version of SolarLune’s .blend (won’t/shouldn’t crash)

Edit: attached my version (tictoc). It’s pretty much the same, but demonstrated a bit better.
Explaination:


tics.py: gets the game's average frame rate, sets the logic tic rate to the frame rate, then creates a variable called "dt" (delta time) in the logic (which is basically always imported into every script, so it's better than writing "import <i>x</i>" every time too.

display_time.py: changes the Text object's value to show that time is actually in sync, even though the frame rate is not 60fps.

cube.py: moves cube object 1 blender unit every second. Ruler object confirms this. Prints the logic.dt variable for debugging.

Attachments

DeltaTimeExample.blend (531 KB)tictoc.blend (514 KB)