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/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 "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. public partial class CustomMainLoop : MainLoop
  36. {
  37. private double _timeElapsed = 0;
  38. public override void _Initialize()
  39. {
  40. GD.Print("Initialized:");
  41. GD.Print($" Starting Time: {_timeElapsed}");
  42. }
  43. public override bool _Process(double delta)
  44. {
  45. _timeElapsed += delta;
  46. // Return true to end the main loop.
  47. return Input.GetMouseButtonMask() != 0 || Input.IsKeyPressed(Key.Escape);
  48. }
  49. private void _Finalize()
  50. {
  51. GD.Print("Finalized:");
  52. GD.Print($" End Time: {_timeElapsed}");
  53. }
  54. }
  55. .. rst-class:: classref-reftable-group
  56. Methods
  57. -------
  58. .. table::
  59. :widths: auto
  60. +-------------------------+-------------------------------------------------------------------------------------------------------------------------------+
  61. | void | :ref:`_finalize<class_MainLoop_private_method__finalize>` **(** **)** |virtual| |
  62. +-------------------------+-------------------------------------------------------------------------------------------------------------------------------+
  63. | void | :ref:`_initialize<class_MainLoop_private_method__initialize>` **(** **)** |virtual| |
  64. +-------------------------+-------------------------------------------------------------------------------------------------------------------------------+
  65. | :ref:`bool<class_bool>` | :ref:`_physics_process<class_MainLoop_private_method__physics_process>` **(** :ref:`float<class_float>` delta **)** |virtual| |
  66. +-------------------------+-------------------------------------------------------------------------------------------------------------------------------+
  67. | :ref:`bool<class_bool>` | :ref:`_process<class_MainLoop_private_method__process>` **(** :ref:`float<class_float>` delta **)** |virtual| |
  68. +-------------------------+-------------------------------------------------------------------------------------------------------------------------------+
  69. .. rst-class:: classref-section-separator
  70. ----
  71. .. rst-class:: classref-descriptions-group
  72. Signals
  73. -------
  74. .. _class_MainLoop_signal_on_request_permissions_result:
  75. .. rst-class:: classref-signal
  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. .. rst-class:: classref-section-separator
  79. ----
  80. .. rst-class:: classref-descriptions-group
  81. Constants
  82. ---------
  83. .. _class_MainLoop_constant_NOTIFICATION_OS_MEMORY_WARNING:
  84. .. rst-class:: classref-constant
  85. **NOTIFICATION_OS_MEMORY_WARNING** = ``2009``
  86. Notification received from the OS when the application is exceeding its allocated memory.
  87. Specific to the iOS platform.
  88. .. _class_MainLoop_constant_NOTIFICATION_TRANSLATION_CHANGED:
  89. .. rst-class:: classref-constant
  90. **NOTIFICATION_TRANSLATION_CHANGED** = ``2010``
  91. 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>`.
  92. .. _class_MainLoop_constant_NOTIFICATION_WM_ABOUT:
  93. .. rst-class:: classref-constant
  94. **NOTIFICATION_WM_ABOUT** = ``2011``
  95. Notification received from the OS when a request for "About" information is sent.
  96. Specific to the macOS platform.
  97. .. _class_MainLoop_constant_NOTIFICATION_CRASH:
  98. .. rst-class:: classref-constant
  99. **NOTIFICATION_CRASH** = ``2012``
  100. Notification received from Godot's crash handler when the engine is about to crash.
  101. Implemented on desktop platforms if the crash handler is enabled.
  102. .. _class_MainLoop_constant_NOTIFICATION_OS_IME_UPDATE:
  103. .. rst-class:: classref-constant
  104. **NOTIFICATION_OS_IME_UPDATE** = ``2013``
  105. Notification received from the OS when an update of the Input Method Engine occurs (e.g. change of IME cursor position or composition string).
  106. Specific to the macOS platform.
  107. .. _class_MainLoop_constant_NOTIFICATION_APPLICATION_RESUMED:
  108. .. rst-class:: classref-constant
  109. **NOTIFICATION_APPLICATION_RESUMED** = ``2014``
  110. Notification received from the OS when the application is resumed.
  111. Specific to the Android platform.
  112. .. _class_MainLoop_constant_NOTIFICATION_APPLICATION_PAUSED:
  113. .. rst-class:: classref-constant
  114. **NOTIFICATION_APPLICATION_PAUSED** = ``2015``
  115. Notification received from the OS when the application is paused.
  116. Specific to the Android platform.
  117. .. _class_MainLoop_constant_NOTIFICATION_APPLICATION_FOCUS_IN:
  118. .. rst-class:: classref-constant
  119. **NOTIFICATION_APPLICATION_FOCUS_IN** = ``2016``
  120. 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.
  121. Implemented on desktop platforms.
  122. .. _class_MainLoop_constant_NOTIFICATION_APPLICATION_FOCUS_OUT:
  123. .. rst-class:: classref-constant
  124. **NOTIFICATION_APPLICATION_FOCUS_OUT** = ``2017``
  125. 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.
  126. Implemented on desktop platforms.
  127. .. _class_MainLoop_constant_NOTIFICATION_TEXT_SERVER_CHANGED:
  128. .. rst-class:: classref-constant
  129. **NOTIFICATION_TEXT_SERVER_CHANGED** = ``2018``
  130. Notification received when text server is changed.
  131. .. rst-class:: classref-section-separator
  132. ----
  133. .. rst-class:: classref-descriptions-group
  134. Method Descriptions
  135. -------------------
  136. .. _class_MainLoop_private_method__finalize:
  137. .. rst-class:: classref-method
  138. void **_finalize** **(** **)** |virtual|
  139. Called before the program exits.
  140. .. rst-class:: classref-item-separator
  141. ----
  142. .. _class_MainLoop_private_method__initialize:
  143. .. rst-class:: classref-method
  144. void **_initialize** **(** **)** |virtual|
  145. Called once during initialization.
  146. .. rst-class:: classref-item-separator
  147. ----
  148. .. _class_MainLoop_private_method__physics_process:
  149. .. rst-class:: classref-method
  150. :ref:`bool<class_bool>` **_physics_process** **(** :ref:`float<class_float>` delta **)** |virtual|
  151. 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>`.
  152. If implemented, the method must return a boolean value. ``true`` ends the main loop, while ``false`` lets it proceed to the next frame.
  153. .. rst-class:: classref-item-separator
  154. ----
  155. .. _class_MainLoop_private_method__process:
  156. .. rst-class:: classref-method
  157. :ref:`bool<class_bool>` **_process** **(** :ref:`float<class_float>` delta **)** |virtual|
  158. 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>`.
  159. If implemented, the method must return a boolean value. ``true`` ends the main loop, while ``false`` lets it proceed to the next frame.
  160. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  161. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  162. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  163. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  164. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  165. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  166. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`