Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

bool

內建布林型別。

說明

bool 是內建 Variant 型別,只能儲存兩個值之一:truefalse你可以把它想像成一個可以打開或關閉的開關,或者是一個可以是1或0的二進制數字。

布林值可以直接用在if和其他條件敘述:

var can_shoot = true
if can_shoot:
    launch_bullet()

所有比較運算子都傳回布林值(==><= 等) 。因此,沒有必要比較布林值本身。您不需要新增 == true== false

布林值可以與邏輯運算子 andornot 組合來建立複雜的條件:

if bullets > 0 and not is_reloading():
    launch_bullet()

if bullets == 0 or is_reloading():
    play_clack_sound()

注意: 在現代程式語言中,邏輯運算子依序求值。如果剩餘條件的結果對最終值沒有影響,則將跳過所有剩餘條件。這個概念被稱為短路評估,可用於避免在某些性能關鍵的情況下評估昂貴的條件。

注意: 依照慣例,傳回布林值的內建方法和屬性通常定義為是非問題、單一形容詞或類似的(String.is_empty()Node.can_process()Camera2D.enabled等)。

建構子

bool

bool()

bool

bool(from: bool)

bool

bool(from: float)

bool

bool(from: int)

運算子

bool

operator !=(right: bool)

bool

operator <(right: bool)

bool

operator ==(right: bool)

bool

operator >(right: bool)


建構子說明

bool bool() 🔗

建構設為 0int


bool bool(from: bool)

建構給定 bool 的副本。


bool bool(from: float)

Casts a float value to a bool. 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)

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


運算子說明

bool operator !=(right: bool) 🔗

Returns true if one bool is true and the other bool is false. Equivalent to logical XOR (NEQ).


bool operator <(right: bool) 🔗

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


bool operator ==(right: bool) 🔗

Returns true if both bools are true, or if both bools are false. Equivalent to logical XNOR (EQ).


bool operator >(right: bool) 🔗

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