class_mainloop.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/3.6/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/3.6/doc/classes/MainLoop.xml.
  6. .. _class_MainLoop:
  7. MainLoop
  8. ========
  9. **Inherits:** :ref:`Object<class_Object>`
  10. **Inherited By:** :ref:`SceneTree<class_SceneTree>`
  11. Abstract base class for the game's main loop.
  12. .. rst-class:: classref-introduction-group
  13. Description
  14. -----------
  15. **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.
  16. 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.
  17. Here is an example script implementing a simple **MainLoop**:
  18. ::
  19. extends MainLoop
  20. var time_elapsed = 0
  21. var keys_typed = []
  22. var quit = false
  23. func _initialize():
  24. print("Initialized:")
  25. print(" Starting time: %s" % str(time_elapsed))
  26. func _idle(delta):
  27. time_elapsed += delta
  28. # Return true to end the main loop.
  29. return quit
  30. func _input_event(event):
  31. # Record keys.
  32. if event is InputEventKey and event.pressed and !event.echo:
  33. keys_typed.append(OS.get_scancode_string(event.scancode))
  34. # Quit on Escape press.
  35. if event.scancode == KEY_ESCAPE:
  36. quit = true
  37. # Quit on any mouse click.
  38. if event is InputEventMouseButton:
  39. quit = true
  40. func _finalize():
  41. print("Finalized:")
  42. print(" End time: %s" % str(time_elapsed))
  43. print(" Keys typed: %s" % var2str(keys_typed))
  44. .. rst-class:: classref-reftable-group
  45. Methods
  46. -------
  47. .. table::
  48. :widths: auto
  49. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  50. | void | :ref:`_drop_files<class_MainLoop_method__drop_files>` **(** :ref:`PoolStringArray<class_PoolStringArray>` files, :ref:`int<class_int>` from_screen **)** |virtual| |
  51. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  52. | void | :ref:`_finalize<class_MainLoop_method__finalize>` **(** **)** |virtual| |
  53. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  54. | void | :ref:`_global_menu_action<class_MainLoop_method__global_menu_action>` **(** :ref:`Variant<class_Variant>` id, :ref:`Variant<class_Variant>` meta **)** |virtual| |
  55. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  56. | :ref:`bool<class_bool>` | :ref:`_idle<class_MainLoop_method__idle>` **(** :ref:`float<class_float>` delta **)** |virtual| |
  57. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  58. | void | :ref:`_initialize<class_MainLoop_method__initialize>` **(** **)** |virtual| |
  59. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  60. | void | :ref:`_input_event<class_MainLoop_method__input_event>` **(** :ref:`InputEvent<class_InputEvent>` event **)** |virtual| |
  61. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  62. | void | :ref:`_input_text<class_MainLoop_method__input_text>` **(** :ref:`String<class_String>` text **)** |virtual| |
  63. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  64. | :ref:`bool<class_bool>` | :ref:`_iteration<class_MainLoop_method__iteration>` **(** :ref:`float<class_float>` delta **)** |virtual| |
  65. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  66. | void | :ref:`finish<class_MainLoop_method_finish>` **(** **)** |
  67. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  68. | :ref:`bool<class_bool>` | :ref:`idle<class_MainLoop_method_idle>` **(** :ref:`float<class_float>` delta **)** |
  69. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  70. | void | :ref:`init<class_MainLoop_method_init>` **(** **)** |
  71. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  72. | void | :ref:`input_event<class_MainLoop_method_input_event>` **(** :ref:`InputEvent<class_InputEvent>` event **)** |
  73. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  74. | void | :ref:`input_text<class_MainLoop_method_input_text>` **(** :ref:`String<class_String>` text **)** |
  75. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  76. | :ref:`bool<class_bool>` | :ref:`iteration<class_MainLoop_method_iteration>` **(** :ref:`float<class_float>` delta **)** |
  77. +-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  78. .. rst-class:: classref-section-separator
  79. ----
  80. .. rst-class:: classref-descriptions-group
  81. Signals
  82. -------
  83. .. _class_MainLoop_signal_on_request_permissions_result:
  84. .. rst-class:: classref-signal
  85. **on_request_permissions_result** **(** :ref:`String<class_String>` permission, :ref:`bool<class_bool>` granted **)**
  86. Emitted when a user responds to a permission request.
  87. .. rst-class:: classref-section-separator
  88. ----
  89. .. rst-class:: classref-descriptions-group
  90. Constants
  91. ---------
  92. .. _class_MainLoop_constant_NOTIFICATION_WM_MOUSE_ENTER:
  93. .. rst-class:: classref-constant
  94. **NOTIFICATION_WM_MOUSE_ENTER** = ``1002``
  95. Notification received from the OS when the mouse enters the game window.
  96. Implemented on desktop and web platforms.
  97. .. _class_MainLoop_constant_NOTIFICATION_WM_MOUSE_EXIT:
  98. .. rst-class:: classref-constant
  99. **NOTIFICATION_WM_MOUSE_EXIT** = ``1003``
  100. Notification received from the OS when the mouse leaves the game window.
  101. Implemented on desktop and web platforms.
  102. .. _class_MainLoop_constant_NOTIFICATION_WM_FOCUS_IN:
  103. .. rst-class:: classref-constant
  104. **NOTIFICATION_WM_FOCUS_IN** = ``1004``
  105. Notification received from the OS when the game window is focused.
  106. Implemented on all platforms.
  107. .. _class_MainLoop_constant_NOTIFICATION_WM_FOCUS_OUT:
  108. .. rst-class:: classref-constant
  109. **NOTIFICATION_WM_FOCUS_OUT** = ``1005``
  110. Notification received from the OS when the game window is unfocused.
  111. Implemented on all platforms.
  112. .. _class_MainLoop_constant_NOTIFICATION_WM_QUIT_REQUEST:
  113. .. rst-class:: classref-constant
  114. **NOTIFICATION_WM_QUIT_REQUEST** = ``1006``
  115. Notification received from the OS when a quit request is sent (e.g. closing the window with a "Close" button or Alt+F4).
  116. Implemented on desktop platforms.
  117. .. _class_MainLoop_constant_NOTIFICATION_WM_GO_BACK_REQUEST:
  118. .. rst-class:: classref-constant
  119. **NOTIFICATION_WM_GO_BACK_REQUEST** = ``1007``
  120. Notification received from the OS when a go back request is sent (e.g. pressing the "Back" button on Android).
  121. Specific to the Android platform.
  122. .. _class_MainLoop_constant_NOTIFICATION_WM_UNFOCUS_REQUEST:
  123. .. rst-class:: classref-constant
  124. **NOTIFICATION_WM_UNFOCUS_REQUEST** = ``1008``
  125. Notification received from the OS when an unfocus request is sent (e.g. another OS window wants to take the focus).
  126. No supported platforms currently send this notification.
  127. .. _class_MainLoop_constant_NOTIFICATION_OS_MEMORY_WARNING:
  128. .. rst-class:: classref-constant
  129. **NOTIFICATION_OS_MEMORY_WARNING** = ``1009``
  130. Notification received from the OS when the application is exceeding its allocated memory.
  131. Specific to the iOS platform.
  132. .. _class_MainLoop_constant_NOTIFICATION_TRANSLATION_CHANGED:
  133. .. rst-class:: classref-constant
  134. **NOTIFICATION_TRANSLATION_CHANGED** = ``1010``
  135. 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>`.
  136. .. _class_MainLoop_constant_NOTIFICATION_WM_ABOUT:
  137. .. rst-class:: classref-constant
  138. **NOTIFICATION_WM_ABOUT** = ``1011``
  139. Notification received from the OS when a request for "About" information is sent.
  140. Specific to the macOS platform.
  141. .. _class_MainLoop_constant_NOTIFICATION_CRASH:
  142. .. rst-class:: classref-constant
  143. **NOTIFICATION_CRASH** = ``1012``
  144. Notification received from Godot's crash handler when the engine is about to crash.
  145. Implemented on desktop platforms if the crash handler is enabled.
  146. .. _class_MainLoop_constant_NOTIFICATION_OS_IME_UPDATE:
  147. .. rst-class:: classref-constant
  148. **NOTIFICATION_OS_IME_UPDATE** = ``1013``
  149. Notification received from the OS when an update of the Input Method Engine occurs (e.g. change of IME cursor position or composition string).
  150. Specific to the macOS platform.
  151. .. _class_MainLoop_constant_NOTIFICATION_APP_RESUMED:
  152. .. rst-class:: classref-constant
  153. **NOTIFICATION_APP_RESUMED** = ``1014``
  154. Notification received from the OS when the app is resumed.
  155. Specific to mobile platforms.
  156. .. _class_MainLoop_constant_NOTIFICATION_APP_PAUSED:
  157. .. rst-class:: classref-constant
  158. **NOTIFICATION_APP_PAUSED** = ``1015``
  159. Notification received from the OS when the app is paused.
  160. Specific to mobile platforms.
  161. \ **Note:** On iOS, you only have approximately 5 seconds to finish a task started by this signal. If you go over this allotment, iOS will kill the app instead of pausing it.
  162. .. rst-class:: classref-section-separator
  163. ----
  164. .. rst-class:: classref-descriptions-group
  165. Method Descriptions
  166. -------------------
  167. .. _class_MainLoop_method__drop_files:
  168. .. rst-class:: classref-method
  169. void **_drop_files** **(** :ref:`PoolStringArray<class_PoolStringArray>` files, :ref:`int<class_int>` from_screen **)** |virtual|
  170. 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.
  171. .. rst-class:: classref-item-separator
  172. ----
  173. .. _class_MainLoop_method__finalize:
  174. .. rst-class:: classref-method
  175. void **_finalize** **(** **)** |virtual|
  176. Called before the program exits.
  177. .. rst-class:: classref-item-separator
  178. ----
  179. .. _class_MainLoop_method__global_menu_action:
  180. .. rst-class:: classref-method
  181. void **_global_menu_action** **(** :ref:`Variant<class_Variant>` id, :ref:`Variant<class_Variant>` meta **)** |virtual|
  182. Called when the user performs an action in the system global menu (e.g. the Mac OS menu bar).
  183. .. rst-class:: classref-item-separator
  184. ----
  185. .. _class_MainLoop_method__idle:
  186. .. rst-class:: classref-method
  187. :ref:`bool<class_bool>` **_idle** **(** :ref:`float<class_float>` delta **)** |virtual|
  188. 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>`.
  189. If implemented, the method must return a boolean value. ``true`` ends the main loop, while ``false`` lets it proceed to the next frame.
  190. .. rst-class:: classref-item-separator
  191. ----
  192. .. _class_MainLoop_method__initialize:
  193. .. rst-class:: classref-method
  194. void **_initialize** **(** **)** |virtual|
  195. Called once during initialization.
  196. .. rst-class:: classref-item-separator
  197. ----
  198. .. _class_MainLoop_method__input_event:
  199. .. rst-class:: classref-method
  200. void **_input_event** **(** :ref:`InputEvent<class_InputEvent>` event **)** |virtual|
  201. Called whenever an :ref:`InputEvent<class_InputEvent>` is received by the main loop.
  202. .. rst-class:: classref-item-separator
  203. ----
  204. .. _class_MainLoop_method__input_text:
  205. .. rst-class:: classref-method
  206. void **_input_text** **(** :ref:`String<class_String>` text **)** |virtual|
  207. 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.
  208. .. rst-class:: classref-item-separator
  209. ----
  210. .. _class_MainLoop_method__iteration:
  211. .. rst-class:: classref-method
  212. :ref:`bool<class_bool>` **_iteration** **(** :ref:`float<class_float>` delta **)** |virtual|
  213. 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>`.
  214. If implemented, the method must return a boolean value. ``true`` ends the main loop, while ``false`` lets it proceed to the next frame.
  215. .. rst-class:: classref-item-separator
  216. ----
  217. .. _class_MainLoop_method_finish:
  218. .. rst-class:: classref-method
  219. void **finish** **(** **)**
  220. Should not be called manually, override :ref:`_finalize<class_MainLoop_method__finalize>` instead. Will be removed in Godot 4.0.
  221. .. rst-class:: classref-item-separator
  222. ----
  223. .. _class_MainLoop_method_idle:
  224. .. rst-class:: classref-method
  225. :ref:`bool<class_bool>` **idle** **(** :ref:`float<class_float>` delta **)**
  226. Should not be called manually, override :ref:`_idle<class_MainLoop_method__idle>` instead. Will be removed in Godot 4.0.
  227. .. rst-class:: classref-item-separator
  228. ----
  229. .. _class_MainLoop_method_init:
  230. .. rst-class:: classref-method
  231. void **init** **(** **)**
  232. Should not be called manually, override :ref:`_initialize<class_MainLoop_method__initialize>` instead. Will be removed in Godot 4.0.
  233. .. rst-class:: classref-item-separator
  234. ----
  235. .. _class_MainLoop_method_input_event:
  236. .. rst-class:: classref-method
  237. void **input_event** **(** :ref:`InputEvent<class_InputEvent>` event **)**
  238. Should not be called manually, override :ref:`_input_event<class_MainLoop_method__input_event>` instead. Will be removed in Godot 4.0.
  239. .. rst-class:: classref-item-separator
  240. ----
  241. .. _class_MainLoop_method_input_text:
  242. .. rst-class:: classref-method
  243. void **input_text** **(** :ref:`String<class_String>` text **)**
  244. Should not be called manually, override :ref:`_input_text<class_MainLoop_method__input_text>` instead. Will be removed in Godot 4.0.
  245. .. rst-class:: classref-item-separator
  246. ----
  247. .. _class_MainLoop_method_iteration:
  248. .. rst-class:: classref-method
  249. :ref:`bool<class_bool>` **iteration** **(** :ref:`float<class_float>` delta **)**
  250. Should not be called manually, override :ref:`_iteration<class_MainLoop_method__iteration>` instead. Will be removed in Godot 4.0.
  251. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  252. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  253. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  254. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`