godot.MainLoop
is the abstract base class for a Godot project's game loop. It is inherited by godot.SceneTree
, which is the default game loop implementation used in Godot projects, though it is also possible to write and use one's own godot.MainLoop
subclass instead of the scene tree.
Upon the application start, a godot.MainLoop
implementation must be provided to the OS; otherwise, the application will exit. This happens automatically (and a godot.SceneTree
is created) unless a main godot.Script
is provided from the command line (with e.g. godot -s my_loop.gd
, which should then be a godot.MainLoop
implementation.
Here is an example script implementing a simple godot.MainLoop
:
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:
keys_typed.append(OS.get_scancode_string(event.scancode))
# Quit on Escape press.
if event.scancode == KEY_ESCAPE:
quit = true
# Quit on any mouse click.
if event is InputEventMouseButton:
quit = true
func _finalize():
print("Finalized:")
print(" End time: %s" % str(time_elapsed))
print(" Keys typed: %s" % var2str(keys_typed))
Static variables
staticread onlyNOTIFICATION_APP_PAUSED:Int
Notification received from the OS when the app is paused.
Specific to the Android platform.
staticread onlyNOTIFICATION_APP_RESUMED:Int
Notification received from the OS when the app is resumed.
Specific to the Android platform.
staticread onlyNOTIFICATION_CRASH:Int
Notification received from Godot's crash handler when the engine is about to crash.
Implemented on desktop platforms if the crash handler is enabled.
staticread onlyNOTIFICATION_OS_IME_UPDATE:Int
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.
staticread onlyNOTIFICATION_OS_MEMORY_WARNING:Int
Notification received from the OS when the application is exceeding its allocated memory.
Specific to the iOS platform.
staticread onlyNOTIFICATION_TRANSLATION_CHANGED:Int
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 godot.Object.tr
.
staticread onlyNOTIFICATION_WM_ABOUT:Int
Notification received from the OS when a request for "About" information is sent.
Specific to the macOS platform.
staticread onlyNOTIFICATION_WM_FOCUS_IN:Int
Notification received from the OS when the game window is focused.
Implemented on all platforms.
staticread onlyNOTIFICATION_WM_FOCUS_OUT:Int
Notification received from the OS when the game window is unfocused.
Implemented on all platforms.
staticread onlyNOTIFICATION_WM_GO_BACK_REQUEST:Int
Notification received from the OS when a go back request is sent (e.g. pressing the "Back" button on Android).
Specific to the Android platform.
staticread onlyNOTIFICATION_WM_MOUSE_ENTER:Int
Notification received from the OS when the mouse enters the game window.
Implemented on desktop and web platforms.
staticread onlyNOTIFICATION_WM_MOUSE_EXIT:Int
Notification received from the OS when the mouse leaves the game window.
Implemented on desktop and web platforms.
staticread onlyNOTIFICATION_WM_QUIT_REQUEST:Int
Notification received from the OS when a quit request is sent (e.g. closing the window with a "Close" button or Alt+F4).
Implemented on desktop platforms.
staticread onlyNOTIFICATION_WM_UNFOCUS_REQUEST:Int
Notification received from the OS when an unfocus request is sent (e.g. another OS window wants to take the focus).
No supported platforms currently send this notification.
Constructor
Variables
read onlyonOnRequestPermissionsResult:Signal<(permission:String, granted:Bool) ‑> Void>
on_request_permissions_result
signal.
Methods
_DropFiles(files:HaxeArray<String>, fromScreen:Int):Void
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.
_GlobalMenuAction(id:Dynamic, meta:Dynamic):Void
Called when the user performs an action in the system global menu (e.g. the Mac OS menu bar).
_Idle(delta:Single):Bool
Called each idle frame with the time since the last idle frame as argument (in seconds). Equivalent to godot.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.
_InputText(text:String):Void
Deprecated callback, does not do anything. Use godot.MainLoop._InputEvent
to parse text input. Will be removed in Godot 4.0.
_Iteration(delta:Single):Bool
Called each physics frame with the time since the last physics frame as argument (delta
, in seconds). Equivalent to godot.Node._PhysicsProcess
.
If implemented, the method must return a boolean value. true
ends the main loop, while false
lets it proceed to the next frame.
finish():Void
Should not be called manually, override godot.MainLoop._Finalize
instead. Will be removed in Godot 4.0.
idle(delta:Single):Bool
Should not be called manually, override godot.MainLoop._Idle
instead. Will be removed in Godot 4.0.
init():Void
Should not be called manually, override godot.MainLoop._Initialize
instead. Will be removed in Godot 4.0.
inputEvent(event:InputEvent):Void
Should not be called manually, override godot.MainLoop._InputEvent
instead. Will be removed in Godot 4.0.
inputText(text:String):Void
Should not be called manually, override godot.MainLoop._InputText
instead. Will be removed in Godot 4.0.
iteration(delta:Single):Bool
Should not be called manually, override godot.MainLoop._Iteration
instead. Will be removed in Godot 4.0.