bool.xml 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <class name="bool" version="3.4">
  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. [codeblock]
  10. var can_shoot = true
  11. func shoot():
  12. if can_shoot:
  13. pass # Perform shooting actions here.
  14. [/codeblock]
  15. 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].
  16. [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.
  17. [codeblock]
  18. var can_shoot = true
  19. func shoot():
  20. if can_shoot and Input.is_action_pressed("shoot"):
  21. create_bullet()
  22. [/codeblock]
  23. 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.
  24. [codeblock]
  25. var can_shoot = true
  26. onready var cool_down = $CoolDownTimer
  27. func shoot():
  28. if can_shoot and Input.is_action_pressed("shoot"):
  29. create_bullet()
  30. can_shoot = false
  31. cool_down.start()
  32. func _on_CoolDownTimer_timeout():
  33. can_shoot = true
  34. [/codeblock]
  35. </description>
  36. <tutorials>
  37. </tutorials>
  38. <methods>
  39. <method name="bool">
  40. <return type="bool">
  41. </return>
  42. <argument index="0" name="from" type="int">
  43. </argument>
  44. <description>
  45. 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.
  46. </description>
  47. </method>
  48. <method name="bool">
  49. <return type="bool">
  50. </return>
  51. <argument index="0" name="from" type="float">
  52. </argument>
  53. <description>
  54. 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.
  55. </description>
  56. </method>
  57. <method name="bool">
  58. <return type="bool">
  59. </return>
  60. <argument index="0" name="from" type="String">
  61. </argument>
  62. <description>
  63. Cast a [String] value to a boolean value, this method will return [code]false[/code] if [code]""[/code] is passed in, and [code]true[/code] for all non-empty strings.
  64. Examples: [code]bool("False")[/code] returns [code]true[/code], [code]bool("")[/code] returns [code]false[/code].
  65. </description>
  66. </method>
  67. </methods>
  68. <constants>
  69. </constants>
  70. </class>