class_mainloop.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/makerst.py in Godot's source tree.
  3. .. DO NOT EDIT THIS FILE, but the MainLoop.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_MainLoop:
  6. MainLoop
  7. ========
  8. **Inherits:** :ref:`Object<class_Object>`
  9. **Inherited By:** :ref:`SceneTree<class_SceneTree>`
  10. **Category:** Core
  11. Brief Description
  12. -----------------
  13. Abstract base class for the game's main loop.
  14. Methods
  15. -------
  16. +-------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  17. | void | :ref:`_drop_files<class_MainLoop_method__drop_files>` **(** :ref:`PoolStringArray<class_PoolStringArray>` files, :ref:`int<class_int>` from_screen **)** virtual |
  18. +-------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  19. | void | :ref:`_finalize<class_MainLoop_method__finalize>` **(** **)** virtual |
  20. +-------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  21. | :ref:`bool<class_bool>` | :ref:`_idle<class_MainLoop_method__idle>` **(** :ref:`float<class_float>` delta **)** virtual |
  22. +-------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  23. | void | :ref:`_initialize<class_MainLoop_method__initialize>` **(** **)** virtual |
  24. +-------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  25. | void | :ref:`_input_event<class_MainLoop_method__input_event>` **(** :ref:`InputEvent<class_InputEvent>` event **)** virtual |
  26. +-------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  27. | void | :ref:`_input_text<class_MainLoop_method__input_text>` **(** :ref:`String<class_String>` text **)** virtual |
  28. +-------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  29. | :ref:`bool<class_bool>` | :ref:`_iteration<class_MainLoop_method__iteration>` **(** :ref:`float<class_float>` delta **)** virtual |
  30. +-------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  31. | void | :ref:`finish<class_MainLoop_method_finish>` **(** **)** |
  32. +-------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  33. | :ref:`bool<class_bool>` | :ref:`idle<class_MainLoop_method_idle>` **(** :ref:`float<class_float>` delta **)** |
  34. +-------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  35. | void | :ref:`init<class_MainLoop_method_init>` **(** **)** |
  36. +-------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  37. | void | :ref:`input_event<class_MainLoop_method_input_event>` **(** :ref:`InputEvent<class_InputEvent>` event **)** |
  38. +-------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  39. | void | :ref:`input_text<class_MainLoop_method_input_text>` **(** :ref:`String<class_String>` text **)** |
  40. +-------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  41. | :ref:`bool<class_bool>` | :ref:`iteration<class_MainLoop_method_iteration>` **(** :ref:`float<class_float>` delta **)** |
  42. +-------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  43. Constants
  44. ---------
  45. .. _class_MainLoop_constant_NOTIFICATION_WM_MOUSE_ENTER:
  46. .. _class_MainLoop_constant_NOTIFICATION_WM_MOUSE_EXIT:
  47. .. _class_MainLoop_constant_NOTIFICATION_WM_FOCUS_IN:
  48. .. _class_MainLoop_constant_NOTIFICATION_WM_FOCUS_OUT:
  49. .. _class_MainLoop_constant_NOTIFICATION_WM_QUIT_REQUEST:
  50. .. _class_MainLoop_constant_NOTIFICATION_WM_GO_BACK_REQUEST:
  51. .. _class_MainLoop_constant_NOTIFICATION_WM_UNFOCUS_REQUEST:
  52. .. _class_MainLoop_constant_NOTIFICATION_OS_MEMORY_WARNING:
  53. .. _class_MainLoop_constant_NOTIFICATION_TRANSLATION_CHANGED:
  54. .. _class_MainLoop_constant_NOTIFICATION_WM_ABOUT:
  55. .. _class_MainLoop_constant_NOTIFICATION_CRASH:
  56. .. _class_MainLoop_constant_NOTIFICATION_OS_IME_UPDATE:
  57. - **NOTIFICATION_WM_MOUSE_ENTER** = **2** --- Notification received from the OS when the mouse enters the game window.
  58. Implemented on desktop and web platforms.
  59. - **NOTIFICATION_WM_MOUSE_EXIT** = **3** --- Notification received from the OS when the mouse leaves the game window.
  60. Implemented on desktop and web platforms.
  61. - **NOTIFICATION_WM_FOCUS_IN** = **4** --- Notification received from the OS when the game window is focused.
  62. Implemented on all platforms.
  63. - **NOTIFICATION_WM_FOCUS_OUT** = **5** --- Notification received from the OS when the game window is unfocused.
  64. Implemented on all platforms.
  65. - **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).
  66. Implemented on desktop platforms.
  67. - **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).
  68. Specific to the Android platform.
  69. - **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).
  70. No supported platforms currently send this notification.
  71. - **NOTIFICATION_OS_MEMORY_WARNING** = **9** --- Notification received from the OS when the application is exceeding its allocated memory.
  72. Specific to the iOS platform.
  73. - **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 :ref:`Object.tr<class_Object_method_tr>`.
  74. - **NOTIFICATION_WM_ABOUT** = **91** --- Notification received from the OS when a request for "About" information is sent.
  75. Specific to the macOS platform.
  76. - **NOTIFICATION_CRASH** = **92** --- Notification received from Godot's crash handler when the engine is about to crash.
  77. Implemented on desktop platforms if the crash handler is enabled.
  78. - **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).
  79. Specific to the macOS platform.
  80. Description
  81. -----------
  82. ``MainLoop`` is the abstract base class for a Godot project's game loop. It in inherited by :ref:`SceneTree<class_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.
  83. Upon application start, a ``MainLoop`` implementation has to be provided to the OS, or the application will exit. This happens automatically (and a :ref:`SceneTree<class_SceneTree>` is created) unless a main :ref:`Script<class_Script>` is provided from the command line (with e.g. ``godot -s my_loop.gd``, which should then be a ``MainLoop`` implementation.
  84. Here is an example script implementing a simple ``MainLoop``:
  85. ::
  86. extends MainLoop
  87. var time_elapsed = 0
  88. var keys_typed = []
  89. var quit = false
  90. func _initialize():
  91. print("Initialized:")
  92. print(" Starting time: %s" % str(time_elapsed))
  93. func _idle(delta):
  94. time_elapsed += delta
  95. # Return true to end the main loop
  96. return quit
  97. func _input_event(event):
  98. # Record keys
  99. if event is InputEventKey and event.pressed and !event.echo:
  100. keys_typed.append(OS.get_scancode_string(event.scancode))
  101. # Quit on Escape press
  102. if event.scancode == KEY_ESCAPE:
  103. quit = true
  104. # Quit on any mouse click
  105. if event is InputEventMouseButton:
  106. quit = true
  107. func _finalize():
  108. print("Finalized:")
  109. print(" End time: %s" % str(time_elapsed))
  110. print(" Keys typed: %s" % var2str(keys_typed))
  111. Method Descriptions
  112. -------------------
  113. .. _class_MainLoop_method__drop_files:
  114. - void **_drop_files** **(** :ref:`PoolStringArray<class_PoolStringArray>` files, :ref:`int<class_int>` from_screen **)** virtual
  115. 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.
  116. ----
  117. .. _class_MainLoop_method__finalize:
  118. - void **_finalize** **(** **)** virtual
  119. Called before the program exits.
  120. ----
  121. .. _class_MainLoop_method__idle:
  122. - :ref:`bool<class_bool>` **_idle** **(** :ref:`float<class_float>` delta **)** virtual
  123. Called each idle frame with the time since the last idle frame as argument (in seconds). Equivalent to :ref:`Node._process<class_Node_method__process>`.
  124. If implemented, the method must return a boolean value. ``true`` ends the main loop, while ``false`` lets it proceed to the next frame.
  125. ----
  126. .. _class_MainLoop_method__initialize:
  127. - void **_initialize** **(** **)** virtual
  128. Called once during initialization.
  129. ----
  130. .. _class_MainLoop_method__input_event:
  131. - void **_input_event** **(** :ref:`InputEvent<class_InputEvent>` event **)** virtual
  132. Called whenever an :ref:`InputEvent<class_InputEvent>` is received by the main loop.
  133. ----
  134. .. _class_MainLoop_method__input_text:
  135. - void **_input_text** **(** :ref:`String<class_String>` text **)** virtual
  136. Deprecated callback, does not do anything. Use :ref:`_input_event<class_MainLoop_method__input_event>` to parse text input. Will be removed in Godot 4.0.
  137. ----
  138. .. _class_MainLoop_method__iteration:
  139. - :ref:`bool<class_bool>` **_iteration** **(** :ref:`float<class_float>` delta **)** virtual
  140. Called each physics frame with the time since the last physics frame as argument (in seconds). Equivalent to :ref:`Node._physics_process<class_Node_method__physics_process>`.
  141. If implemented, the method must return a boolean value. ``true`` ends the main loop, while ``false`` lets it proceed to the next frame.
  142. ----
  143. .. _class_MainLoop_method_finish:
  144. - void **finish** **(** **)**
  145. Should not be called manually, override :ref:`_finalize<class_MainLoop_method__finalize>` instead. Will be removed in Godot 4.0.
  146. ----
  147. .. _class_MainLoop_method_idle:
  148. - :ref:`bool<class_bool>` **idle** **(** :ref:`float<class_float>` delta **)**
  149. Should not be called manually, override :ref:`_idle<class_MainLoop_method__idle>` instead. Will be removed in Godot 4.0.
  150. ----
  151. .. _class_MainLoop_method_init:
  152. - void **init** **(** **)**
  153. Should not be called manually, override :ref:`_initialize<class_MainLoop_method__initialize>` instead. Will be removed in Godot 4.0.
  154. ----
  155. .. _class_MainLoop_method_input_event:
  156. - void **input_event** **(** :ref:`InputEvent<class_InputEvent>` event **)**
  157. Should not be called manually, override :ref:`_input_event<class_MainLoop_method__input_event>` instead. Will be removed in Godot 4.0.
  158. ----
  159. .. _class_MainLoop_method_input_text:
  160. - void **input_text** **(** :ref:`String<class_String>` text **)**
  161. Should not be called manually, override :ref:`_input_text<class_MainLoop_method__input_text>` instead. Will be removed in Godot 4.0.
  162. ----
  163. .. _class_MainLoop_method_iteration:
  164. - :ref:`bool<class_bool>` **iteration** **(** :ref:`float<class_float>` delta **)**
  165. Should not be called manually, override :ref:`_iteration<class_MainLoop_method__iteration>` instead. Will be removed in Godot 4.0.