class_bool.rst 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/3.6/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/3.6/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. ::
  16. var can_shoot = true
  17. func shoot():
  18. if can_shoot:
  19. pass # Perform shooting actions here.
  20. The following code will only create a bullet if both conditions are met: action "shoot" is pressed and if ``can_shoot`` is ``true``.
  21. \ **Note:** ``Input.is_action_pressed("shoot")`` is also a boolean that is ``true`` when "shoot" is pressed and ``false`` when "shoot" isn't pressed.
  22. ::
  23. var can_shoot = true
  24. func shoot():
  25. if can_shoot and Input.is_action_pressed("shoot"):
  26. create_bullet()
  27. 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.
  28. ::
  29. var can_shoot = true
  30. onready var cool_down = $CoolDownTimer
  31. func shoot():
  32. if can_shoot and Input.is_action_pressed("shoot"):
  33. create_bullet()
  34. can_shoot = false
  35. cool_down.start()
  36. func _on_CoolDownTimer_timeout():
  37. can_shoot = true
  38. .. rst-class:: classref-reftable-group
  39. Methods
  40. -------
  41. .. table::
  42. :widths: auto
  43. +-------------------------+----------------------------------------------------------------------------------+
  44. | :ref:`bool<class_bool>` | :ref:`bool<class_bool_method_bool>` **(** :ref:`int<class_int>` from **)** |
  45. +-------------------------+----------------------------------------------------------------------------------+
  46. | :ref:`bool<class_bool>` | :ref:`bool<class_bool_method_bool>` **(** :ref:`float<class_float>` from **)** |
  47. +-------------------------+----------------------------------------------------------------------------------+
  48. | :ref:`bool<class_bool>` | :ref:`bool<class_bool_method_bool>` **(** :ref:`String<class_String>` from **)** |
  49. +-------------------------+----------------------------------------------------------------------------------+
  50. .. rst-class:: classref-section-separator
  51. ----
  52. .. rst-class:: classref-descriptions-group
  53. Method Descriptions
  54. -------------------
  55. .. _class_bool_method_bool:
  56. .. rst-class:: classref-method
  57. :ref:`bool<class_bool>` **bool** **(** :ref:`int<class_int>` from **)**
  58. 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.
  59. .. rst-class:: classref-item-separator
  60. ----
  61. .. rst-class:: classref-method
  62. :ref:`bool<class_bool>` **bool** **(** :ref:`float<class_float>` from **)**
  63. 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.
  64. .. rst-class:: classref-item-separator
  65. ----
  66. .. rst-class:: classref-method
  67. :ref:`bool<class_bool>` **bool** **(** :ref:`String<class_String>` from **)**
  68. Cast a :ref:`String<class_String>` value to a boolean value, this method will return ``false`` if ``""`` is passed in, and ``true`` for all non-empty strings.
  69. Examples: ``bool("False")`` returns ``true``, ``bool("")`` returns ``false``.
  70. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  71. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  72. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  73. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`