Guide to the Godot game engine/Data types
null
[edit | edit source]Null is the absence of value. Null can be inferred as a function's return value with -> void
.
Never evaluates as true
in if statements:
if null: pass # This never runs
This is the default value used in a variable if it has not been set. If a function that you made is supposed to returns something, but instead return null
, be sure to check the output for errors, and double check the function is working properly.
bool
[edit | edit source]A bool is a basic data type which may only have 2 values: true
(positive, on or 1) and false
(negative, off or 0). bool may be inferred as a function's return value with -> bool
.
bools evaluate as true
in an if statements if it is equal to true
:
if true: pass # This runs
If converted to an integer (with int(bool)
), it becomes 0
if it's false
and 1
if true
.
History
[edit | edit source]Bools were invented by George Boole. George was an English mathematician whose work is the result of a special programming system. This system may only have 2 values, true and false. It was called "Boolean logic" in his honor.
See the official documentation for more: https://docs.godotengine.org/en/stable/classes/class_bool.html
int
[edit | edit source]An int, or integer, is a whole number. int may be inferred as a function's return value with -> int
.
ints evaluate as true
in an if statements if not equal to 0
:
if 1: pass # This runs
Operators
[edit | edit source]Ints are great for maths. They support the following operators:
Comparitive:
< Less than > More than == Equal to
Modifiers (use a number on both sides):
* Multiplication / Division + Addition - Subtraction % A type of divide. It sets a 64-bit integer to a limit of the second (randi()%5 is an integer 0 to 5) ^ To the power of
Dividing ints or floats by 0 is impossible! Attempting to do so will cause an error!
See maths is fun: dividing by zero.
Useful constants:
INF # Or "infinity". When printed, shows "1.#INF" NAN # Or "not a number". When printed, shows "1.#QNAN"
Speaking of NaN, NaN is not equal to NaN (NAN == NAN
is never true). Instead, use is_nan(variable)
to see if it is NaN.
To check for infinity (INF is equal to INF, but INF is not equal to -INF), use is_inf(variable)
.
Ints can be separated with underscores, like 36_234_785
(which makes 36234785) to be easier to read.
See also
[edit | edit source]See the official documentation for more: https://docs.godotengine.org/en/stable/classes/class_int.html
float
[edit | edit source]Floats, also known as floating-point numbers, are decimal numbers, like 1.7. float may be inferred as a function's return value with -> float
.
floats evaluate as true
in an if statements if not equal to 0.0
:
if .1: pass # This runs
Operators
[edit | edit source]Floats are great for maths. They support the following operators:
Comparative:
< Less than > More than == Equal to
Modifiers (use a number on both sides):
* Multiplication / Division + Addition - Subtraction
Useful constants:
PI # 3.141593
To check if two numbers are approximately equal (I.e: less than 0.00001 difference), use is_equal_approx(num1, num2)
.
Floats can be separated with underscores to be easier to read, like ints.
See also
[edit | edit source]See the official documentation for more: https://docs.godotengine.org/en/stable/classes/class_float.html
String
[edit | edit source]A versatile data type that can be any length. They are a string of characters. Hence the name "String". String may be inferred as a function's return value with -> String
.
Strings evaluate as true
in an if statements if not empty:
if "false": pass # This runs
See the official documentation for more: https://docs.godotengine.org/en/stable/classes/class_string.html
Array
[edit | edit source]An array is even more versatile than a String. It may be any length, and each value has a numerical index (of int). An Array may be inferred as a function's return value with -> Array
.
In an if statement Arrays evaluate as false
when empty:
if [false]: pass # This runs if []: pass # This does not run
Arrays are passed by reference rather than value. See Dictionary.
See the official documentation for more: https://docs.godotengine.org/en/stable/classes/class_array.html]
Dictionary
[edit | edit source]Dictionarys are the most versatile data type in Godot, besides Objects themselves. Dictionary may be inferred as a function's return value with -> Dictionary
.
Dictionarys evaluate as true
in an if statements if not empty:
if {"team 1": 0, "team 2": 0}: pass # This runs
Every value is indexed with a value. You can use any data type or Object you wish. As the value or key. FuncRefs and Nodes also count, but remember to keep that same FuncRef or Node (instead of using a different FuncRef for the same function). Even another Dictionary or Array can be used as a key or value.
Dictionaries are passed as reference rather than value, meaning this is never true:
return {"red": Color.red} == {"red": Color.red} # False
Although this is:
var dictionary={"red": Color.red} return dictionary == dictionary # True
This can be useful by saving the same Dictionary in multiple variables, then changing one of them changes them all. If you don't want this functionality, use dictionary.duplicate(true)
.
To compare them, use hash():
return hash({"red": Color.red}) == hash({"red": Color.red}) # True
Indexing
[edit | edit source]To get a value from a dictionary, you need to retrieve it with its index.
var points = {"Hannah": 5, "Paul": 8, "Tim": 3, "Bob": 0}
var username = "Bob"
func update_hud():
$Label.text = "Score: " + String(points[username])
So "Bob" has no points. So the label says "Score: 0".
See the official documentation for more: https://docs.godotengine.org/en/stable/classes/class_dictionary.html
Vector2
[edit | edit source]A vector is a value with multiple floats. Vector2 is used for 2D positioning among other things. They are indexed at the end with:
- ".x" or "[0]"
- ".y" or "[1]"
They are complex things, including many functions. Vector2 may be inferred as a function's return value with ->Vector2
.
Vector2s evaluate as true
in an if statements if at least one value is not equal to 0:
if Vector2(1, 0):
pass # This runs
See the official documentation for more: https://docs.godotengine.org/en/stable/classes/class_vector2.html
Vector3
[edit | edit source]A vector is a value with multiple floats. A Vector3 has 3 numbers and used for 3D positional and rotational data, among other things. They are indexed at the end with:
- ".x" or "[0]"
- ".y" or "[1]"
- ".z" or "[2]"
Vector3s are complex things, having many functions. Vector3 may be inferred as a function's return value with ->Vector3
.
Vector3s evaluate as true
in an if statements if at least one value is not equal to 0:
if Vector3(1, 0, 0):
pass # This runs
See the official documentation for more: https://docs.godotengine.org/en/stable/classes/class_vector3.html
Color
[edit | edit source]Technically considered a vector, it contains 4 values:
- ".r" - the red channel (E.g: 0 is no red, 1 is full red)
- ".g"- the green channel
- ".b" - the blue channel
- ".a" - alpha (or transparency)
Color may be inferred as a function's return value with ->Color
.
Colors evaluate as true
in an "if" statements if not black with no transparency (Color(0, 0, 0, 1)
is false):
if Color(1, 1, 1):
pass # This runs
If r
, g
and b
are 1, the color is white. If they are 0, the color is black. Know your color wheel! No color or all the colors!
Now, this can be irritating sometimes. How do you quickly get basic colors like orange and yellow? Or common shades of red, green and blue? Color.orange
is a simple way to do so.
All color constants:
aliceblue
|
antiquewhite
|
aqua
|
aquamarine
|
azure
|
beige
|
bisque
|
black
|
blanchedalmond | blue | blueviolet | brown |
burlywood | cadetblue | chartreuse | chocolate |
coral | cornflower | cornsilk | crimson |
cyan | darkblue | darkcyan | darkgoldenrod |
darkgray | darkgreen | darkkhaki | darkmagenta |
darkolivegreen | darkorange | darkorchid | darkred |
darksalmon | darkseagreen | darkslateblue | darkslategray |
darkturquoise | darkviolet | deeppink | deepskyblue |
dodgerblue | firebrick | florawhite | forestgreen |
fuchsia | gainsborow | ghostwhite | gold |
goldenrod | gray | green | greenyellow |
honeydew | hotpink | indianred | indigo |
ivory | khaki | lavender | lavenderblush |
lawngreen | lemonchiffon | lightblue | lightcoral |
lightcyan | lightgoldenrod | lightgray | lightgreen |
lightpink | lightsalmon | lightseagreen | lightskyblue |
lightslategray | lightsteelblue | lightyellow | lime |
limegreen | linen | magenta | maroon |
mediumaquamarine | mediumblue | mediumorchid | mediumpurple |
mediumseagreen | mediumslateblue | mediumspringgreen | mediumturquoise |
mediumvioletred | midnightblue | mintcream | mistyrose |
moccasin | navajowhite | navyblue | oldlace |
olive | olivedrab | orange | orangered |
orchid | palegoldenrod | palegreen | paleturquoise |
palevioletred | papayawhip | peachpuff | peru |
pink | plum | powderblue | purple |
rebeccapurple | red | rosybrown | royalblue |
saddlebrown | salmon | sandybrown | seagreen |
seashell | sienna | silver | skyblue |
slateblue | slategray | snow | springgreen |
steelblue | tan | teal | thistle |
tomato | transparent | turquoise | violet |
webgray | webgreen | webmaroon | webpurple |
wheat | white | whitesmoke | yellow |
yellowgreen | dimgray |
See the official documentation for more: https://docs.godotengine.org/en/stable/classes/class_color.html
Basis
[edit | edit source]A Basis contains a 3x3 matrix for 3D position, rotation and scale of 3D objects. Basis may be inferred as a function's return value with ->Basis
.
Basiss evaluate as true
in an if statements if at least one of its Vector3s would also evaluate as true:
if Basis(Vector3(1, 0, 0), Vector3(0, 0, 0), Vector3(0, 0, 0)):
pass # This runs
See also
[edit | edit source]Matrices and transforms tutorial.
See the official documentation for more: https://docs.godotengine.org/en/stable/classes/class_basis.html
Transform2D
[edit | edit source]A Transform2D is a 2x3 matrix used for 2D linear transformations. It can represent transformations such as translation, rotation or scaling. It consists of three Vector2 values: x, y and origin. Transform2D may be inferred as a function's return value with ->Transform2D
.
Transform2Ds evaluate as true
in an if statements if at least one value is not 0:
if Transform2D(1, 0, 0, 1, 0, 0):
pass # This runs
See more: The matrices and transforms tutorial.
See the official documentation for more: https://docs.godotengine.org/en/stable/classes/class_transform2d.html
Transform
[edit | edit source]A Transform is a 3x4 matrix used for 3D linear transformations. It can represent transformations such as translation, rotation or scaling. It consists of a Basis (first 3 Vector3s) and origin (last value). Transform may be inferred as a function's return value with ->Transform
.
Transforms evaluate as true
in an if statements if at least one value is not 0:
if Transform(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0):
pass # This runs
See more: The matrices and transforms tutorial and the using Transforms tutorial.
See the official documentation for more: https://docs.godotengine.org/en/stable/classes/class_transform.html
Pool<type>Array
[edit | edit source]For detailed information, please see Array. PoolArrays are optimized versions of Array, but allow storing only one variant type. They also don't contain certain functions, like find()
.
PoolArrays are passed as value rather than reference:
var names = PoolStringArray(["Hannah", "Paul", "Bob"]) var reference_array = ["Hi"] func _ready(): var names_copy = names names_copy.append("Sam") prints(names, names_copy) # Only the second one was changed var reference_array_copy = reference_array reference_array_copy.append("human") prints(reference_array) # Both Arrays were changed here var copy2 = reference_array.duplicate() copy2.append("I am a robot") prints(reference_array, copy2) # They are now both different
As you can see, the PoolStringArray does not keep the same values across all references, but the Array does.
Allowed types for Pool<type>Arrays are: int, float, string, real (bytes), color, vector2, and vector3. They may only store data types that match the type mentioned in the name, and are more optimized in terms of memory.