int

A built-in type for integers.

Description

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.

GDScriptC#

  1. var x: int = 1 # x is 1
  2. x = 4.2 # x is 4, because 4.2 gets truncated
  3. var max_int = 9223372036854775807 # Biggest value an int can store
  4. max_int += 1 # max_int is -9223372036854775808, because it wrapped around
  1. int x = 1; // x is 1
  2. x = (int)4.2; // x is 4, because 4.2 gets truncated
  3. // We use long below, because GDScript's int is 64-bit while C#'s int is 32-bit.
  4. long maxLong = 9223372036854775807; // Biggest value a long can store
  5. maxLong++; // maxLong is now -9223372036854775808, because it wrapped around.
  6. // Alternatively with C#'s 32-bit int type, which has a smaller maximum value.
  7. int maxInt = 2147483647; // Biggest value an int can store
  8. 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.

GDScriptC#

  1. var x = 0b1001 # x is 9
  2. var y = 0xF5 # y is 245
  3. var z = 10_000_000 # z is 10000000
  1. int x = 0b1001; // x is 9
  2. int y = 0xF5; // y is 245
  3. int z = 10_000_000; // z is 10000000

Constructors

int

int ( )

int

int ( int from )

int

int ( String from )

int

int ( bool from )

int

int ( float from )

Operators

bool

operator != ( float right )

bool

operator != ( int right )

int

operator % ( int right )

int

operator & ( int right )

Color

operator ( Color right )

Quaternion

operator ( Quaternion right )

Vector2

operator ( Vector2 right )

Vector2i

operator ( Vector2i right )

Vector3

operator ( Vector3 right )

Vector3i

operator ( Vector3i right )

Vector4

operator ( Vector4 right )

Vector4i

operator ( Vector4i right )

float

operator ( float right )

int

operator ( int right )

float

operator ( float right )

int

operator ( int right )

float

operator + ( float right )

int

operator + ( int right )

float

operator - ( float right )

int

operator - ( int right )

float

operator / ( float right )

int

operator / ( int right )

bool

operator < ( float right )

bool

operator < ( int right )

int

operator << ( int right )

bool

operator <= ( float right )

bool

operator <= ( int right )

bool

operator == ( float right )

bool

operator == ( int right )

bool

operator > ( float right )

bool

operator > ( int right )

bool

operator >= ( float right )

bool

operator >= ( int right )

int

operator >> ( int right )

int

operator ^ ( int right )

int

operator unary+ ( )

int

operator unary- ( )

int

operator | ( int right )

int

operator ~ ( )


Constructor Descriptions

int int ( )

Constructs an int set to 0.


int int ( int from )

Constructs an int as a copy of the given int.


int int ( String from )

Constructs a new int from a String, following the same rules as String.to_int.


int int ( bool from )

Constructs a new int from a bool. true is converted to 1 and false is converted to 0.


int int ( float from )

Constructs a new int from a float. This will truncate the float, discarding anything after the floating point.


Operator Descriptions

bool operator != ( float right )

Returns true if the int is not equivalent to the float.


bool operator != ( int right )

Returns true if the ints are not equal.


int operator % ( int right )

Returns the remainder after dividing two ints. Uses truncated division, which returns a negative number if the dividend is negative. If this is not desired, consider using @GlobalScope.posmod.

  1. print(6 % 2) # Prints 0
  2. print(11 % 4) # Prints 3
  3. print(-5 % 3) # Prints -2

int operator & ( int right )

Performs the bitwise AND operation.

  1. print(0b1100 & 0b1010) # Prints 8 (binary 1000)

This is useful for retrieving binary flags from a variable.

  1. var flags = 0b101
  2. # Check if the first or second bit are enabled.
  3. if flags & 0b011:
  4. do_stuff() # This line will run.

Color operator * ( Color right )

Multiplies each component of the Color by the int.


Quaternion operator * ( Quaternion right )

Multiplies each component of the Quaternion by the 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 int.

  1. print(2 * Vector2(1, 4)) # Prints (2, 8)

Vector2i operator * ( Vector2i right )

Multiplies each component of the Vector2i by the int.


Vector3 operator * ( Vector3 right )

Multiplies each component of the Vector3 by the int.


Vector3i operator * ( Vector3i right )

Multiplies each component of the Vector3i by the int.


Vector4 operator * ( Vector4 right )

Multiplies each component of the Vector4 by the int.


Vector4i operator * ( Vector4i right )

Multiplies each component of the Vector4i by the int.


float operator * ( float right )

Multiplies the float by the int. The result is a float.


int operator * ( int right )

Multiplies the two ints.


float operator ** ( float right )

Raises an int to a power of a float. The result is a float.

  1. print(2 ** 0.5) # Prints 1.4142135623731

int operator ** ( int right )

Raises the left int to a power of the right int.

  1. print(3 ** 4) # Prints 81

float operator + ( float right )

Adds the int and the float. The result is a float.


int operator + ( int right )

Adds the two ints.


float operator - ( float right )

Subtracts the float from the int. The result is a float.


int operator - ( int right )

Subtracts the two ints.


float operator / ( float right )

Divides the int by the float. The result is a float.

  1. print(10 / 3.0) # Prints 3.33333333333333

int operator / ( int right )

Divides the two ints. The result is an int. This will truncate the float, discarding anything after the floating point.

  1. print(6 / 2) # Prints 3
  2. print(5 / 3) # Prints 1

bool operator < ( float right )

Returns true if the int is less than the float.


bool operator < ( int right )

Returns true if the left int is less than the right int.


int operator << ( int right )

Performs the bitwise shift left operation. Effectively the same as multiplying by a power of 2.

  1. print(0b1010 << 1) # Prints 20 (binary 10100)
  2. print(0b1010 << 3) # Prints 80 (binary 1010000)

bool operator <= ( float right )

Returns true if the int is less than or equal to the float.


bool operator <= ( int right )

Returns true if the left int is less than or equal to the right int.


bool operator == ( float right )

Returns true if the int is equal to the float.


bool operator == ( int right )

Returns true if the two ints are equal.


bool operator > ( float right )

Returns true if the int is greater than the float.


bool operator > ( int right )

Returns true if the left int is greater than the right int.


bool operator >= ( float right )

Returns true if the int is greater than or equal to the float.


bool operator >= ( int right )

Returns true if the left int is greater than or equal to the right int.


int operator >> ( int right )

Performs the bitwise shift right operation. Effectively the same as dividing by a power of 2.

  1. print(0b1010 >> 1) # Prints 5 (binary 101)
  2. print(0b1010 >> 2) # Prints 2 (binary 10)

int operator ^ ( int right )

Performs the bitwise XOR operation.

  1. print(0b1100 ^ 0b1010) # Prints 6 (binary 110)

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.


int operator | ( int right )

Performs the bitwise OR operation.

  1. print(0b1100 | 0b1010) # Prints 14 (binary 1110)

This is useful for storing binary flags in a variable.

  1. var flags = 0
  2. flags |= 0b101 # Turn the first and third bits on.

int operator ~ ( )

Performs the bitwise NOT operation on the int. Due to 2’s complement, it’s effectively equal to -(int + 1).

  1. print(~4) # Prints -5
  2. print(~(-7)) # Prints 6

Previous Next


© Copyright 2014-present Juan Linietsky, Ariel Manzur and the Godot community (CC BY 3.0). Revision 53e837c6.

Built with Sphinx using a theme provided by Read the Docs.