class_mainloop.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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/master/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/master/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 **MainLoop** :ref:`Script<class_Script>` is provided from the command line (with e.g. ``godot -s my_loop.gd``) or the :ref:`ProjectSettings.application/run/main_loop_type<class_ProjectSettings_property_application/run/main_loop_type>` project setting is overwritten.
  17. Here is an example script implementing a simple **MainLoop**:
  18. .. tabs::
  19. .. code-tab:: gdscript
  20. class_name CustomMainLoop
  21. extends MainLoop
  22. var time_elapsed = 0
  23. func _initialize():
  24. print("Initialized:")
  25. print(" Starting time: %s" % str(time_elapsed))
  26. func _process(delta):
  27. time_elapsed += delta
  28. # Return true to end the main loop.
  29. return Input.get_mouse_button_mask() != 0 || Input.is_key_pressed(KEY_ESCAPE)
  30. func _finalize():
  31. print("Finalized:")
  32. print(" End time: %s" % str(time_elapsed))
  33. .. code-tab:: csharp
  34. using Godot;
  35. [GlobalClass]
  36. public partial class CustomMainLoop : MainLoop
  37. {
  38. private double _timeElapsed = 0;
  39. public override void _Initialize()
  40. {
  41. GD.Print("Initialized:");
  42. GD.Print($" Starting Time: {_timeElapsed}");
  43. }
  44. public override bool _Process(double delta)
  45. {
  46. _timeElapsed += delta;
  47. // Return true to end the main loop.
  48. return Input.GetMouseButtonMask() != 0 || Input.IsKeyPressed(Key.Escape);
  49. }
  50. private void _Finalize()
  51. {
  52. GD.Print("Finalized:");
  53. GD.Print($" End Time: {_timeElapsed}");
  54. }
  55. }
  56. .. rst-class:: classref-reftable-group
  57. Methods
  58. -------
  59. .. table::
  60. :widths: auto
  61. +-------------------------+----------------------------------------------------------------------------------------------------------------------------+
  62. | |void| | :ref:`_finalize<class_MainLoop_private_method__finalize>`\ (\ ) |virtual| |
  63. +-------------------------+----------------------------------------------------------------------------------------------------------------------------+
  64. | |void| | :ref:`_initialize<class_MainLoop_private_method__initialize>`\ (\ ) |virtual| |
  65. +-------------------------+----------------------------------------------------------------------------------------------------------------------------+
  66. | :ref:`bool<class_bool>` | :ref:`_physics_process<class_MainLoop_private_method__physics_process>`\ (\ delta\: :ref:`float<class_float>`\ ) |virtual| |
  67. +-------------------------+----------------------------------------------------------------------------------------------------------------------------+
  68. | :ref:`bool<class_bool>` | :ref:`_process<class_MainLoop_private_method__process>`\ (\ delta\: :ref:`float<class_float>`\ ) |virtual| |
  69. +-------------------------+----------------------------------------------------------------------------------------------------------------------------+
  70. .. rst-class:: classref-section-separator
  71. ----
  72. .. rst-class:: classref-descriptions-group
  73. Signals
  74. -------
  75. .. _class_MainLoop_signal_on_request_permissions_result:
  76. .. rst-class:: classref-signal
  77. **on_request_permissions_result**\ (\ permission\: :ref:`String<class_String>`, granted\: :ref:`bool<class_bool>`\ ) :ref:`🔗<class_MainLoop_signal_on_request_permissions_result>`
  78. Emitted when a user responds to a permission request.
  79. .. rst-class:: classref-section-separator
  80. ----
  81. .. rst-class:: classref-descriptions-group
  82. Constants
  83. ---------
  84. .. _class_MainLoop_constant_NOTIFICATION_OS_MEMORY_WARNING:
  85. .. rst-class:: classref-constant
  86. **NOTIFICATION_OS_MEMORY_WARNING** = ``2009`` :ref:`🔗<class_MainLoop_constant_NOTIFICATION_OS_MEMORY_WARNING>`
  87. Notification received from the OS when the application is exceeding its allocated memory.
  88. Specific to the iOS platform.
  89. .. _class_MainLoop_constant_NOTIFICATION_TRANSLATION_CHANGED:
  90. .. rst-class:: classref-constant
  91. **NOTIFICATION_TRANSLATION_CHANGED** = ``2010`` :ref:`🔗<class_MainLoop_constant_NOTIFICATION_TRANSLATION_CHANGED>`
  92. 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>`.
  93. .. _class_MainLoop_constant_NOTIFICATION_WM_ABOUT:
  94. .. rst-class:: classref-constant
  95. **NOTIFICATION_WM_ABOUT** = ``2011`` :ref:`🔗<class_MainLoop_constant_NOTIFICATION_WM_ABOUT>`
  96. Notification received from the OS when a request for "About" information is sent.
  97. Specific to the macOS platform.
  98. .. _class_MainLoop_constant_NOTIFICATION_CRASH:
  99. .. rst-class:: classref-constant
  100. **NOTIFICATION_CRASH** = ``2012`` :ref:`🔗<class_MainLoop_constant_NOTIFICATION_CRASH>`
  101. Notification received from Godot's crash handler when the engine is about to crash.
  102. Implemented on desktop platforms if the crash handler is enabled.
  103. .. _class_MainLoop_constant_NOTIFICATION_OS_IME_UPDATE:
  104. .. rst-class:: classref-constant
  105. **NOTIFICATION_OS_IME_UPDATE** = ``2013`` :ref:`🔗<class_MainLoop_constant_NOTIFICATION_OS_IME_UPDATE>`
  106. Notification received from the OS when an update of the Input Method Engine occurs (e.g. change of IME cursor position or composition string).
  107. Specific to the macOS platform.
  108. .. _class_MainLoop_constant_NOTIFICATION_APPLICATION_RESUMED:
  109. .. rst-class:: classref-constant
  110. **NOTIFICATION_APPLICATION_RESUMED** = ``2014`` :ref:`🔗<class_MainLoop_constant_NOTIFICATION_APPLICATION_RESUMED>`
  111. Notification received from the OS when the application is resumed.
  112. Specific to the Android and iOS platforms.
  113. .. _class_MainLoop_constant_NOTIFICATION_APPLICATION_PAUSED:
  114. .. rst-class:: classref-constant
  115. **NOTIFICATION_APPLICATION_PAUSED** = ``2015`` :ref:`🔗<class_MainLoop_constant_NOTIFICATION_APPLICATION_PAUSED>`
  116. Notification received from the OS when the application is paused.
  117. Specific to the Android and iOS platforms.
  118. \ **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.
  119. .. _class_MainLoop_constant_NOTIFICATION_APPLICATION_FOCUS_IN:
  120. .. rst-class:: classref-constant
  121. **NOTIFICATION_APPLICATION_FOCUS_IN** = ``2016`` :ref:`🔗<class_MainLoop_constant_NOTIFICATION_APPLICATION_FOCUS_IN>`
  122. Notification received from the OS when the application is focused, i.e. when changing the focus from the OS desktop or a thirdparty application to any open window of the Godot instance.
  123. Implemented on desktop and mobile platforms.
  124. .. _class_MainLoop_constant_NOTIFICATION_APPLICATION_FOCUS_OUT:
  125. .. rst-class:: classref-constant
  126. **NOTIFICATION_APPLICATION_FOCUS_OUT** = ``2017`` :ref:`🔗<class_MainLoop_constant_NOTIFICATION_APPLICATION_FOCUS_OUT>`
  127. Notification received from the OS when the application is defocused, i.e. when changing the focus from any open window of the Godot instance to the OS desktop or a thirdparty application.
  128. Implemented on desktop and mobile platforms.
  129. .. _class_MainLoop_constant_NOTIFICATION_TEXT_SERVER_CHANGED:
  130. .. rst-class:: classref-constant
  131. **NOTIFICATION_TEXT_SERVER_CHANGED** = ``2018`` :ref:`🔗<class_MainLoop_constant_NOTIFICATION_TEXT_SERVER_CHANGED>`
  132. Notification received when text server is changed.
  133. .. rst-class:: classref-section-separator
  134. ----
  135. .. rst-class:: classref-descriptions-group
  136. Method Descriptions
  137. -------------------
  138. .. _class_MainLoop_private_method__finalize:
  139. .. rst-class:: classref-method
  140. |void| **_finalize**\ (\ ) |virtual| :ref:`🔗<class_MainLoop_private_method__finalize>`
  141. Called before the program exits.
  142. .. rst-class:: classref-item-separator
  143. ----
  144. .. _class_MainLoop_private_method__initialize:
  145. .. rst-class:: classref-method
  146. |void| **_initialize**\ (\ ) |virtual| :ref:`🔗<class_MainLoop_private_method__initialize>`
  147. Called once during initialization.
  148. .. rst-class:: classref-item-separator
  149. ----
  150. .. _class_MainLoop_private_method__physics_process:
  151. .. rst-class:: classref-method
  152. :ref:`bool<class_bool>` **_physics_process**\ (\ delta\: :ref:`float<class_float>`\ ) |virtual| :ref:`🔗<class_MainLoop_private_method__physics_process>`
  153. 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_private_method__physics_process>`.
  154. If implemented, the method must return a boolean value. ``true`` ends the main loop, while ``false`` lets it proceed to the next frame.
  155. .. rst-class:: classref-item-separator
  156. ----
  157. .. _class_MainLoop_private_method__process:
  158. .. rst-class:: classref-method
  159. :ref:`bool<class_bool>` **_process**\ (\ delta\: :ref:`float<class_float>`\ ) |virtual| :ref:`🔗<class_MainLoop_private_method__process>`
  160. Called each process (idle) frame with the time since the last process frame as argument (in seconds). Equivalent to :ref:`Node._process<class_Node_private_method__process>`.
  161. If implemented, the method must return a boolean value. ``true`` ends the main loop, while ``false`` lets it proceed to the next frame.
  162. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  163. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  164. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  165. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  166. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  167. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  168. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
  169. .. |void| replace:: :abbr:`void (No return value.)`