Class bool
A built-in boolean type.
Remarks
The bool is a built-in Variant type that may only store one of two values: true
or false
. 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.
Booleans can be directly used in if
, and other conditional statements:
All comparison operators return booleans (==
, >
, <=
, etc.). As such, it is not necessary to compare booleans themselves. You do not need to add == true
or == false
.
Booleans can be combined with the logical operators and
, or
, not
to create complex conditions:
if bullets > 0 and not is_reloading():
launch_bullet()
if bullets == 0 or is_reloading():
play_clack_sound()
Note: 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 short-circuit evaluation and can be useful to avoid evaluating expensive conditions in some performance-critical cases.
Note: By convention, built-in methods and properties that return booleans are usually defined as yes-no questions, single adjectives, or similar (is_empty, can_process, enabled, etc.).
Constructors
bool
Constructs a bool set to false
.
bool bool
bool(bool)
Constructs a bool as a copy of the given bool.
bool bool(bool from)
Parameters
from
bool
bool(float)
Cast a float value to a boolean value. Returns false
if from
is equal to 0.0
(including -0.0
), and true
for all other values (including INF and NAN).
bool bool(float from)
Parameters
from
float
bool(int)
Cast an int value to a boolean value. Returns false
if from
is equal to 0
, and true
for all other values.
bool bool(int from)
Parameters
from
int
Operators
!= (bool)
Returns true
if the two booleans are not equal. That is, one is true
and the other is false
. This operation can be seen as a logical XOR.
bool != (bool right)
Parameters
right
bool
< (bool)
Returns true
if the left operand is false
and the right operand is true
.
bool < (bool right)
Parameters
right
bool
== (bool)
Returns true
if the two booleans are equal. That is, both are true
or both are false
. This operation can be seen as a logical EQ or XNOR.
bool == (bool right)
Parameters
right
bool
> (bool)
Returns true
if the left operand is true
and the right operand is false
.
bool > (bool right)
Parameters
right
bool