Game loop
- Last UpdatedDec 04, 2023
- 1 minute read
Game loop is the main logical loop of the engine. Its main functions are:
-
updateScene: Actual logic update.
-
syncUpdate: Responsible for updating engine for some shared functionalities, such as audio playing and animations.
-
stepPhysx: Triggers Physx system update.
Additionally, game loop updates and reads data managed together with many other threads, such as messaging and 2D graphics.
Game Loop execution
You can execute all three game loop functions more than once in a single loop, according to a specific policy.
UpdateScene
If the time of the last updateScene execution (delta) is lower than 4/FPSGame (Scene.fps.game parameter of config.xml), updateScene is called one time only. If the value is larger, the function can be called 2 to N times according to the elapsed time.
Scenario example
If FPSGame is set to 100:
-
If delta is less than 0.04 seconds (more than 25 fps) > updateScene is executed 1 time.
-
If delta is 0.06 seconds (16 fps) > updateScene is executed 2 times.
-
If delta is 0.1 seconds (10 fps) > updateScene is executed 3 times.
SyncUpdate
SyncUpdate considers the time elapsed from last syncUpdate call.
Then, it divides it by the supposed rendering time (1/FPSGame).
The quotient of the division defines how many times the syncUpdate must be called (0 to N).
UpdatePhysx
UpdatePhysx considers the time elapsed from the last updatePhysx call.
Then, divides it by the supposed physx update time (1/FPSPhysx) (Graphics/physx/simPerSec). The quotient of the division defines how many times the updatePhysx must be called (0 to 3). In this case, 3 is the maximum allowed value.