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.
Checking the stable version of the documentation...
bool
內建布林型別。
說明
bool 是內建 Variant 型別,只能儲存兩個值之一:true 或 false你可以把它想像成一個可以打開或關閉的開關,或者是一個可以是1或0的二進位數字。
布林值可以直接用在if和其他條件敘述:
var can_shoot = true
if can_shoot:
launch_bullet()
bool canShoot = true;
if (canShoot)
{
LaunchBullet();
}
所有比較運算子都傳回布林值(==、>、<= 等) 。因此,沒有必要比較布林值本身。您不需要新增 == true 或 == false。
布林值可以與邏輯運算子 and、or、not 組合來建立複雜的條件:
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();
}
注意: 在現代程式語言中,邏輯運算子依序求值。如果剩餘條件的結果對最終值沒有影響,則將跳過所有剩餘條件。這個概念被稱為短路評估,可用於避免在某些效能關鍵的情況下評估昂貴的條件。
注意: 依照慣例,傳回布林值的內建方法和屬性通常定義為是非問題、單一形容詞或類似的(String.is_empty()、Node.can_process()、 Camera2D.enabled等)。
建構子
bool() |
|
運算子
operator !=(right: bool) |
|
operator <(right: bool) |
|
operator ==(right: bool) |
|
operator >(right: bool) |
建構子說明
建構設為 0 的 int。
建構給定 bool 的副本。
將 float 值轉換為 bool。如果 from 等於 0.0(包括 -0.0)則回傳 false,其他值則回傳 true(包括 @GDScript.INF 和 @GDScript.NAN)。
將 int 值轉換為 bool。如果 from 等於 0 則回傳 false,其他值則回傳 true。
運算子說明
bool operator !=(right: bool) 🔗
如果一個 bool 為 true 且另一個 bool 為 false,則回傳 true。相當於邏輯異或(XOR (NEQ))。
bool operator <(right: bool) 🔗
如果左側 bool 為 false 且 right 為 true,則回傳 true。
bool operator ==(right: bool) 🔗
如果兩個 bool 都為 true,或兩個 bool 都為 false,則回傳 true。等效於邏輯同或(XNOR / EQ)運算。
bool operator >(right: bool) 🔗
如果左側 bool 為 true 且 right 為 false,則回傳 true。