class_bool.rst 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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/bool.xml.
  6. .. _class_bool:
  7. bool
  8. ====
  9. Boolean built-in type.
  10. .. rst-class:: classref-introduction-group
  11. Description
  12. -----------
  13. Boolean is a built-in type. There are two boolean values: ``true`` and ``false``. You can think of it as a switch with on or off (1 or 0) setting. Booleans are used in programming for logic in condition statements, like ``if`` statements.
  14. Booleans can be directly used in ``if`` statements. The code below demonstrates this on the ``if can_shoot:`` line. You don't need to use ``== true``, you only need ``if can_shoot:``. Similarly, use ``if not can_shoot:`` rather than ``== false``.
  15. .. tabs::
  16. .. code-tab:: gdscript
  17. var _can_shoot = true
  18. func shoot():
  19. if _can_shoot:
  20. pass # Perform shooting actions here.
  21. .. code-tab:: csharp
  22. private bool _canShoot = true;
  23. public void Shoot()
  24. {
  25. if (_canShoot)
  26. {
  27. // Perform shooting actions here.
  28. }
  29. }
  30. The following code will only create a bullet if both conditions are met: action "shoot" is pressed and if ``can_shoot`` is ``true``.
  31. \ **Note:** ``Input.is_action_pressed("shoot")`` is also a boolean that is ``true`` when "shoot" is pressed and ``false`` when "shoot" isn't pressed.
  32. .. tabs::
  33. .. code-tab:: gdscript
  34. var _can_shoot = true
  35. func shoot():
  36. if _can_shoot and Input.is_action_pressed("shoot"):
  37. create_bullet()
  38. .. code-tab:: csharp
  39. private bool _canShoot = true;
  40. public void Shoot()
  41. {
  42. if (_canShoot && Input.IsActionPressed("shoot"))
  43. {
  44. CreateBullet();
  45. }
  46. }
  47. The following code will set ``can_shoot`` to ``false`` and start a timer. This will prevent player from shooting until the timer runs out. Next ``can_shoot`` will be set to ``true`` again allowing player to shoot once again.
  48. .. tabs::
  49. .. code-tab:: gdscript
  50. var _can_shoot = true
  51. @onready var _cool_down = $CoolDownTimer
  52. func shoot():
  53. if _can_shoot and Input.is_action_pressed("shoot"):
  54. create_bullet()
  55. _can_shoot = false
  56. _cool_down.start()
  57. func _on_cool_down_timer_timeout():
  58. _can_shoot = true
  59. .. code-tab:: csharp
  60. private bool _canShoot = true;
  61. private Timer _coolDown;
  62. public override void _Ready()
  63. {
  64. _coolDown = GetNode<Timer>("CoolDownTimer");
  65. }
  66. public void Shoot()
  67. {
  68. if (_canShoot && Input.IsActionPressed("shoot"))
  69. {
  70. CreateBullet();
  71. _canShoot = false;
  72. _coolDown.Start();
  73. }
  74. }
  75. public void OnCoolDownTimerTimeout()
  76. {
  77. _canShoot = true;
  78. }
  79. .. rst-class:: classref-reftable-group
  80. Constructors
  81. ------------
  82. .. table::
  83. :widths: auto
  84. +-------------------------+-------------------------------------------------------------------------------------+
  85. | :ref:`bool<class_bool>` | :ref:`bool<class_bool_constructor_bool>` **(** **)** |
  86. +-------------------------+-------------------------------------------------------------------------------------+
  87. | :ref:`bool<class_bool>` | :ref:`bool<class_bool_constructor_bool>` **(** :ref:`bool<class_bool>` from **)** |
  88. +-------------------------+-------------------------------------------------------------------------------------+
  89. | :ref:`bool<class_bool>` | :ref:`bool<class_bool_constructor_bool>` **(** :ref:`float<class_float>` from **)** |
  90. +-------------------------+-------------------------------------------------------------------------------------+
  91. | :ref:`bool<class_bool>` | :ref:`bool<class_bool_constructor_bool>` **(** :ref:`int<class_int>` from **)** |
  92. +-------------------------+-------------------------------------------------------------------------------------+
  93. .. rst-class:: classref-reftable-group
  94. Operators
  95. ---------
  96. .. table::
  97. :widths: auto
  98. +-------------------------+--------------------------------------------------------------------------------------------+
  99. | :ref:`bool<class_bool>` | :ref:`operator !=<class_bool_operator_neq_bool>` **(** :ref:`bool<class_bool>` right **)** |
  100. +-------------------------+--------------------------------------------------------------------------------------------+
  101. | :ref:`bool<class_bool>` | :ref:`operator \<<class_bool_operator_lt_bool>` **(** :ref:`bool<class_bool>` right **)** |
  102. +-------------------------+--------------------------------------------------------------------------------------------+
  103. | :ref:`bool<class_bool>` | :ref:`operator ==<class_bool_operator_eq_bool>` **(** :ref:`bool<class_bool>` right **)** |
  104. +-------------------------+--------------------------------------------------------------------------------------------+
  105. | :ref:`bool<class_bool>` | :ref:`operator ><class_bool_operator_gt_bool>` **(** :ref:`bool<class_bool>` right **)** |
  106. +-------------------------+--------------------------------------------------------------------------------------------+
  107. .. rst-class:: classref-section-separator
  108. ----
  109. .. rst-class:: classref-descriptions-group
  110. Constructor Descriptions
  111. ------------------------
  112. .. _class_bool_constructor_bool:
  113. .. rst-class:: classref-constructor
  114. :ref:`bool<class_bool>` **bool** **(** **)**
  115. Constructs a default-initialized **bool** set to ``false``.
  116. .. rst-class:: classref-item-separator
  117. ----
  118. .. rst-class:: classref-constructor
  119. :ref:`bool<class_bool>` **bool** **(** :ref:`bool<class_bool>` from **)**
  120. Constructs a **bool** as a copy of the given **bool**.
  121. .. rst-class:: classref-item-separator
  122. ----
  123. .. rst-class:: classref-constructor
  124. :ref:`bool<class_bool>` **bool** **(** :ref:`float<class_float>` from **)**
  125. Cast a :ref:`float<class_float>` value to a boolean value, this method will return ``false`` if ``0.0`` is passed in, and ``true`` for all other floats.
  126. .. rst-class:: classref-item-separator
  127. ----
  128. .. rst-class:: classref-constructor
  129. :ref:`bool<class_bool>` **bool** **(** :ref:`int<class_int>` from **)**
  130. Cast an :ref:`int<class_int>` value to a boolean value, this method will return ``false`` if ``0`` is passed in, and ``true`` for all other ints.
  131. .. rst-class:: classref-section-separator
  132. ----
  133. .. rst-class:: classref-descriptions-group
  134. Operator Descriptions
  135. ---------------------
  136. .. _class_bool_operator_neq_bool:
  137. .. rst-class:: classref-operator
  138. :ref:`bool<class_bool>` **operator !=** **(** :ref:`bool<class_bool>` right **)**
  139. Returns ``true`` if two bools are different, i.e. one is ``true`` and the other is ``false``.
  140. .. rst-class:: classref-item-separator
  141. ----
  142. .. _class_bool_operator_lt_bool:
  143. .. rst-class:: classref-operator
  144. :ref:`bool<class_bool>` **operator <** **(** :ref:`bool<class_bool>` right **)**
  145. Returns ``true`` if the left operand is ``false`` and the right operand is ``true``.
  146. .. rst-class:: classref-item-separator
  147. ----
  148. .. _class_bool_operator_eq_bool:
  149. .. rst-class:: classref-operator
  150. :ref:`bool<class_bool>` **operator ==** **(** :ref:`bool<class_bool>` right **)**
  151. Returns ``true`` if two bools are equal, i.e. both are ``true`` or both are ``false``.
  152. .. rst-class:: classref-item-separator
  153. ----
  154. .. _class_bool_operator_gt_bool:
  155. .. rst-class:: classref-operator
  156. :ref:`bool<class_bool>` **operator >** **(** :ref:`bool<class_bool>` right **)**
  157. Returns ``true`` if the left operand is ``true`` and the right operand is ``false``.
  158. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  159. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  160. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  161. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  162. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  163. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`