Up to date
This page is up to date for Godot 4.0
.
If you still find outdated information, please open an issue.
int¶
Integer built-in type.
Description¶
Signed 64-bit integer type.
It can take values in the interval [-2^63, 2^63 - 1]
, i.e. [-9223372036854775808, 9223372036854775807]
. Exceeding those bounds will wrap around.
int is a Variant type, and will thus be used when assigning an integer value to a Variant. It can also be enforced with the : int
type hint.
var my_variant = 0 # int, value 0.
my_variant += 4.2 # float, value 4.2.
var my_int: int = 1 # int, value 1.
my_int = 4.2 # int, value 4, the right value is implicitly cast to int.
my_int = int("6.7") # int, value 6, the String is explicitly cast with int.
var max_int = 9223372036854775807
print(max_int) # 9223372036854775807, OK.
max_int += 1
print(max_int) # -9223372036854775808, we overflowed and wrapped around.
int myInt = (int)"6.7".ToFloat(); // int, value 6, the String is explicitly cast with int.
// We have to use `long` here, because GDSript's `int`
// is 64 bits long while C#'s `int` is only 32 bits.
long maxInt = 9223372036854775807;
GD.Print(maxInt); // 9223372036854775807, OK.
maxInt++;
GD.Print(maxInt); // -9223372036854775808, we overflowed and wrapped around.
// Alternatively, if we used C#'s 32-bit `int` type, the maximum value is much smaller:
int halfInt = 2147483647;
GD.Print(halfInt); // 2147483647, OK.
halfInt++;
GD.Print(halfInt); // -2147483648, we overflowed and wrapped around.
Constructors¶
int ( ) |
|
Operators¶
Constructor Descriptions¶
int int ( )
Constructs a default-initialized int set to 0
.
Constructs an int as a copy of the given int.
Converts a String to an int, following the same rules as String.to_int.
Cast a bool value to an integer value, int(true)
will be equals to 1 and int(false)
will be equals to 0.
Cast a float value to an integer value, this method simply removes the number fractions (i.e. rounds from
towards zero), so for example int(2.7)
will be equals to 2, int(0.1)
will be equals to 0 and int(-2.7)
will be equals to -2. This operation is also called truncation.
Operator Descriptions¶
bool operator != ( float right )
Returns true
if this int is not equivalent to the given float.
bool operator != ( int right )
Returns true
if the integers are not equal.
Returns the remainder after dividing two integers. This operation uses truncated division, which is often not desired as it does not work well with negative numbers. Consider using @GlobalScope.posmod instead if you want to handle negative numbers.
print(5 % 2) # 1
print(12 % 4) # 0
print(-5 % 3) # -2
Returns the result of bitwise AND
operation for two integers.
print(3 & 1) # 1
print(11 & 3) # 3
It's useful to retrieve binary flags from a variable.
var flags = 5
# Do something if the first bit is enabled.
if flags & 1:
do_stuff()
Color operator * ( Color right )
Multiplies each component of the Color by the given int.
Quaternion operator * ( Quaternion right )
Multiplies each component of the Quaternion by the given int. This operation is not meaningful on its own, but it can be used as a part of a larger expression.
Vector2 operator * ( Vector2 right )
Multiplies each component of the Vector2 by the given int.
print(2 * Vector2(1, 1)) # Vector2(2, 2)
Vector2i operator * ( Vector2i right )
Multiplies each component of the Vector2i by the given int.
Vector3 operator * ( Vector3 right )
Multiplies each component of the Vector3 by the given int.
Vector3i operator * ( Vector3i right )
Multiplies each component of the Vector3i by the given int.
Vector4 operator * ( Vector4 right )
Multiplies each component of the Vector4 by the given int.
Vector4i operator * ( Vector4i right )
Multiplies each component of the Vector4i by the given int.
float operator * ( float right )
Multiplies an int and a float. The result is a float.
Multiplies two ints.
float operator ** ( float right )
Raises an int to a power of a float. The result is a float.
print(8**0.25) # 1.68179283050743
Raises an int to a power of a int.
print(5**5) # 3125
float operator + ( float right )
Adds an int and a float. The result is a float.
Adds two integers.
float operator - ( float right )
Subtracts a float from an int. The result is a float.
Subtracts two integers.
float operator / ( float right )
Divides an int by a float. The result is a float.
print(10 / 3.0) # 3.333...
Divides two integers. The decimal part of the result is discarded (truncated).
print(10 / 2) # 5
print(10 / 3) # 3
bool operator < ( float right )
Returns true
if this int is less than the given float.
Returns true
if the left integer is less than the right one.
Performs bitwise shift left operation on the integer. Effectively the same as multiplying by a power of 2.
print(10 << 1) # 20
print(10 << 4) # 160
bool operator <= ( float right )
Returns true
if this int is less than or equal to the given float.
bool operator <= ( int right )
Returns true
if the left integer is less than or equal to the right one.
bool operator == ( float right )
Returns true
if the integer is equal to the given float.
bool operator == ( int right )
Returns true
if both integers are equal.
bool operator > ( float right )
Returns true
if this int is greater than the given float.
Returns true
if the left integer is greater than the right one.
bool operator >= ( float right )
Returns true
if this int is greater than or equal to the given float.
bool operator >= ( int right )
Returns true
if the left integer is greater than or equal to the right one.
Performs bitwise shift right operation on the integer. Effectively the same as dividing by a power of 2.
print(10 >> 1) # 5
print(10 >> 2) # 2
Returns the result of bitwise XOR
operation for two integers.
print(5 ^ 1) # 4
print(4 ^ 7) # 3
int operator unary+ ( )
Returns the same value as if the +
was not there. Unary +
does nothing, but sometimes it can make your code more readable.
int operator unary- ( )
Returns the negated value of the int. If positive, turns the number negative. If negative, turns the number positive. If zero, does nothing.
Returns the result of bitwise OR
operation for two integers.
print(2 | 4) # 6
print(1 | 3) # 3
It's useful to store binary flags in a variable.
var flags = 0
# Turn first and third bit on.
flags |= 1
flags |= 4
int operator ~ ( )
Returns the result of bitwise NOT
operation for the integer. It's effectively equal to -int + 1
.
print(~4) # -3
print(~7) # -6