Processing diagram (game loop)
I've made a basic main loop diagram by looking at the code. I'd be willing to improve it and adding it to the documentation, but I don't know where or how. For now, I'd like to make sure it's correct and complete.
I needed to know the order things happen inside the Godot engine to better write code for it. I don't think there's something similar in the documentation. There are some pages that give some information but not the whole picture:
- https://docs.godotengine.org/en/stable/tutorials/scripting/scene_tree.html
- https://docs.godotengine.org/en/stable/tutorials/best_practices/godot_notifications.html
- https://docs.godotengine.org/en/stable/tutorials/scripting/idle_and_physics_processing.html
- https://docs.godotengine.org/en/stable/tutorials/inputs/inputevent.html
Please, add any I may have missed, also corrections and suggestions.
Related issues: #9204 #5492
Processing diagram for Godot 4
MainLoop is the scene tree object. In the default configuration it's an SceneTree object.
It may lack some platform specific information.
- Call
MainLoop._initialize().- Call
DisplayServer::process_events(). Processes window manager events including input. - Process joystick/gamepad events.
- Physics processing step (run as many times as needed).
- Flush input buffered events when
agile_event_flushingis true (Android only). - Call NOTIFICATION_TRANSFORM_CHANGED on nodes and update physics objects transforms.
- Call
MainLoop._physics_process(). - Send signal
SceneTree.physics_frame. - Process object picking.
- Call
_physics_process()in the scene nodes. - Flush unique calls to groups.
- Process timers in physics processing mode.
- Process tweens in physics processing mode.
- Call NOTIFICATION_TRANSFORM_CHANGED on nodes and update physics objects transforms.
- Process
queue_delete()/queue_free()calls. - Process deferred calls.
- Physics server step.
- Flush input buffered events when
- Flush input buffered events when
agile_event_flushingis true (Android only). - Idle processing step.
- Call
MainLoop._process(). - Automatic polling of MultiplayerAPI.
- Call NOTIFICATION_TRANSFORM_CHANGED on nodes and update physics objects transforms.
- Call
_process()in the scene nodes. - Flush unique calls to groups.
- Call NOTIFICATION_TRANSFORM_CHANGED on nodes and update physics objects transforms.
- Process
queue_delete()/queue_free()calls. - Change scene if requested. If changed, emit
scene_changedsignal (Godot 4.5+). - Process timers in idle processing mode.
- Process tweens in idle processing mode.
- Call NOTIFICATION_TRANSFORM_CHANGED on nodes and update physics objects transforms.
- Process deferred calls.
- Call
- Rendering. Emits signals
RenderingServer.frame_pre_drawandRenderingServer.frame_post_draw. - Audio update.
- Debugger iteration.
- Flush input buffered events when
agile_event_flushingis false (Android only). - Add frame to MovieWriter if used.
- Optional frame delay to achieve target fps.
- Call
- Call
MainLoop._finalize().
Related: #9204.
Additional information: #5492
Thank you. I needed to know this for my situation but nowhere was as thorough as I needed, except this fabulous post.