MainLoop is the abstract base class for a Godot project's game loop. It is inherited by SceneTree, which is the default game loop implementation used in Godot projects, though it is also possible to write and use one's own MainLoop subclass instead of the scene tree.
Upon the application start, a MainLoop implementation must be provided to the OS; otherwise, the application will exit. This happens automatically (and a SceneTree is created) unless a MainLoop Script is provided from the command line (with e.g. godot -s my_loop.gd
) or the ProjectSettings.application/run/main_loop_type project setting is overwritten.
Here is an example script implementing a simple MainLoop:
Emitted when a user responds to a permission request.
Called before the program exits.
Called once during initialization.
Called each physics frame with the time since the last physics frame as argument (delta
, in seconds). Equivalent to Node._physics_process().
If implemented, the method must return a boolean value. true
ends the main loop, while false
lets it proceed to the next frame.
Note: delta
will be larger than expected if running at a framerate lower than Engine.physics_ticks_per_second / Engine.max_physics_steps_per_frame FPS. This is done to avoid "spiral of death" scenarios where performance would plummet due to an ever-increasing number of physics steps per frame. This behavior affects both _process() and _physics_process(). As a result, avoid using delta
for time measurements in real-world seconds. Use the Time singleton's methods for this purpose instead, such as Time.get_ticks_usec().
Called each process (idle) frame with the time since the last process frame as argument (in seconds). Equivalent to Node._process().
If implemented, the method must return a boolean value. true
ends the main loop, while false
lets it proceed to the next frame.
Note: delta
will be larger than expected if running at a framerate lower than Engine.physics_ticks_per_second / Engine.max_physics_steps_per_frame FPS. This is done to avoid "spiral of death" scenarios where performance would plummet due to an ever-increasing number of physics steps per frame. This behavior affects both _process() and _physics_process(). As a result, avoid using delta
for time measurements in real-world seconds. Use the Time singleton's methods for this purpose instead, such as Time.get_ticks_usec().