class_mainloop.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/make_rst.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. Abstract base class for the game's main loop.
  11. Description
  12. -----------
  13. ``MainLoop`` is the abstract base class for a Godot project's game loop. It is 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.
  14. Upon the application start, a ``MainLoop`` implementation must be provided to the OS; otherwise, 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.
  15. Here is an example script implementing a simple ``MainLoop``:
  16. ::
  17. extends MainLoop
  18. var time_elapsed = 0
  19. var keys_typed = []
  20. var quit = false
  21. func _initialize():
  22. print("Initialized:")
  23. print(" Starting time: %s" % str(time_elapsed))
  24. func _idle(delta):
  25. time_elapsed += delta
  26. # Return true to end the main loop.
  27. return quit
  28. func _input_event(event):
  29. # Record keys.
  30. if event is InputEventKey and event.pressed and !event.echo:
  31. keys_typed.append(OS.get_scancode_string(event.scancode))
  32. # Quit on Escape press.
  33. if event.scancode == KEY_ESCAPE:
  34. quit = true
  35. # Quit on any mouse click.
  36. if event is InputEventMouseButton:
  37. quit = true
  38. func _finalize():
  39. print("Finalized:")
  40. print(" End time: %s" % str(time_elapsed))
  41. print(" Keys typed: %s" % var2str(keys_typed))
  42. Methods
  43. -------
  44. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  45. | void | :ref:`_drop_files<class_MainLoop_method__drop_files>` **(** :ref:`PoolStringArray<class_PoolStringArray>` files, :ref:`int<class_int>` from_screen **)** |virtual| |
  46. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  47. | void | :ref:`_finalize<class_MainLoop_method__finalize>` **(** **)** |virtual| |
  48. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  49. | void | :ref:`_global_menu_action<class_MainLoop_method__global_menu_action>` **(** :ref:`Variant<class_Variant>` id, :ref:`Variant<class_Variant>` meta **)** |virtual| |
  50. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  51. | :ref:`bool<class_bool>` | :ref:`_idle<class_MainLoop_method__idle>` **(** :ref:`float<class_float>` delta **)** |virtual| |
  52. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  53. | void | :ref:`_initialize<class_MainLoop_method__initialize>` **(** **)** |virtual| |
  54. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  55. | void | :ref:`_input_event<class_MainLoop_method__input_event>` **(** :ref:`InputEvent<class_InputEvent>` event **)** |virtual| |
  56. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  57. | void | :ref:`_input_text<class_MainLoop_method__input_text>` **(** :ref:`String<class_String>` text **)** |virtual| |
  58. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  59. | :ref:`bool<class_bool>` | :ref:`_iteration<class_MainLoop_method__iteration>` **(** :ref:`float<class_float>` delta **)** |virtual| |
  60. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  61. | void | :ref:`finish<class_MainLoop_method_finish>` **(** **)** |
  62. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  63. | :ref:`bool<class_bool>` | :ref:`idle<class_MainLoop_method_idle>` **(** :ref:`float<class_float>` delta **)** |
  64. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  65. | void | :ref:`init<class_MainLoop_method_init>` **(** **)** |
  66. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  67. | void | :ref:`input_event<class_MainLoop_method_input_event>` **(** :ref:`InputEvent<class_InputEvent>` event **)** |
  68. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  69. | void | :ref:`input_text<class_MainLoop_method_input_text>` **(** :ref:`String<class_String>` text **)** |
  70. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  71. | :ref:`bool<class_bool>` | :ref:`iteration<class_MainLoop_method_iteration>` **(** :ref:`float<class_float>` delta **)** |
  72. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  73. Signals
  74. -------
  75. .. _class_MainLoop_signal_on_request_permissions_result:
  76. - **on_request_permissions_result** **(** :ref:`String<class_String>` permission, :ref:`bool<class_bool>` granted **)**
  77. Emitted when a user responds to a permission request.
  78. Constants
  79. ---------
  80. .. _class_MainLoop_constant_NOTIFICATION_WM_MOUSE_ENTER:
  81. .. _class_MainLoop_constant_NOTIFICATION_WM_MOUSE_EXIT:
  82. .. _class_MainLoop_constant_NOTIFICATION_WM_FOCUS_IN:
  83. .. _class_MainLoop_constant_NOTIFICATION_WM_FOCUS_OUT:
  84. .. _class_MainLoop_constant_NOTIFICATION_WM_QUIT_REQUEST:
  85. .. _class_MainLoop_constant_NOTIFICATION_WM_GO_BACK_REQUEST:
  86. .. _class_MainLoop_constant_NOTIFICATION_WM_UNFOCUS_REQUEST:
  87. .. _class_MainLoop_constant_NOTIFICATION_OS_MEMORY_WARNING:
  88. .. _class_MainLoop_constant_NOTIFICATION_TRANSLATION_CHANGED:
  89. .. _class_MainLoop_constant_NOTIFICATION_WM_ABOUT:
  90. .. _class_MainLoop_constant_NOTIFICATION_CRASH:
  91. .. _class_MainLoop_constant_NOTIFICATION_OS_IME_UPDATE:
  92. .. _class_MainLoop_constant_NOTIFICATION_APP_RESUMED:
  93. .. _class_MainLoop_constant_NOTIFICATION_APP_PAUSED:
  94. - **NOTIFICATION_WM_MOUSE_ENTER** = **1002** --- Notification received from the OS when the mouse enters the game window.
  95. Implemented on desktop and web platforms.
  96. - **NOTIFICATION_WM_MOUSE_EXIT** = **1003** --- Notification received from the OS when the mouse leaves the game window.
  97. Implemented on desktop and web platforms.
  98. - **NOTIFICATION_WM_FOCUS_IN** = **1004** --- Notification received from the OS when the game window is focused.
  99. Implemented on all platforms.
  100. - **NOTIFICATION_WM_FOCUS_OUT** = **1005** --- Notification received from the OS when the game window is unfocused.
  101. Implemented on all platforms.
  102. - **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).
  103. Implemented on desktop platforms.
  104. - **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).
  105. Specific to the Android platform.
  106. - **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).
  107. No supported platforms currently send this notification.
  108. - **NOTIFICATION_OS_MEMORY_WARNING** = **1009** --- Notification received from the OS when the application is exceeding its allocated memory.
  109. Specific to the iOS platform.
  110. - **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, like :ref:`Object.tr<class_Object_method_tr>`.
  111. - **NOTIFICATION_WM_ABOUT** = **1011** --- Notification received from the OS when a request for "About" information is sent.
  112. Specific to the macOS platform.
  113. - **NOTIFICATION_CRASH** = **1012** --- Notification received from Godot's crash handler when the engine is about to crash.
  114. Implemented on desktop platforms if the crash handler is enabled.
  115. - **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).
  116. Specific to the macOS platform.
  117. - **NOTIFICATION_APP_RESUMED** = **1014** --- Notification received from the OS when the app is resumed.
  118. Specific to the Android platform.
  119. - **NOTIFICATION_APP_PAUSED** = **1015** --- Notification received from the OS when the app is paused.
  120. Specific to the Android platform.
  121. Method Descriptions
  122. -------------------
  123. .. _class_MainLoop_method__drop_files:
  124. - void **_drop_files** **(** :ref:`PoolStringArray<class_PoolStringArray>` files, :ref:`int<class_int>` from_screen **)** |virtual|
  125. 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.
  126. ----
  127. .. _class_MainLoop_method__finalize:
  128. - void **_finalize** **(** **)** |virtual|
  129. Called before the program exits.
  130. ----
  131. .. _class_MainLoop_method__global_menu_action:
  132. - void **_global_menu_action** **(** :ref:`Variant<class_Variant>` id, :ref:`Variant<class_Variant>` meta **)** |virtual|
  133. Called when the user performs an action in the system global menu (e.g. the Mac OS menu bar).
  134. ----
  135. .. _class_MainLoop_method__idle:
  136. - :ref:`bool<class_bool>` **_idle** **(** :ref:`float<class_float>` delta **)** |virtual|
  137. 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>`.
  138. If implemented, the method must return a boolean value. ``true`` ends the main loop, while ``false`` lets it proceed to the next frame.
  139. ----
  140. .. _class_MainLoop_method__initialize:
  141. - void **_initialize** **(** **)** |virtual|
  142. Called once during initialization.
  143. ----
  144. .. _class_MainLoop_method__input_event:
  145. - void **_input_event** **(** :ref:`InputEvent<class_InputEvent>` event **)** |virtual|
  146. Called whenever an :ref:`InputEvent<class_InputEvent>` is received by the main loop.
  147. ----
  148. .. _class_MainLoop_method__input_text:
  149. - void **_input_text** **(** :ref:`String<class_String>` text **)** |virtual|
  150. 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.
  151. ----
  152. .. _class_MainLoop_method__iteration:
  153. - :ref:`bool<class_bool>` **_iteration** **(** :ref:`float<class_float>` delta **)** |virtual|
  154. Called each physics frame with the time since the last physics frame as argument (``delta``, in seconds). Equivalent to :ref:`Node._physics_process<class_Node_method__physics_process>`.
  155. If implemented, the method must return a boolean value. ``true`` ends the main loop, while ``false`` lets it proceed to the next frame.
  156. ----
  157. .. _class_MainLoop_method_finish:
  158. - void **finish** **(** **)**
  159. Should not be called manually, override :ref:`_finalize<class_MainLoop_method__finalize>` instead. Will be removed in Godot 4.0.
  160. ----
  161. .. _class_MainLoop_method_idle:
  162. - :ref:`bool<class_bool>` **idle** **(** :ref:`float<class_float>` delta **)**
  163. Should not be called manually, override :ref:`_idle<class_MainLoop_method__idle>` instead. Will be removed in Godot 4.0.
  164. ----
  165. .. _class_MainLoop_method_init:
  166. - void **init** **(** **)**
  167. Should not be called manually, override :ref:`_initialize<class_MainLoop_method__initialize>` instead. Will be removed in Godot 4.0.
  168. ----
  169. .. _class_MainLoop_method_input_event:
  170. - void **input_event** **(** :ref:`InputEvent<class_InputEvent>` event **)**
  171. Should not be called manually, override :ref:`_input_event<class_MainLoop_method__input_event>` instead. Will be removed in Godot 4.0.
  172. ----
  173. .. _class_MainLoop_method_input_text:
  174. - void **input_text** **(** :ref:`String<class_String>` text **)**
  175. Should not be called manually, override :ref:`_input_text<class_MainLoop_method__input_text>` instead. Will be removed in Godot 4.0.
  176. ----
  177. .. _class_MainLoop_method_iteration:
  178. - :ref:`bool<class_bool>` **iteration** **(** :ref:`float<class_float>` delta **)**
  179. Should not be called manually, override :ref:`_iteration<class_MainLoop_method__iteration>` instead. Will be removed in Godot 4.0.
  180. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  181. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  182. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`