bool

A built-in boolean type.

描述

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:


    var can_shoot = true
    if can_shoot:
        launch_bullet()

    bool canShoot = 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 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()

    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 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.).

构造函数

boolbool ( )
boolbool ( from: bool )
boolbool ( from: float )
boolbool ( from: int )

运算符

booloperator != ( right: bool )
booloperator < ( right: bool )
booloperator == ( right: bool )
booloperator > ( right: bool )

构造函数说明

bool bool ( )

Constructs a bool set to false.


bool bool ( from: bool )

Constructs a bool as a copy of the given bool.


bool bool ( from: 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 @GDScript.INF and @GDScript.NAN).


bool bool ( from: int )

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


运算符说明

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

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


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

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

1

本方法通常需要用户覆盖才能生效。

2

本方法无副作用,不会修改该实例的任何成员变量。

3

本方法除了能接受在此处描述的参数外,还能够继续接受任意数量的参数。

4

本方法用于构造某个类型。

5

调用本方法无需实例,可直接使用类名进行调用。

6

本方法描述的是使用本类型作为左操作数的有效运算符。

7

这个值是由下列位标志构成位掩码的整数。

8

无返回值。