class_mainloop.rst 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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/4.2/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/4.2/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 "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>` **(** :ref:`float<class_float>` delta **)** |virtual| |
  67. +-------------------------+-------------------------------------------------------------------------------------------------------------------------------+
  68. | :ref:`bool<class_bool>` | :ref:`_process<class_MainLoop_private_method__process>` **(** :ref:`float<class_float>` delta **)** |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** **(** :ref:`String<class_String>` permission, :ref:`bool<class_bool>` granted **)**
  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``
  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``
  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``
  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``
  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``
  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``
  111. Notification received from the OS when the application is resumed.
  112. Specific to the Android platform.
  113. .. _class_MainLoop_constant_NOTIFICATION_APPLICATION_PAUSED:
  114. .. rst-class:: classref-constant
  115. **NOTIFICATION_APPLICATION_PAUSED** = ``2015``
  116. Notification received from the OS when the application is paused.
  117. Specific to the Android platform.
  118. .. _class_MainLoop_constant_NOTIFICATION_APPLICATION_FOCUS_IN:
  119. .. rst-class:: classref-constant
  120. **NOTIFICATION_APPLICATION_FOCUS_IN** = ``2016``
  121. 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.
  122. Implemented on desktop platforms.
  123. .. _class_MainLoop_constant_NOTIFICATION_APPLICATION_FOCUS_OUT:
  124. .. rst-class:: classref-constant
  125. **NOTIFICATION_APPLICATION_FOCUS_OUT** = ``2017``
  126. 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.
  127. Implemented on desktop platforms.
  128. .. _class_MainLoop_constant_NOTIFICATION_TEXT_SERVER_CHANGED:
  129. .. rst-class:: classref-constant
  130. **NOTIFICATION_TEXT_SERVER_CHANGED** = ``2018``
  131. Notification received when text server is changed.
  132. .. rst-class:: classref-section-separator
  133. ----
  134. .. rst-class:: classref-descriptions-group
  135. Method Descriptions
  136. -------------------
  137. .. _class_MainLoop_private_method__finalize:
  138. .. rst-class:: classref-method
  139. void **_finalize** **(** **)** |virtual|
  140. Called before the program exits.
  141. .. rst-class:: classref-item-separator
  142. ----
  143. .. _class_MainLoop_private_method__initialize:
  144. .. rst-class:: classref-method
  145. void **_initialize** **(** **)** |virtual|
  146. Called once during initialization.
  147. .. rst-class:: classref-item-separator
  148. ----
  149. .. _class_MainLoop_private_method__physics_process:
  150. .. rst-class:: classref-method
  151. :ref:`bool<class_bool>` **_physics_process** **(** :ref:`float<class_float>` delta **)** |virtual|
  152. 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>`.
  153. If implemented, the method must return a boolean value. ``true`` ends the main loop, while ``false`` lets it proceed to the next frame.
  154. .. rst-class:: classref-item-separator
  155. ----
  156. .. _class_MainLoop_private_method__process:
  157. .. rst-class:: classref-method
  158. :ref:`bool<class_bool>` **_process** **(** :ref:`float<class_float>` delta **)** |virtual|
  159. 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>`.
  160. If implemented, the method must return a boolean value. ``true`` ends the main loop, while ``false`` lets it proceed to the next frame.
  161. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  162. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  163. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  164. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  165. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  166. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  167. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`