bool

A built-in boolean type.

Description

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:

GDScriptC#

  1. var can_shoot = true
  2. if can_shoot:
  3. launch_bullet()
  1. bool canShoot = true;
  2. if (canShoot)
  3. {
  4. LaunchBullet();
  5. }

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:

GDScriptC#

  1. if bullets > 0 and not is_reloading():
  2. launch_bullet()
  3. if bullets == 0 or is_reloading():
  4. play_clack_sound()
  1. if (bullets > 0 && !IsReloading())
  2. {
  3. LaunchBullet();
  4. }
  5. if (bullets == 0 || IsReloading())
  6. {
  7. PlayClackSound();
  8. }

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 (String.is_empty, Node.can_process, Camera2D.enabled, etc.).

Constructors

bool

bool ( )

bool

bool ( bool from )

bool

bool ( float from )

bool

bool ( int from )

Operators

bool

operator != ( bool right )

bool

operator < ( bool right )

bool

operator == ( bool right )

bool

operator > ( bool right )


Constructor Descriptions

bool bool ( )

Constructs a bool set to false.


bool bool ( bool from )

Constructs a bool as a copy of the given bool.


bool bool ( float from )

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 @GDScript.INF and @GDScript.NAN).


bool bool ( int from )

Cast an int value to a boolean value. Returns false if from is equal to 0, and true for all other values.


Operator Descriptions

bool operator != ( bool right )

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 operator < ( bool right )

Returns true if the left operand is false and the right operand is true.


bool operator == ( bool right )

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 operator > ( bool right )

Returns true if the left operand is true and the right operand is false.

Previous Next


© Copyright 2014-present Juan Linietsky, Ariel Manzur and the Godot community (CC BY 3.0). Revision 53e837c6.

Built with Sphinx using a theme provided by Read the Docs.