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 值轉換為布林值。如果傳入 0.0,本方法將返回 false,傳入其他值則返回 true。
將 int 值轉換為布林值。如果傳入 0,本方法將返回 false,傳入其他值則返回 true。
運算子說明
bool operator !=(right: bool) 🔗
如果兩個布林值不同,即一個是 true,另一個是 false,則返回 true。
bool operator <(right: bool) 🔗
如果左運算元為 false 且右運算元為 true,則返回 true。
bool operator ==(right: bool) 🔗
如果兩個布林值相等,即都為 true 或都為 false,則返回 true。
bool operator >(right: bool) 🔗
如果左運算元為 true 且右運算元為 false,則返回 true。