Index · Object → MainLoop

MainLoop

Abstract base class for the game's main loop.

Inherited by (1): SceneTree
Class Description

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:

class_name CustomMainLoop
extends MainLoop

var time_elapsed = 0

func _initialize():
	print("Initialized:")
	print("  Starting time: %s" % str(time_elapsed))

func _process(delta):
	time_elapsed += delta
	# Return true to end the main loop.
	return Input.get_mouse_button_mask() != 0 || Input.is_key_pressed(KEY_ESCAPE)

func _finalize():
	print("Finalized:")
	print("  End time: %s" % str(time_elapsed))

Methods

func _finalize() -> void

Called before the program exits.

func _initialize() -> void

Called once during initialization.

func _physics_process(delta: float) -> bool

Called each physics tick. delta is the logical time between physics ticks in seconds and is equal to Engine.time_scale / Engine.physics_ticks_per_second. 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 step.

Note: _physics_process() may be called up to Engine.max_physics_steps_per_frame times per (idle) frame. This step limit may be reached when the engine is suffering performance issues.

Note: Accumulated delta may diverge from real world seconds.

func _process(delta: float) -> bool

Called on each idle frame, prior to rendering, and after physics ticks have been processed. delta is the time between frames 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: When the engine is struggling and the frame rate is lowered, delta will increase. When delta is increased, it's capped at a maximum of Engine.time_scale * Engine.max_physics_steps_per_frame / Engine.physics_ticks_per_second. As a result, accumulated delta may not represent real world time.

Note: When --fixed-fps is enabled or the engine is running in Movie Maker mode (see MovieWriter), process delta will always be the same for every frame, regardless of how much time the frame took to render.

Note: Frame delta may be post-processed by OS.delta_smoothing if this is enabled for the project.

Inherited from Object (62)
func _get(property: StringName) -> Variant
func _get_property_list() -> Dictionary[]
func _init() -> void
func _iter_get(iter: Variant) -> Variant
func _iter_init(iter: Array) -> bool
func _iter_next(iter: Array) -> bool
func _notification(what: int) -> void
func _set(property: StringName, value: Variant) -> bool
func _to_string() -> String
func _validate_property(property: Dictionary) -> void
func add_user_signal(signal: String, arguments: Array = []) -> void
func call(method: StringName) -> Variant
func callv(method: StringName, arg_array: Array) -> Variant
func cancel_free() -> void
func connect(signal: StringName, callable: Callable, flags: int = 0) -> Error
func disconnect(signal: StringName, callable: Callable) -> void
func emit_signal(signal: StringName) -> Error
func free() -> void
func get(property: StringName) -> Variant
func get_class() -> String
func get_incoming_connections() -> Dictionary[]
func get_indexed(property_path: NodePath) -> Variant
func get_meta(name: StringName, default: Variant = null) -> Variant
func get_meta_list() -> StringName[]
func get_method_list() -> Dictionary[]
func get_property_list() -> Dictionary[]
func get_signal_connection_list(signal: StringName) -> Dictionary[]
func get_signal_list() -> Dictionary[]
func has_meta(name: StringName) -> bool
func has_method(method: StringName) -> bool
func has_signal(signal: StringName) -> bool
func is_class(class: StringName) -> bool
func is_connected(signal: StringName, callable: Callable) -> bool
func notification(what: int, reversed: bool = false) -> void
func remove_meta(name: StringName) -> void
func remove_user_signal(signal: StringName) -> void
func set(property: StringName, value: Variant) -> void
func set_block_signals(enable: bool) -> void
func set_deferred(property: StringName, value: Variant) -> void
func set_indexed(property_path: NodePath, value: Variant) -> void
func set_message_translation(enable: bool) -> void
func set_meta(name: StringName, value: Variant) -> void
func set_script(script: Variant) -> void
func set_translation_domain(domain: StringName) -> void
func to_string() -> String
func tr(message: StringName, context: StringName = &"") -> String
func tr_n(message: StringName, plural_message: StringName, n: int, context: StringName = &"") -> String

Signals

signal on_request_permissions_result(permission: String, granted: bool)

Emitted when a user responds to a permission request.

Inherited from Object (2)

Enums & Constants

const NOTIFICATION_OS_MEMORY_WARNING = 2009

Notification received from the OS when the application is exceeding its allocated memory.

Specific to the iOS platform.

const NOTIFICATION_TRANSLATION_CHANGED = 2010

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 Object.tr().

const NOTIFICATION_WM_ABOUT = 2011

Notification received from the OS when a request for "About" information is sent.

Specific to the macOS platform.

const NOTIFICATION_CRASH = 2012

Notification received from Godot's crash handler when the engine is about to crash.

Implemented on desktop platforms if the crash handler is enabled.

const NOTIFICATION_OS_IME_UPDATE = 2013

Notification received from the OS when an update of the Input Method Engine occurs (e.g. change of IME cursor position or composition string).

Implemented on desktop and web platforms.

const NOTIFICATION_APPLICATION_RESUMED = 2014

Notification received from the OS when the application is resumed.

Specific to the Android and iOS platforms.

const NOTIFICATION_APPLICATION_PAUSED = 2015

Notification received from the OS when the application is paused.

Specific to the Android and iOS platforms.

Note: On iOS, you only have approximately 5 seconds to finish a task started by this signal. If you go over this allotment, iOS will kill the app instead of pausing it.

const NOTIFICATION_APPLICATION_FOCUS_IN = 2016

Notification received from the OS when the application is focused, i.e. when changing the focus from the OS desktop or a thirdparty application to any open window of the Godot instance.

Implemented on desktop and mobile platforms.

const NOTIFICATION_APPLICATION_FOCUS_OUT = 2017

Notification received from the OS when the application is defocused, i.e. when changing the focus from any open window of the Godot instance to the OS desktop or a thirdparty application.

Implemented on desktop and mobile platforms.

const NOTIFICATION_TEXT_SERVER_CHANGED = 2018

Notification received when text server is changed.

const NOTIFICATION_APPLICATION_PIP_MODE_ENTERED = 2019

Notification received when the application enters picture-in-picture mode.

const NOTIFICATION_APPLICATION_PIP_MODE_EXITED = 2020

Notification received when the application exits picture-in-picture mode.