bool
A built-in boolean type.
Description
Thebool is a built-inVariant type that may only store one of two values:true
orfalse
. 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 inif
, and other conditional statements:
varcan_shoot=trueifcan_shoot:launch_bullet()
boolcanShoot=true;if(canShoot){LaunchBullet();}
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 operatorsand
,or
,not
to create complex conditions:
ifbullets>0andnotis_reloading():launch_bullet()ifbullets==0oris_reloading():play_clack_sound()
if(bullets>0&&!IsReloading()){LaunchBullet();}if(bullets==0||IsReloading()){PlayClackSound();}
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 asshort-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 (String.is_empty(),Node.can_process(),Camera2D.enabled, etc.).
Constructors
bool() | |
Operators
operator !=(right:bool) | |
operator <(right:bool) | |
operator ==(right:bool) | |
operator >(right:bool) |
Constructor Descriptions
Constructs abool set tofalse
.
Constructs abool as a copy of the givenbool.
Cast afloat value to a boolean value. Returnsfalse
iffrom
is equal to0.0
(including-0.0
), andtrue
for all other values (including@GDScript.INF and@GDScript.NAN).
Cast anint value to a boolean value. Returnsfalse
iffrom
is equal to0
, andtrue
for all other values.
Operator Descriptions
Returnstrue
if the two booleans are not equal. That is, one istrue
and the other isfalse
. This operation can be seen as a logical XOR.
Returnstrue
if the left operand isfalse
and the right operand istrue
.
Returnstrue
if the two booleans are equal. That is, both aretrue
or both arefalse
. This operation can be seen as a logical EQ or XNOR.
Returnstrue
if the left operand istrue
and the right operand isfalse
.