0.2.0-dev Download ZIP
123 lines ·4.9 KB Raw
| 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 | A built-in boolean type. |
| 5 | </brief_description> |
| 6 | <description> |
| 7 | The [bool] is a built-in [Variant] type that may only store one of two values: [code]true[/code] or [code]false[/code]. You can imagine it as a switch that can be either turned on or off, or as a binary digit that can either be 1 or 0. |
| 8 | Booleans can be directly used in [code]if[/code], and other conditional statements: |
| 9 | [codeblocks] |
| 10 | [gdscript] |
| 11 | var can_shoot = true |
| 12 | if can_shoot: |
| 13 | launch_bullet() |
| 14 | [/gdscript] |
| 15 | [csharp] |
| 16 | bool canShoot = true; |
| 17 | if (canShoot) |
| 18 | { |
| 19 | LaunchBullet(); |
| 20 | } |
| 21 | [/csharp] |
| 22 | [/codeblocks] |
| 23 | All comparison operators return booleans ([code]==[/code], [code]>[/code], [code]<=[/code], etc.). As such, it is not necessary to compare booleans themselves. You do not need to add [code]== true[/code] or [code]== false[/code]. |
| 24 | Booleans can be combined with the logical operators [code]and[/code], [code]or[/code], [code]not[/code] to create complex conditions: |
| 25 | [codeblocks] |
| 26 | [gdscript] |
| 27 | if bullets > 0 and not is_reloading(): |
| 28 | launch_bullet() |
| 29 | |
| 30 | if bullets == 0 or is_reloading(): |
| 31 | play_clack_sound() |
| 32 | [/gdscript] |
| 33 | [csharp] |
| 34 | if (bullets > 0 && !IsReloading()) |
| 35 | { |
| 36 | LaunchBullet(); |
| 37 | } |
| 38 | |
| 39 | if (bullets == 0 || IsReloading()) |
| 40 | { |
| 41 | PlayClackSound(); |
| 42 | } |
| 43 | [/csharp] |
| 44 | [/codeblocks] |
| 45 | [b]Note:[/b] In modern programming languages, logical operators are evaluated in order. All remaining conditions are skipped if their result would have no effect on the final value. This concept is known as [url=https://en.wikipedia.org/wiki/Short-circuit_evaluation]short-circuit evaluation[/url] and can be useful to avoid evaluating expensive conditions in some performance-critical cases. |
| 46 | [b]Note:[/b] By convention, built-in methods and properties that return booleans are usually defined as yes-no questions, single adjectives, or similar ([method String.is_empty], [method Node.can_process], [member Camera2D.enabled], etc.). |
| 47 | </description> |
| 48 | <tutorials> |
| 49 | </tutorials> |
| 50 | <constructors> |
| 51 | <constructor name="bool"> |
| 52 | <return type="bool" /> |
| 53 | <description> |
| 54 | Constructs a [bool] set to [code]false[/code]. |
| 55 | </description> |
| 56 | </constructor> |
| 57 | <constructor name="bool"> |
| 58 | <return type="bool" /> |
| 59 | <param index="0" name="from" type="bool" /> |
| 60 | <description> |
| 61 | Constructs a [bool] as a copy of the given [bool]. |
| 62 | </description> |
| 63 | </constructor> |
| 64 | <constructor name="bool"> |
| 65 | <return type="bool" /> |
| 66 | <param index="0" name="from" type="float" /> |
| 67 | <description> |
| 68 | Casts a [float] value to a [bool]. Returns [code]false[/code] if [param from] is equal to [code]0.0[/code] (including [code]-0.0[/code]), and [code]true[/code] for all other values (including [constant @GDScript.INF] and [constant @GDScript.NAN]). |
| 69 | </description> |
| 70 | </constructor> |
| 71 | <constructor name="bool"> |
| 72 | <return type="bool" /> |
| 73 | <param index="0" name="from" type="int" /> |
| 74 | <description> |
| 75 | Casts an [int] value to a [bool]. Returns [code]false[/code] if [param from] is equal to [code]0[/code], and [code]true[/code] for all other values. |
| 76 | </description> |
| 77 | </constructor> |
| 78 | </constructors> |
| 79 | <operators> |
| 80 | <operator name="operator !="> |
| 81 | <return type="bool" /> |
| 82 | <param index="0" name="right" type="bool" /> |
| 83 | <description> |
| 84 | Returns [code]true[/code] if one [bool] is [code]true[/code] and the other [bool] is [code]false[/code]. Equivalent to logical XOR (NEQ). |
| 85 | </description> |
| 86 | </operator> |
| 87 | <operator name="operator <"> |
| 88 | <return type="bool" /> |
| 89 | <param index="0" name="right" type="bool" /> |
| 90 | <description> |
| 91 | Returns [code]true[/code] if the left [bool] is [code]false[/code] and [param right] is [code]true[/code]. |
| 92 | </description> |
| 93 | </operator> |
| 94 | <operator name="operator <="> |
| 95 | <return type="bool" /> |
| 96 | <param index="0" name="right" type="bool" /> |
| 97 | <description> |
| 98 | Returns [code]true[/code] if the left [bool] is [code]false[/code], or if both [bool]s are [code]true[/code]. |
| 99 | </description> |
| 100 | </operator> |
| 101 | <operator name="operator =="> |
| 102 | <return type="bool" /> |
| 103 | <param index="0" name="right" type="bool" /> |
| 104 | <description> |
| 105 | Returns [code]true[/code] if both [bool]s are [code]true[/code], or if both [bool]s are [code]false[/code]. Equivalent to logical XNOR (EQ). |
| 106 | </description> |
| 107 | </operator> |
| 108 | <operator name="operator >"> |
| 109 | <return type="bool" /> |
| 110 | <param index="0" name="right" type="bool" /> |
| 111 | <description> |
| 112 | Returns [code]true[/code] if the left [bool] is [code]true[/code] and [param right] is [code]false[/code]. |
| 113 | </description> |
| 114 | </operator> |
| 115 | <operator name="operator >="> |
| 116 | <return type="bool" /> |
| 117 | <param index="0" name="right" type="bool" /> |
| 118 | <description> |
| 119 | Returns [code]true[/code] if the left [bool] is [code]true[/code], or if both [bool]s are [code]false[/code]. |
| 120 | </description> |
| 121 | </operator> |
| 122 | </operators> |
| 123 | </class> |