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 bySceneTree, which is the default game loop implementation used in Godot projects, though it is also possible to write and use one's ownMainLoop subclass instead of the scene tree.
Upon the application start, aMainLoop implementation must be provided to the OS; otherwise, the application will exit. This happens automatically (and aSceneTree is created) unless a mainScript is provided from the command line (with e.g.godot-smy_loop.gd, which should then be aMainLoop implementation.
Here is an example script implementing a simpleMainLoop:
extendsMainLoopvartime_elapsed=0varkeys_typed=[]varquit=falsefunc_initialize():print("Initialized:")print(" Starting time:%s"%str(time_elapsed))func_idle(delta):time_elapsed+=delta# Return true to end the main loop.returnquitfunc_input_event(event):# Record keys.ifeventisInputEventKeyandevent.pressedand!event.echo:keys_typed.append(OS.get_scancode_string(event.scancode))# Quit on Escape press.ifevent.scancode==KEY_ESCAPE:quit=true# Quit on any mouse click.ifeventisInputEventMouseButton:quit=truefunc_finalize():print("Finalized:")print(" End time:%s"%str(time_elapsed))print(" Keys typed:%s"%var2str(keys_typed))
Methods¶
void | _drop_files(PoolStringArray files,int from_screen)virtual |
void | |
void | _global_menu_action(Variant id,Variant meta)virtual |
void | |
void | _input_event(InputEvent event)virtual |
void | _input_text(String text)virtual |
_iteration(float delta)virtual | |
void | finish() |
void | init() |
void | input_event(InputEvent event) |
void | input_text(String text) |
Signals¶
Emitted when a user responds to a permission request.
Constants¶
NOTIFICATION_WM_MOUSE_ENTER =1002 --- Notification received from the OS when the mouse enters the game window.
Implemented on desktop and web platforms.
NOTIFICATION_WM_MOUSE_EXIT =1003 --- Notification received from the OS when the mouse leaves the game window.
Implemented on desktop and web platforms.
NOTIFICATION_WM_FOCUS_IN =1004 --- Notification received from the OS when the game window is focused.
Implemented on all platforms.
NOTIFICATION_WM_FOCUS_OUT =1005 --- Notification received from the OS when the game window is unfocused.
Implemented on all platforms.
NOTIFICATION_WM_QUIT_REQUEST =1006 --- 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 =1007 --- 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 =1008 --- 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 =1009 --- Notification received from the OS when the application is exceeding its allocated memory.
Specific to the iOS platform.
NOTIFICATION_TRANSLATION_CHANGED =1010 --- 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, likeObject.tr.
NOTIFICATION_WM_ABOUT =1011 --- Notification received from the OS when a request for "About" information is sent.
Specific to the macOS platform.
NOTIFICATION_CRASH =1012 --- 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 =1013 --- 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_APP_RESUMED =1014 --- Notification received from the OS when the app is resumed.
Specific to the Android platform.
NOTIFICATION_APP_PAUSED =1015 --- Notification received from the OS when the app is paused.
Specific to the Android platform.
Method Descriptions¶
void_drop_files(PoolStringArray files,int from_screen)virtual
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 when the user performs an action in the system global menu (e.g. the Mac OS menu bar).
Called each idle frame with the time since the last idle frame as argument (in seconds). Equivalent toNode._process.
If implemented, the method must return a boolean value.true ends the main loop, whilefalse lets it proceed to the next frame.
void_initialize()virtual
Called once during initialization.
void_input_event(InputEvent event)virtual
Called whenever anInputEvent is received by the main loop.
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 (delta, in seconds). Equivalent toNode._physics_process.
If implemented, the method must return a boolean value.true ends the main loop, whilefalse lets it proceed to the next frame.
voidfinish()
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.
voidinit()
Should not be called manually, override_initialize instead. Will be removed in Godot 4.0.
voidinput_event(InputEvent event)
Should not be called manually, override_input_event instead. Will be removed in Godot 4.0.
voidinput_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.