Every value in GDScript has a type. These are the ones you use every day:
3 250.5 true Level 1
(64.0, 128.0) (1.0, 0.5, 0.0, 1.0)
["key", "map"] { "hp": 10, "mp": 4 } <null>
Numbers: int and float
int is a 64-bit whole number. float is a 64-bit decimal number. A number with a dot is a float, even 3.0:
3
3.5
1
1024
1.5
1000000 255 5 1000.0
Integer division is the classic surprise. When you want a fraction, write one side as a float. Underscores in a number are only for reading, and 0x and 0b write hex and binary.
Floats are not exact. Compare them with is_equal_approx() rather than ==:
false
true
bool
bool is true or false. Logic uses words (and, or, not), and the symbols &&, || and ! also work. Many values count as false in an if: 0, 0.0, "", an empty array or dictionary, and null.
String and StringName
String holds text. StringName is a text value Godot stores once and compares fast, written &"idle". Node names, animation names, input actions and signal names are StringNames, and they compare equal to the same String. The strings page covers text methods.
Vectors, Colors and Other Engine Types
GDScript has game math types built in, with no imports:
| Type | Holds | Example |
|---|---|---|
Vector2, Vector3 | a position or direction, as floats | Vector2(10, 20) |
Vector2i, Vector3i | the same, as ints (tile grids) | Vector2i(3, 4) |
Color | red, green, blue, alpha from 0 to 1 | Color(1, 0, 0), Color.RED |
Rect2 | a rectangle: position and size | Rect2(0, 0, 32, 32) |
Transform2D | position, rotation and scale together | Transform2D() |
See Vector2 for the vector math a 2D game needs.
Collections and Objects
Array: an ordered list. See arrays.Dictionary: key and value pairs. See dictionaries.- Packed arrays such as
PackedInt32ArrayandPackedVector2Array: compact lists of one type. See packed arrays. Object: every node and resource. Objects are passed by reference; the other types above are copied, except arrays and dictionaries, which are shared too.null: no value. See null and is_instance_valid.
Checking a Type
typeof() returns a number from the TYPE_* constants, and type_string() turns it into a name. is checks a type directly and also works for classes:
int
float
String
bool
Vector2
Array
Nil
Object
true
true
Converting Between Types
43
7.0
3
4.0
10 coins
0
false
false true
int() on text that is not a number gives 0 without an error, so validate input first with is_valid_int() or is_valid_float().
Frequently Asked Questions
How do I convert a string to an int in Godot?
Use int("42") or "42".to_int(). Both give 0 when the text is not a number, so check first with "42".is_valid_int(). For decimals use float("3.5") or to_float().
How do I convert an int to a string in GDScript?
Use str(42). You need it when joining with +, because "Score: " + 42 is an error. print() and % formatting convert for you, so print("Score: ", 42) works without str().
Why does 7 / 2 give 3 in GDScript?
Dividing two int values gives an int and drops the remainder. Make one side a float to get 3.5: 7 / 2.0 or float(7) / 2. The editor shows an INTEGER_DIVISION warning to point this out.
What is a Variant in Godot?
Variant is the type that can hold any value. An untyped GDScript variable is a Variant: it can hold an int now and a string later. typeof() tells you which type a Variant holds right now.