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...
int
整數內建型別。
說明
Signed 64-bit integer type. This means that it can take values from -2^63 to 2^63 - 1, i.e. from -9223372036854775808 to 9223372036854775807. When it exceeds these bounds, it will wrap around.
ints can be automatically converted to floats when necessary, for example when passing them as arguments in functions. The float will be as close to the original integer as possible.
Likewise, floats can be automatically converted into ints. This will truncate the float, discarding anything after the floating-point.
Note: In a boolean context, an int will evaluate to false if it equals 0, and to true otherwise.
var x: int = 1 # x is 1
x = 4.2 # x is 4, because 4.2 gets truncated
var max_int = 9223372036854775807 # Biggest value an int can store
max_int += 1 # max_int is -9223372036854775808, because it wrapped around
int x = 1; // x is 1
x = (int)4.2; // x is 4, because 4.2 gets truncated
// We use long below, because GDScript's int is 64-bit while C#'s int is 32-bit.
long maxLong = 9223372036854775807; // Biggest value a long can store
maxLong++; // maxLong is now -9223372036854775808, because it wrapped around.
// Alternatively with C#'s 32-bit int type, which has a smaller maximum value.
int maxInt = 2147483647; // Biggest value an int can store
maxInt++; // maxInt is now -2147483648, because it wrapped around
You can use the 0b literal for binary representation, the 0x literal for hexadecimal representation, and the _ symbol to separate long numbers and improve readability.
var x = 0b1001 # x is 9
var y = 0xF5 # y is 245
var z = 10_000_000 # z is 10000000
int x = 0b1001; // x is 9
int y = 0xF5; // y is 245
int z = 10_000_000; // z is 10000000
建構子
int() |
|
運算子
operator !=(right: float) |
|
operator !=(right: int) |
|
operator %(right: int) |
|
operator &(right: int) |
|
operator *(right: Color) |
|
operator *(right: Quaternion) |
|
operator *(right: Vector2) |
|
operator *(right: Vector2i) |
|
operator *(right: Vector3) |
|
operator *(right: Vector3i) |
|
operator *(right: Vector4) |
|
operator *(right: Vector4i) |
|
operator *(right: float) |
|
operator *(right: int) |
|
operator **(right: float) |
|
operator **(right: int) |
|
operator +(right: float) |
|
operator +(right: int) |
|
operator -(right: float) |
|
operator -(right: int) |
|
operator /(right: float) |
|
operator /(right: int) |
|
operator <(right: float) |
|
operator <(right: int) |
|
operator <<(right: int) |
|
operator <=(right: float) |
|
operator <=(right: int) |
|
operator ==(right: float) |
|
operator ==(right: int) |
|
operator >(right: float) |
|
operator >(right: int) |
|
operator >=(right: float) |
|
operator >=(right: int) |
|
operator >>(right: int) |
|
operator ^(right: int) |
|
operator |(right: int) |
|
建構子說明
建構設為 0 的 int。
建構給定 int 的副本 int。
從 String 建構新的 int,遵循與 String.to_int() 相同的規則。
從 bool 建構新的 int。true 會轉換為 1,false 會轉換為 0。
運算子說明
bool operator !=(right: float) 🔗
如果該 int 與該 float 不等價,則返回 true。
bool operator !=(right: int) 🔗
如果 int 不相等,則返回 true。
返回兩個 int 相除後的餘數。該操作使用截斷除法,被除數為負數時會返回負數。如果不希望如此,請考慮使用 @GlobalScope.posmod()。
print(6 % 2) # 輸出 0
print(11 % 4) # 輸出 3
print(-5 % 3) # 輸出 -2
執行按位 AND 運算。
print(0b1100 & 0b1010) # 輸出 8(二進位 1000)
可用於從變數中檢索二進位旗標。
var flags = 0b101
# 檢查是否啟用了第一或第二個比特位。
if flags & 0b011:
do_stuff() # 會執行這一行。
Color operator *(right: Color) 🔗
將 Color 的每個分量乘以該 int。
Quaternion operator *(right: Quaternion) 🔗
將 Quaternion 的每個分量乘以該 int。此操作本身沒有意義,但可以用作更大表達式的一部分。
Vector2 operator *(right: Vector2) 🔗
print(2 * Vector2(1, 4)) # 輸出 (2, 8)
Vector2i operator *(right: Vector2i) 🔗
將 Vector2i 的每個分量乘以該 int。
Vector3 operator *(right: Vector3) 🔗
將 Vector3 的每個分量乘以該 int。
Vector3i operator *(right: Vector3i) 🔗
將 Vector3i 的每個分量乘以該 int。
Vector4 operator *(right: Vector4) 🔗
將 Vector4 的每個分量乘以該 int。
Vector4i operator *(right: Vector4i) 🔗
將 Vector4i 的每個分量乘以該 int。
float operator *(right: float) 🔗
將兩個 int 相乘。
float operator **(right: float) 🔗
print(2 ** 0.5) # 輸出 1.4142135623731
將左側的 int 提升到右側的 int 次冪。
print(3 ** 4) # 輸出 81
float operator +(right: float) 🔗
將兩個 int 相加。
float operator -(right: float) 🔗
將兩個 int 相減。
float operator /(right: float) 🔗
print(10 / 3.0) # 輸出 3.33333333333333
將兩個 int 相除。結果為 int。這樣會截斷該 float,丟棄小數點後的部分。
print(6 / 2) # 輸出 3
print(5 / 3) # 輸出 1
bool operator <(right: float) 🔗
如果該 int 小於該 float,則返回 true。
如果左側的 int 小於右側的 int,則返回 true。
執行按位左移操作。效果上與乘以 2 的冪相同。
print(0b1010 << 1) # 輸出 20(二進位 10100)
print(0b1010 << 3) # 輸出 80(二進位 1010000)
bool operator <=(right: float) 🔗
如果該 int 小於等於該 float,則返回 true。
bool operator <=(right: int) 🔗
如果左側的 int 小於等於右側的 int,則返回 true。
bool operator ==(right: float) 🔗
如果該 int 等於該 float,則返回 true。
bool operator ==(right: int) 🔗
如果兩個 int 相等,則返回 true。
bool operator >(right: float) 🔗
如果該 int 大於該 float,則返回 true。
如果左側的 int 大於右側的 int,則返回 true。
bool operator >=(right: float) 🔗
如果該 int 大於等於該 float,則返回 true。
bool operator >=(right: int) 🔗
如果左側的 int 大於等於右側的 int,則返回 true。
執行按位右移操作。效果上與除以 2 的冪相同。
print(0b1010 >> 1) # 輸出 5(二進位 101)
print(0b1010 >> 2) # 輸出 2(二進位 10)
執行按位 XOR(異或)運算。
print(0b1100 ^ 0b1010) # 輸出 6(二進位 110)
返回與 + 不存在時相同的值。單目 + 沒有作用,但有時可以使你的程式碼更具可讀性。
返回該 int 的相反值。如果為正數,則該將數變為負數。如果為負數,則將該數變為正數。如果為零,則不執行任何操作。
執行按位 OR(或)運算。
print(0b1100 | 0b1010) # 輸出 14(二進位 1110)
可用於在變數中儲存二進位標記。
var flags = 0
flags |= 0b101 # 置第一和第三位。
執行按位 NOT(反)運算。由於補數,效果上與 -(int + 1) 相同。
print(~4) # 輸出 -5
print(~(-7)) # 輸出 6