class_timer.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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/Timer.xml.
  6. .. _class_Timer:
  7. Timer
  8. =====
  9. **Inherits:** :ref:`Node<class_Node>` **<** :ref:`Object<class_Object>`
  10. A countdown timer.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. The **Timer** node is a countdown timer and is the simplest way to handle time-based logic in the engine. When a timer reaches the end of its :ref:`wait_time<class_Timer_property_wait_time>`, it will emit the :ref:`timeout<class_Timer_signal_timeout>` signal.
  15. After a timer enters the tree, it can be manually started with :ref:`start<class_Timer_method_start>`. A timer node is also started automatically if :ref:`autostart<class_Timer_property_autostart>` is ``true``.
  16. Without requiring much code, a timer node can be added and configured in the editor. The :ref:`timeout<class_Timer_signal_timeout>` signal it emits can also be connected through the Node dock in the editor:
  17. ::
  18. func _on_timer_timeout():
  19. print("Time to attack!")
  20. \ **Note:** To create a one-shot timer without instantiating a node, use :ref:`SceneTree.create_timer<class_SceneTree_method_create_timer>`.
  21. \ **Note:** Timers are affected by :ref:`Engine.time_scale<class_Engine_property_time_scale>`. The higher the time scale, the sooner timers will end. How often a timer processes may depend on the framerate or :ref:`Engine.physics_ticks_per_second<class_Engine_property_physics_ticks_per_second>`.
  22. .. rst-class:: classref-introduction-group
  23. Tutorials
  24. ---------
  25. - `2D Dodge The Creeps Demo <https://godotengine.org/asset-library/asset/2712>`__
  26. .. rst-class:: classref-reftable-group
  27. Properties
  28. ----------
  29. .. table::
  30. :widths: auto
  31. +--------------------------------------------------------------+------------------------------------------------------------------+-----------+
  32. | :ref:`bool<class_bool>` | :ref:`autostart<class_Timer_property_autostart>` | ``false`` |
  33. +--------------------------------------------------------------+------------------------------------------------------------------+-----------+
  34. | :ref:`bool<class_bool>` | :ref:`ignore_time_scale<class_Timer_property_ignore_time_scale>` | ``false`` |
  35. +--------------------------------------------------------------+------------------------------------------------------------------+-----------+
  36. | :ref:`bool<class_bool>` | :ref:`one_shot<class_Timer_property_one_shot>` | ``false`` |
  37. +--------------------------------------------------------------+------------------------------------------------------------------+-----------+
  38. | :ref:`bool<class_bool>` | :ref:`paused<class_Timer_property_paused>` | |
  39. +--------------------------------------------------------------+------------------------------------------------------------------+-----------+
  40. | :ref:`TimerProcessCallback<enum_Timer_TimerProcessCallback>` | :ref:`process_callback<class_Timer_property_process_callback>` | ``1`` |
  41. +--------------------------------------------------------------+------------------------------------------------------------------+-----------+
  42. | :ref:`float<class_float>` | :ref:`time_left<class_Timer_property_time_left>` | |
  43. +--------------------------------------------------------------+------------------------------------------------------------------+-----------+
  44. | :ref:`float<class_float>` | :ref:`wait_time<class_Timer_property_wait_time>` | ``1.0`` |
  45. +--------------------------------------------------------------+------------------------------------------------------------------+-----------+
  46. .. rst-class:: classref-reftable-group
  47. Methods
  48. -------
  49. .. table::
  50. :widths: auto
  51. +-------------------------+-----------------------------------------------------------------------------------------+
  52. | :ref:`bool<class_bool>` | :ref:`is_stopped<class_Timer_method_is_stopped>`\ (\ ) |const| |
  53. +-------------------------+-----------------------------------------------------------------------------------------+
  54. | |void| | :ref:`start<class_Timer_method_start>`\ (\ time_sec\: :ref:`float<class_float>` = -1\ ) |
  55. +-------------------------+-----------------------------------------------------------------------------------------+
  56. | |void| | :ref:`stop<class_Timer_method_stop>`\ (\ ) |
  57. +-------------------------+-----------------------------------------------------------------------------------------+
  58. .. rst-class:: classref-section-separator
  59. ----
  60. .. rst-class:: classref-descriptions-group
  61. Signals
  62. -------
  63. .. _class_Timer_signal_timeout:
  64. .. rst-class:: classref-signal
  65. **timeout**\ (\ ) :ref:`🔗<class_Timer_signal_timeout>`
  66. Emitted when the timer reaches the end.
  67. .. rst-class:: classref-section-separator
  68. ----
  69. .. rst-class:: classref-descriptions-group
  70. Enumerations
  71. ------------
  72. .. _enum_Timer_TimerProcessCallback:
  73. .. rst-class:: classref-enumeration
  74. enum **TimerProcessCallback**: :ref:`🔗<enum_Timer_TimerProcessCallback>`
  75. .. _class_Timer_constant_TIMER_PROCESS_PHYSICS:
  76. .. rst-class:: classref-enumeration-constant
  77. :ref:`TimerProcessCallback<enum_Timer_TimerProcessCallback>` **TIMER_PROCESS_PHYSICS** = ``0``
  78. Update the timer every physics process frame (see :ref:`Node.NOTIFICATION_INTERNAL_PHYSICS_PROCESS<class_Node_constant_NOTIFICATION_INTERNAL_PHYSICS_PROCESS>`).
  79. .. _class_Timer_constant_TIMER_PROCESS_IDLE:
  80. .. rst-class:: classref-enumeration-constant
  81. :ref:`TimerProcessCallback<enum_Timer_TimerProcessCallback>` **TIMER_PROCESS_IDLE** = ``1``
  82. Update the timer every process (rendered) frame (see :ref:`Node.NOTIFICATION_INTERNAL_PROCESS<class_Node_constant_NOTIFICATION_INTERNAL_PROCESS>`).
  83. .. rst-class:: classref-section-separator
  84. ----
  85. .. rst-class:: classref-descriptions-group
  86. Property Descriptions
  87. ---------------------
  88. .. _class_Timer_property_autostart:
  89. .. rst-class:: classref-property
  90. :ref:`bool<class_bool>` **autostart** = ``false`` :ref:`🔗<class_Timer_property_autostart>`
  91. .. rst-class:: classref-property-setget
  92. - |void| **set_autostart**\ (\ value\: :ref:`bool<class_bool>`\ )
  93. - :ref:`bool<class_bool>` **has_autostart**\ (\ )
  94. If ``true``, the timer will start immediately when it enters the scene tree.
  95. \ **Note:** After the timer enters the tree, this property is automatically set to ``false``.
  96. \ **Note:** This property does nothing when the timer is running in the editor.
  97. .. rst-class:: classref-item-separator
  98. ----
  99. .. _class_Timer_property_ignore_time_scale:
  100. .. rst-class:: classref-property
  101. :ref:`bool<class_bool>` **ignore_time_scale** = ``false`` :ref:`🔗<class_Timer_property_ignore_time_scale>`
  102. .. rst-class:: classref-property-setget
  103. - |void| **set_ignore_time_scale**\ (\ value\: :ref:`bool<class_bool>`\ )
  104. - :ref:`bool<class_bool>` **is_ignoring_time_scale**\ (\ )
  105. If ``true``, the timer will ignore :ref:`Engine.time_scale<class_Engine_property_time_scale>` and update with the real, elapsed time.
  106. .. rst-class:: classref-item-separator
  107. ----
  108. .. _class_Timer_property_one_shot:
  109. .. rst-class:: classref-property
  110. :ref:`bool<class_bool>` **one_shot** = ``false`` :ref:`🔗<class_Timer_property_one_shot>`
  111. .. rst-class:: classref-property-setget
  112. - |void| **set_one_shot**\ (\ value\: :ref:`bool<class_bool>`\ )
  113. - :ref:`bool<class_bool>` **is_one_shot**\ (\ )
  114. If ``true``, the timer will stop after reaching the end. Otherwise, as by default, the timer will automatically restart.
  115. .. rst-class:: classref-item-separator
  116. ----
  117. .. _class_Timer_property_paused:
  118. .. rst-class:: classref-property
  119. :ref:`bool<class_bool>` **paused** :ref:`🔗<class_Timer_property_paused>`
  120. .. rst-class:: classref-property-setget
  121. - |void| **set_paused**\ (\ value\: :ref:`bool<class_bool>`\ )
  122. - :ref:`bool<class_bool>` **is_paused**\ (\ )
  123. If ``true``, the timer is paused. A paused timer does not process until this property is set back to ``false``, even when :ref:`start<class_Timer_method_start>` is called.
  124. .. rst-class:: classref-item-separator
  125. ----
  126. .. _class_Timer_property_process_callback:
  127. .. rst-class:: classref-property
  128. :ref:`TimerProcessCallback<enum_Timer_TimerProcessCallback>` **process_callback** = ``1`` :ref:`🔗<class_Timer_property_process_callback>`
  129. .. rst-class:: classref-property-setget
  130. - |void| **set_timer_process_callback**\ (\ value\: :ref:`TimerProcessCallback<enum_Timer_TimerProcessCallback>`\ )
  131. - :ref:`TimerProcessCallback<enum_Timer_TimerProcessCallback>` **get_timer_process_callback**\ (\ )
  132. Specifies when the timer is updated during the main loop (see :ref:`TimerProcessCallback<enum_Timer_TimerProcessCallback>`).
  133. .. rst-class:: classref-item-separator
  134. ----
  135. .. _class_Timer_property_time_left:
  136. .. rst-class:: classref-property
  137. :ref:`float<class_float>` **time_left** :ref:`🔗<class_Timer_property_time_left>`
  138. .. rst-class:: classref-property-setget
  139. - :ref:`float<class_float>` **get_time_left**\ (\ )
  140. The timer's remaining time in seconds. This is always ``0`` if the timer is stopped.
  141. \ **Note:** This property is read-only and cannot be modified. It is based on :ref:`wait_time<class_Timer_property_wait_time>`.
  142. .. rst-class:: classref-item-separator
  143. ----
  144. .. _class_Timer_property_wait_time:
  145. .. rst-class:: classref-property
  146. :ref:`float<class_float>` **wait_time** = ``1.0`` :ref:`🔗<class_Timer_property_wait_time>`
  147. .. rst-class:: classref-property-setget
  148. - |void| **set_wait_time**\ (\ value\: :ref:`float<class_float>`\ )
  149. - :ref:`float<class_float>` **get_wait_time**\ (\ )
  150. The time required for the timer to end, in seconds. This property can also be set every time :ref:`start<class_Timer_method_start>` is called.
  151. \ **Note:** Timers can only process once per physics or process frame (depending on the :ref:`process_callback<class_Timer_property_process_callback>`). An unstable framerate may cause the timer to end inconsistently, which is especially noticeable if the wait time is lower than roughly ``0.05`` seconds. For very short timers, it is recommended to write your own code instead of using a **Timer** node. Timers are also affected by :ref:`Engine.time_scale<class_Engine_property_time_scale>`.
  152. .. rst-class:: classref-section-separator
  153. ----
  154. .. rst-class:: classref-descriptions-group
  155. Method Descriptions
  156. -------------------
  157. .. _class_Timer_method_is_stopped:
  158. .. rst-class:: classref-method
  159. :ref:`bool<class_bool>` **is_stopped**\ (\ ) |const| :ref:`🔗<class_Timer_method_is_stopped>`
  160. Returns ``true`` if the timer is stopped or has not started.
  161. .. rst-class:: classref-item-separator
  162. ----
  163. .. _class_Timer_method_start:
  164. .. rst-class:: classref-method
  165. |void| **start**\ (\ time_sec\: :ref:`float<class_float>` = -1\ ) :ref:`🔗<class_Timer_method_start>`
  166. Starts the timer, or resets the timer if it was started already. Fails if the timer is not inside the tree. If ``time_sec`` is greater than ``0``, this value is used for the :ref:`wait_time<class_Timer_property_wait_time>`.
  167. \ **Note:** This method does not resume a paused timer. See :ref:`paused<class_Timer_property_paused>`.
  168. .. rst-class:: classref-item-separator
  169. ----
  170. .. _class_Timer_method_stop:
  171. .. rst-class:: classref-method
  172. |void| **stop**\ (\ ) :ref:`🔗<class_Timer_method_stop>`
  173. Stops the timer.
  174. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  175. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  176. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  177. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  178. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  179. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  180. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
  181. .. |void| replace:: :abbr:`void (No return value.)`