Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

MainLoop

Inherits: Object

Inherited By: SceneTree

Abstract base class for the game's main loop.

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 "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

void

_finalize ( ) virtual

void

_initialize ( ) virtual

bool

_physics_process ( float delta ) virtual

bool

_process ( float delta ) virtual


Signals

on_request_permissions_result ( String permission, bool granted )

Emitted when a user responds to a permission request.


Constants

NOTIFICATION_OS_MEMORY_WARNING = 2009

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

Specific to the iOS platform.

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.

NOTIFICATION_WM_ABOUT = 2011

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

Specific to the macOS platform.

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.

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).

Specific to the macOS platform.

NOTIFICATION_APPLICATION_RESUMED = 2014

Notification received from the OS when the application is resumed.

Specific to the Android platform.

NOTIFICATION_APPLICATION_PAUSED = 2015

Notification received from the OS when the application is paused.

Specific to the Android platform.

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 thir