[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 main [Script] is provided from the command line (with e.g. [code]godot -s my_loop.gd[/code], which should then be a [MainLoop] implementation.
Here is an example script implementing a simple [MainLoop]:
[codeblock]
extends MainLoop
var time_elapsed = 0
var keys_typed = []
var quit = false
func _initialize():
print("Initialized:")
print(" Starting time: %s" % str(time_elapsed))
func _idle(delta):
time_elapsed += delta
# Return true to end the main loop.
return quit
func _input_event(event):
# Record keys.
if event is InputEventKey and event.pressed and !event.echo:
Called when files are dragged from the OS file manager and dropped in the game window. The arguments are a list of file paths and the identifier of the screen where the drag originated.
Called when the user performs an action in the system global menu (e.g. the Mac OS menu bar).
</description>
</method>
<methodname="_idle"qualifiers="virtual">
<returntype="bool"/>
<argumentindex="0"name="delta"type="float"/>
<description>
Called each idle frame with the time since the last idle frame as argument (in seconds). Equivalent to [method Node._process].
If implemented, the method must return a boolean value. [code]true[/code] ends the main loop, while [code]false[/code] lets it proceed to the next frame.
</description>
</method>
<methodname="_initialize"qualifiers="virtual">
<returntype="void"/>
<description>
Called once during initialization.
</description>
</method>
<methodname="_input_event"qualifiers="virtual">
<returntype="void"/>
<argumentindex="0"name="event"type="InputEvent"/>
<description>
Called whenever an [InputEvent] is received by the main loop.
</description>
</method>
<methodname="_input_text"qualifiers="virtual">
<returntype="void"/>
<argumentindex="0"name="text"type="String"/>
<description>
Deprecated callback, does not do anything. Use [method _input_event] to parse text input. Will be removed in Godot 4.0.
</description>
</method>
<methodname="_iteration"qualifiers="virtual">
<returntype="bool"/>
<argumentindex="0"name="delta"type="float"/>
<description>
Called each physics frame with the time since the last physics frame as argument ([code]delta[/code], in seconds). Equivalent to [method Node._physics_process].
If implemented, the method must return a boolean value. [code]true[/code] ends the main loop, while [code]false[/code] lets it proceed to the next frame.
</description>
</method>
<methodname="finish">
<returntype="void"/>
<description>
Should not be called manually, override [method _finalize] instead. Will be removed in Godot 4.0.
</description>
</method>
<methodname="idle">
<returntype="bool"/>
<argumentindex="0"name="delta"type="float"/>
<description>
Should not be called manually, override [method _idle] instead. Will be removed in Godot 4.0.
</description>
</method>
<methodname="init">
<returntype="void"/>
<description>
Should not be called manually, override [method _initialize] instead. Will be removed in Godot 4.0.
</description>
</method>
<methodname="input_event">
<returntype="void"/>
<argumentindex="0"name="event"type="InputEvent"/>
<description>
Should not be called manually, override [method _input_event] instead. Will be removed in Godot 4.0.
</description>
</method>
<methodname="input_text">
<returntype="void"/>
<argumentindex="0"name="text"type="String"/>
<description>
Should not be called manually, override [method _input_text] instead. Will be removed in Godot 4.0.
</description>
</method>
<methodname="iteration">
<returntype="bool"/>
<argumentindex="0"name="delta"type="float"/>
<description>
Should not be called manually, override [method _iteration] instead. Will be removed in Godot 4.0.
Notification received when translations may have changed. Can be triggered by the user changing the locale. Can be used to respond to language changes, for example to change the UI strings on the fly. Useful when working with the built-in translation support, like [method Object.tr].