MainLoop

Inherits: Object

Inherited By: SceneTree

Category: Core

Brief Description

Abstract base class for the game’s main loop.

Methods

void_drop_files ( PoolStringArray files, int from_screen ) virtual
void_finalize ( ) virtual
bool_idle ( float delta ) virtual
void_initialize ( ) virtual
void_input_event ( InputEvent event ) virtual
void_input_text ( String text ) virtual
bool_iteration ( float delta ) virtual
voidfinish ( )
boolidle ( float delta )
voidinit ( )
voidinput_event ( InputEvent event )
voidinput_text ( String text )
booliteration ( float delta )

Constants

  • NOTIFICATION_WM_MOUSE_ENTER = 2 — Notification received from the OS when the mouse enters the game window.

Implemented on desktop and web platforms.

  • NOTIFICATION_WM_MOUSE_EXIT = 3 — Notification received from the OS when the mouse leaves the game window.

Implemented on desktop and web platforms.

  • NOTIFICATION_WM_FOCUS_IN = 4 — Notification received from the OS when the game window is focused.

Implemented on all platforms.

  • NOTIFICATION_WM_FOCUS_OUT = 5 — Notification received from the OS when the game window is unfocused.

Implemented on all platforms.

  • NOTIFICATION_WM_QUIT_REQUEST = 6 — 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.

  • NOTIFICATION_WM_GO_BACK_REQUEST = 7 — 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.

  • NOTIFICATION_WM_UNFOCUS_REQUEST = 8 — 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.

  • NOTIFICATION_OS_MEMORY_WARNING = 9 — Notification received from the OS when the application is exceeding its allocated memory.

Specific to the iOS platform.

  • NOTIFICATION_TRANSLATION_CHANGED = 90 — 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 = 91 — Notification received from the OS when a request for “About” information is sent.

Specific to the macOS platform.

  • NOTIFICATION_CRASH = 92 — 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 = 93 — 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.

Description

MainLoop is the abstract base class for a Godot project’s game loop. It in 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 application start, a MainLoop implementation has to be provided to the OS, or 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. godot -s my_loop.gd, which should then be a MainLoop implementation.

Here is an example script implementing a simple MainLoop:

  1. extends MainLoop
  2. var time_elapsed = 0
  3. var keys_typed = []
  4. var quit = false
  5. func _initialize():
  6. print("Initialized:")
  7. print(" Starting time: %s" % str(time_elapsed))
  8. func _idle(delta):
  9. time_elapsed += delta
  10. # Return true to end the main loop
  11. return quit
  12. func _input_event(event):
  13. # Record keys
  14. if event is InputEventKey and event.pressed and !event.echo:
  15. keys_typed.append(OS.get_scancode_string(event.scancode))
  16. # Quit on Escape press
  17. if event.scancode == KEY_ESCAPE:
  18. quit = true
  19. # Quit on any mouse click
  20. if event is InputEventMouseButton:
  21. quit = true
  22. func _finalize():
  23. print("Finalized:")
  24. print(" End time: %s" % str(time_elapsed))
  25. print(" Keys typed: %s" % var2str(keys_typed))

Method Descriptions

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.


  • void _finalize ( ) virtual

Called before the program exits.


Called each idle frame with the time since the last idle frame as argument (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.


  • void _initialize ( ) virtual

Called once during initialization.


Called whenever an InputEvent is received by the main loop.


  • void _input_text ( String text ) virtual

Deprecated callback, does not do anything. Use _input_event to parse text input. Will be removed in Godot 4.0.


Called each physics frame with the time since the last physics frame as argument (in seconds). 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 frame.


  • void finish ( )

Should not be called manually, override _finalize instead. Will be removed in Godot 4.0.


Should not be called manually, override _idle instead. Will be removed in Godot 4.0.


  • void init ( )

Should not be called manually, override _initialize instead. Will be removed in Godot 4.0.


Should not be called manually, override _input_event instead. Will be removed in Godot 4.0.


  • void input_text ( String text )

Should not be called manually, override _input_text instead. Will be removed in Godot 4.0.


Should not be called manually, override _iteration instead. Will be removed in Godot 4.0.