bool.xml 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <class name="bool" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
  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. <argument index="0" name="from" type="int" />
  42. <description>
  43. 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.
  44. </description>
  45. </method>
  46. <method name="bool">
  47. <return type="bool" />
  48. <argument index="0" name="from" type="float" />
  49. <description>
  50. 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.
  51. </description>
  52. </method>
  53. <method name="bool">
  54. <return type="bool" />
  55. <argument index="0" name="from" type="String" />
  56. <description>
  57. 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.
  58. Examples: [code]bool("False")[/code] returns [code]true[/code], [code]bool("")[/code] returns [code]false[/code].
  59. </description>
  60. </method>
  61. </methods>
  62. <constants>
  63. </constants>
  64. </class>