bool.xml 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <class name="bool" version="4.0">
  3. <brief_description>
  4. Boolean built-in type.
  5. </brief_description>
  6. <description>
  7. Boolean is a built-in type. There are two boolean values: [code]true[/code] and [code]false[/code]. 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 [code]if[/code] statements.
  8. Booleans can be directly used in [code]if[/code] statements. The code below demonstrates this on the [code]if can_shoot:[/code] line. You don't need to use [code]== true[/code], you only need [code]if can_shoot:[/code]. Similarly, use [code]if not can_shoot:[/code] rather than [code]== false[/code].
  9. [codeblocks]
  10. [gdscript]
  11. var _can_shoot = true
  12. func shoot():
  13. if _can_shoot:
  14. pass # Perform shooting actions here.
  15. [/gdscript]
  16. [csharp]
  17. private bool _canShoot = true;
  18. public void Shoot()
  19. {
  20. if (_canShoot)
  21. {
  22. // Perform shooting actions here.
  23. }
  24. }
  25. [/csharp]
  26. [/codeblocks]
  27. The following code will only create a bullet if both conditions are met: action "shoot" is pressed and if [code]can_shoot[/code] is [code]true[/code].
  28. [b]Note:[/b] [code]Input.is_action_pressed("shoot")[/code] is also a boolean that is [code]true[/code] when "shoot" is pressed and [code]false[/code] when "shoot" isn't pressed.
  29. [codeblocks]
  30. [gdscript]
  31. var _can_shoot = true
  32. func shoot():
  33. if _can_shoot and Input.is_action_pressed("shoot"):
  34. create_bullet()
  35. [/gdscript]
  36. [csharp]
  37. private bool _canShoot = true;
  38. public void Shoot()
  39. {
  40. if (_canShoot &amp;&amp; Input.IsActionPressed("shoot"))
  41. {
  42. CreateBullet();
  43. }
  44. }
  45. [/csharp]
  46. [/codeblocks]
  47. The following code will set [code]can_shoot[/code] to [code]false[/code] and start a timer. This will prevent player from shooting until the timer runs out. Next [code]can_shoot[/code] will be set to [code]true[/code] again allowing player to shoot once again.
  48. [codeblocks]
  49. [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_CoolDownTimer_timeout():
  58. _can_shoot = true
  59. [/gdscript]
  60. [csharp]
  61. private bool _canShoot = true;
  62. private Timer _coolDown;
  63. public override void _Ready()
  64. {
  65. _coolDown = GetNode&lt;Timer&gt;("CoolDownTimer");
  66. }
  67. public void Shoot()
  68. {
  69. if (_canShoot &amp;&amp; Input.IsActionPressed("shoot"))
  70. {
  71. CreateBullet();
  72. _canShoot = false;
  73. _coolDown.Start();
  74. }
  75. }
  76. public void OnCoolDownTimerTimeout()
  77. {
  78. _canShoot = true;
  79. }
  80. [/csharp]
  81. [/codeblocks]
  82. </description>
  83. <tutorials>
  84. </tutorials>
  85. <constructors>
  86. <constructor name="bool">
  87. <return type="bool" />
  88. <description>
  89. Constructs a default-initialized [bool] set to [code]false[/code].
  90. </description>
  91. </constructor>
  92. <constructor name="bool">
  93. <return type="bool" />
  94. <argument index="0" name="from" type="bool" />
  95. <description>
  96. Constructs a [bool] as a copy of the given [bool].
  97. </description>
  98. </constructor>
  99. <constructor name="bool">
  100. <return type="bool" />
  101. <argument index="0" name="from" type="float" />
  102. <description>
  103. Cast a [float] value to a boolean value, this method will return [code]false[/code] if [code]0.0[/code] is passed in, and [code]true[/code] for all other floats.
  104. </description>
  105. </constructor>
  106. <constructor name="bool">
  107. <return type="bool" />
  108. <argument index="0" name="from" type="int" />
  109. <description>
  110. Cast an [int] value to a boolean value, this method will return [code]false[/code] if [code]0[/code] is passed in, and [code]true[/code] for all other ints.
  111. </description>
  112. </constructor>
  113. </constructors>
  114. <operators>
  115. <operator name="operator !=">
  116. <return type="bool" />
  117. <description>
  118. </description>
  119. </operator>
  120. <operator name="operator !=">
  121. <return type="bool" />
  122. <argument index="0" name="right" type="bool" />
  123. <description>
  124. Returns [code]true[/code] if two bools are different, i.e. one is [code]true[/code] and the other is [code]false[/code].
  125. </description>
  126. </operator>
  127. <operator name="operator &lt;">
  128. <return type="bool" />
  129. <argument index="0" name="right" type="bool" />
  130. <description>
  131. Returns [code]true[/code] if the left operand is [code]false[/code] and the right operand is [code]true[/code].
  132. </description>
  133. </operator>
  134. <operator name="operator ==">
  135. <return type="bool" />
  136. <description>
  137. Returns [code]true[/code] if two bools are equal, i.e. both are [code]true[/code] or both are [code]false[/code].
  138. </description>
  139. </operator>
  140. <operator name="operator ==">
  141. <return type="bool" />
  142. <argument index="0" name="right" type="bool" />
  143. <description>
  144. Returns [code]true[/code] if two bools are equal, i.e. both are [code]true[/code] or both are [code]false[/code].
  145. </description>
  146. </operator>
  147. <operator name="operator &gt;">
  148. <return type="bool" />
  149. <argument index="0" name="right" type="bool" />
  150. <description>
  151. Returns [code]true[/code] if the left operand is [code]true[/code] and the right operand is [code]false[/code].
  152. </description>
  153. </operator>
  154. </operators>
  155. </class>