class_bool.rst 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/make_rst.py in Godot's source tree.
  3. .. DO NOT EDIT THIS FILE, but the bool.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_bool:
  6. bool
  7. ====
  8. Boolean built-in type.
  9. Description
  10. -----------
  11. 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.
  12. 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``.
  13. .. tabs::
  14. .. code-tab:: gdscript
  15. var _can_shoot = true
  16. func shoot():
  17. if _can_shoot:
  18. pass # Perform shooting actions here.
  19. .. code-tab:: csharp
  20. private bool _canShoot = true;
  21. public void Shoot()
  22. {
  23. if (_canShoot)
  24. {
  25. // Perform shooting actions here.
  26. }
  27. }
  28. The following code will only create a bullet if both conditions are met: action "shoot" is pressed and if ``can_shoot`` is ``true``.
  29. \ **Note:** ``Input.is_action_pressed("shoot")`` is also a boolean that is ``true`` when "shoot" is pressed and ``false`` when "shoot" isn't pressed.
  30. .. tabs::
  31. .. code-tab:: gdscript
  32. var _can_shoot = true
  33. func shoot():
  34. if _can_shoot and Input.is_action_pressed("shoot"):
  35. create_bullet()
  36. .. code-tab:: csharp
  37. private bool _canShoot = true;
  38. public void Shoot()
  39. {
  40. if (_canShoot && Input.IsActionPressed("shoot"))
  41. {
  42. CreateBullet();
  43. }
  44. }
  45. 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.
  46. .. tabs::
  47. .. code-tab:: gdscript
  48. var _can_shoot = true
  49. @onready var _cool_down = $CoolDownTimer
  50. func shoot():
  51. if _can_shoot and Input.is_action_pressed("shoot"):
  52. create_bullet()
  53. _can_shoot = false
  54. _cool_down.start()
  55. func _on_CoolDownTimer_timeout():
  56. _can_shoot = true
  57. .. code-tab:: csharp
  58. private bool _canShoot = true;
  59. private Timer _coolDown;
  60. public override void _Ready()
  61. {
  62. _coolDown = GetNode<Timer>("CoolDownTimer");
  63. }
  64. public void Shoot()
  65. {
  66. if (_canShoot && Input.IsActionPressed("shoot"))
  67. {
  68. CreateBullet();
  69. _canShoot = false;
  70. _coolDown.Start();
  71. }
  72. }
  73. public void OnCoolDownTimerTimeout()
  74. {
  75. _canShoot = true;
  76. }
  77. Constructors
  78. ------------
  79. +-------------------------+-------------------------------------------------------------------------------------+
  80. | :ref:`bool<class_bool>` | :ref:`bool<class_bool_constructor_bool>` **(** **)** |
  81. +-------------------------+-------------------------------------------------------------------------------------+
  82. | :ref:`bool<class_bool>` | :ref:`bool<class_bool_constructor_bool>` **(** :ref:`bool<class_bool>` from **)** |
  83. +-------------------------+-------------------------------------------------------------------------------------+
  84. | :ref:`bool<class_bool>` | :ref:`bool<class_bool_constructor_bool>` **(** :ref:`float<class_float>` from **)** |
  85. +-------------------------+-------------------------------------------------------------------------------------+
  86. | :ref:`bool<class_bool>` | :ref:`bool<class_bool_constructor_bool>` **(** :ref:`int<class_int>` from **)** |
  87. +-------------------------+-------------------------------------------------------------------------------------+
  88. Operators
  89. ---------
  90. +-------------------------+--------------------------------------------------------------------------------------------+
  91. | :ref:`bool<class_bool>` | :ref:`operator !=<class_bool_operator_neq_bool>` **(** **)** |
  92. +-------------------------+--------------------------------------------------------------------------------------------+
  93. | :ref:`bool<class_bool>` | :ref:`operator !=<class_bool_operator_neq_bool>` **(** :ref:`bool<class_bool>` right **)** |
  94. +-------------------------+--------------------------------------------------------------------------------------------+
  95. | :ref:`bool<class_bool>` | :ref:`operator <<class_bool_operator_lt_bool>` **(** :ref:`bool<class_bool>` right **)** |
  96. +-------------------------+--------------------------------------------------------------------------------------------+
  97. | :ref:`bool<class_bool>` | :ref:`operator ==<class_bool_operator_eq_bool>` **(** **)** |
  98. +-------------------------+--------------------------------------------------------------------------------------------+
  99. | :ref:`bool<class_bool>` | :ref:`operator ==<class_bool_operator_eq_bool>` **(** :ref:`bool<class_bool>` right **)** |
  100. +-------------------------+--------------------------------------------------------------------------------------------+
  101. | :ref:`bool<class_bool>` | :ref:`operator ><class_bool_operator_gt_bool>` **(** :ref:`bool<class_bool>` right **)** |
  102. +-------------------------+--------------------------------------------------------------------------------------------+
  103. Constructor Descriptions
  104. ------------------------
  105. .. _class_bool_constructor_bool:
  106. - :ref:`bool<class_bool>` **bool** **(** **)**
  107. Constructs a default-initialized ``bool`` set to ``false``.
  108. ----
  109. - :ref:`bool<class_bool>` **bool** **(** :ref:`bool<class_bool>` from **)**
  110. Constructs a ``bool`` as a copy of the given ``bool``.
  111. ----
  112. - :ref:`bool<class_bool>` **bool** **(** :ref:`float<class_float>` from **)**
  113. 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.
  114. ----
  115. - :ref:`bool<class_bool>` **bool** **(** :ref:`int<class_int>` from **)**
  116. 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.
  117. Operator Descriptions
  118. ---------------------
  119. .. _class_bool_operator_neq_bool:
  120. - :ref:`bool<class_bool>` **operator !=** **(** **)**
  121. ----
  122. - :ref:`bool<class_bool>` **operator !=** **(** :ref:`bool<class_bool>` right **)**
  123. Returns ``true`` if two bools are different, i.e. one is ``true`` and the other is ``false``.
  124. ----
  125. .. _class_bool_operator_lt_bool:
  126. - :ref:`bool<class_bool>` **operator <** **(** :ref:`bool<class_bool>` right **)**
  127. Returns ``true`` if the left operand is ``false`` and the right operand is ``true``.
  128. ----
  129. .. _class_bool_operator_eq_bool:
  130. - :ref:`bool<class_bool>` **operator ==** **(** **)**
  131. Returns ``true`` if two bools are equal, i.e. both are ``true`` or both are ``false``.
  132. ----
  133. - :ref:`bool<class_bool>` **operator ==** **(** :ref:`bool<class_bool>` right **)**
  134. Returns ``true`` if two bools are equal, i.e. both are ``true`` or both are ``false``.
  135. ----
  136. .. _class_bool_operator_gt_bool:
  137. - :ref:`bool<class_bool>` **operator >** **(** :ref:`bool<class_bool>` right **)**
  138. Returns ``true`` if the left operand is ``true`` and the right operand is ``false``.
  139. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  140. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  141. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  142. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  143. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  144. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`