class_bool.rst 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. A built-in boolean type.
  10. .. rst-class:: classref-introduction-group
  11. Description
  12. -----------
  13. The **bool** is a built-in :ref:`Variant<class_Variant>` type that may only store one of two values: ``true`` or ``false``. You can imagine it as a switch that can be either turned on or off, or as a binary digit that can either be 1 or 0.
  14. Booleans can be directly used in ``if``, and other conditional statements:
  15. .. tabs::
  16. .. code-tab:: gdscript
  17. var can_shoot = true
  18. if can_shoot:
  19. launch_bullet()
  20. .. code-tab:: csharp
  21. bool canShoot = true;
  22. if (canShoot)
  23. {
  24. LaunchBullet();
  25. }
  26. All comparison operators return booleans (``==``, ``>``, ``<=``, etc.). As such, it is not necessary to compare booleans themselves. You do not need to add ``== true`` or ``== false``.
  27. Booleans can be combined with the logical operators ``and``, ``or``, ``not`` to create complex conditions:
  28. .. tabs::
  29. .. code-tab:: gdscript
  30. if bullets > 0 and not is_reloading():
  31. launch_bullet()
  32. if bullets == 0 or is_reloading():
  33. play_clack_sound()
  34. .. code-tab:: csharp
  35. if (bullets > 0 && !IsReloading())
  36. {
  37. LaunchBullet();
  38. }
  39. if (bullets == 0 || IsReloading())
  40. {
  41. PlayClackSound();
  42. }
  43. \ **Note:** In modern programming languages, logical operators are evaluated in order. All remaining conditions are skipped if their result would have no effect on the final value. This concept is known as `short-circuit evaluation <https://en.wikipedia.org/wiki/Short-circuit_evaluation>`__ and can be useful to avoid evaluating expensive conditions in some performance-critical cases.
  44. \ **Note:** By convention, built-in methods and properties that return booleans are usually defined as yes-no questions, single adjectives, or similar (:ref:`String.is_empty<class_String_method_is_empty>`, :ref:`Node.can_process<class_Node_method_can_process>`, :ref:`Camera2D.enabled<class_Camera2D_property_enabled>`, etc.).
  45. .. rst-class:: classref-reftable-group
  46. Constructors
  47. ------------
  48. .. table::
  49. :widths: auto
  50. +-------------------------+----------------------------------------------------------------------------------+
  51. | :ref:`bool<class_bool>` | :ref:`bool<class_bool_constructor_bool>`\ (\ ) |
  52. +-------------------------+----------------------------------------------------------------------------------+
  53. | :ref:`bool<class_bool>` | :ref:`bool<class_bool_constructor_bool>`\ (\ from\: :ref:`bool<class_bool>`\ ) |
  54. +-------------------------+----------------------------------------------------------------------------------+
  55. | :ref:`bool<class_bool>` | :ref:`bool<class_bool_constructor_bool>`\ (\ from\: :ref:`float<class_float>`\ ) |
  56. +-------------------------+----------------------------------------------------------------------------------+
  57. | :ref:`bool<class_bool>` | :ref:`bool<class_bool_constructor_bool>`\ (\ from\: :ref:`int<class_int>`\ ) |
  58. +-------------------------+----------------------------------------------------------------------------------+
  59. .. rst-class:: classref-reftable-group
  60. Operators
  61. ---------
  62. .. table::
  63. :widths: auto
  64. +-------------------------+-----------------------------------------------------------------------------------------+
  65. | :ref:`bool<class_bool>` | :ref:`operator !=<class_bool_operator_neq_bool>`\ (\ right\: :ref:`bool<class_bool>`\ ) |
  66. +-------------------------+-----------------------------------------------------------------------------------------+
  67. | :ref:`bool<class_bool>` | :ref:`operator \<<class_bool_operator_lt_bool>`\ (\ right\: :ref:`bool<class_bool>`\ ) |
  68. +-------------------------+-----------------------------------------------------------------------------------------+
  69. | :ref:`bool<class_bool>` | :ref:`operator ==<class_bool_operator_eq_bool>`\ (\ right\: :ref:`bool<class_bool>`\ ) |
  70. +-------------------------+-----------------------------------------------------------------------------------------+
  71. | :ref:`bool<class_bool>` | :ref:`operator ><class_bool_operator_gt_bool>`\ (\ right\: :ref:`bool<class_bool>`\ ) |
  72. +-------------------------+-----------------------------------------------------------------------------------------+
  73. .. rst-class:: classref-section-separator
  74. ----
  75. .. rst-class:: classref-descriptions-group
  76. Constructor Descriptions
  77. ------------------------
  78. .. _class_bool_constructor_bool:
  79. .. rst-class:: classref-constructor
  80. :ref:`bool<class_bool>` **bool**\ (\ ) :ref:`🔗<class_bool_constructor_bool>`
  81. Constructs a **bool** set to ``false``.
  82. .. rst-class:: classref-item-separator
  83. ----
  84. .. rst-class:: classref-constructor
  85. :ref:`bool<class_bool>` **bool**\ (\ from\: :ref:`bool<class_bool>`\ )
  86. Constructs a **bool** as a copy of the given **bool**.
  87. .. rst-class:: classref-item-separator
  88. ----
  89. .. rst-class:: classref-constructor
  90. :ref:`bool<class_bool>` **bool**\ (\ from\: :ref:`float<class_float>`\ )
  91. Cast a :ref:`float<class_float>` value to a boolean value. Returns ``false`` if ``from`` is equal to ``0.0`` (including ``-0.0``), and ``true`` for all other values (including :ref:`@GDScript.INF<class_@GDScript_constant_INF>` and :ref:`@GDScript.NAN<class_@GDScript_constant_NAN>`).
  92. .. rst-class:: classref-item-separator
  93. ----
  94. .. rst-class:: classref-constructor
  95. :ref:`bool<class_bool>` **bool**\ (\ from\: :ref:`int<class_int>`\ )
  96. Cast an :ref:`int<class_int>` value to a boolean value. Returns ``false`` if ``from`` is equal to ``0``, and ``true`` for all other values.
  97. .. rst-class:: classref-section-separator
  98. ----
  99. .. rst-class:: classref-descriptions-group
  100. Operator Descriptions
  101. ---------------------
  102. .. _class_bool_operator_neq_bool:
  103. .. rst-class:: classref-operator
  104. :ref:`bool<class_bool>` **operator !=**\ (\ right\: :ref:`bool<class_bool>`\ ) :ref:`🔗<class_bool_operator_neq_bool>`
  105. Returns ``true`` if the two booleans are not equal. That is, one is ``true`` and the other is ``false``. This operation can be seen as a logical XOR.
  106. .. rst-class:: classref-item-separator
  107. ----
  108. .. _class_bool_operator_lt_bool:
  109. .. rst-class:: classref-operator
  110. :ref:`bool<class_bool>` **operator <**\ (\ right\: :ref:`bool<class_bool>`\ ) :ref:`🔗<class_bool_operator_lt_bool>`
  111. Returns ``true`` if the left operand is ``false`` and the right operand is ``true``.
  112. .. rst-class:: classref-item-separator
  113. ----
  114. .. _class_bool_operator_eq_bool:
  115. .. rst-class:: classref-operator
  116. :ref:`bool<class_bool>` **operator ==**\ (\ right\: :ref:`bool<class_bool>`\ ) :ref:`🔗<class_bool_operator_eq_bool>`
  117. Returns ``true`` if the two booleans are equal. That is, both are ``true`` or both are ``false``. This operation can be seen as a logical EQ or XNOR.
  118. .. rst-class:: classref-item-separator
  119. ----
  120. .. _class_bool_operator_gt_bool:
  121. .. rst-class:: classref-operator
  122. :ref:`bool<class_bool>` **operator >**\ (\ right\: :ref:`bool<class_bool>`\ ) :ref:`🔗<class_bool_operator_gt_bool>`
  123. Returns ``true`` if the left operand is ``true`` and the right operand is ``false``.
  124. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  125. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  126. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  127. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  128. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  129. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  130. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
  131. .. |void| replace:: :abbr:`void (No return value.)`