Cheatsheet
Additional info at GD Script Reference
Classes
Any script is a class. By default, all script files are unnamed classes. If you want to reference it, you need to give it a name (also can give it an icon):
@icon("res://interface/icons/item.png")
class_name MyNode extends Node
Sequences
GDScript doesn't know Lists, use Arrays instead
Array
var array = ["One", 2, 3, "Four"]
print( array.count() )
print( array[0] ) # One.
print( array[-1] ) # Four.
array[2] = "Three"
print( array[-2] ) # Three.
# add value
array.append("Five!")
# merge arrays
var array2 = [6, "seven"]
array += array2 # adds values. Alternative: array.append_array(array2)
# clear array
array.clear()
# cone array
array2 = array.duplicate() # can make a deep clone!
# check for value
if "seven" in array:
print("Seven is in the array!")
# random functions
print( array.pick_random() )
array.shuffle()
# pop and push
var first = array.pop_front()
var last = array.pop_back()
array.push_front( first )
array.push_back( last )
# list lambda/comprehension
var new_array = [1, 2, 3].map( func(x): return round(x) )
var new_array = ([1, 2, 3]
.filter(func(x): return x > 1)
.map(func(x): return round(x))
)
Making arrays useable in the editor:
@export var tiles: Array[Resource]
Dictionary
Dictionaries can be read_only!
var my_dict = {} # Creates an empty dictionary.
var points_dict = {"White": 50, "Yellow": 75, "Orange": 100} # Creats dict with values
var dict_variable_key = "Another key name"
var dict_variable_value = "value2"
var another_dict = {
"Some key name": "value1",
dict_variable_key: dict_variable_value,
}
var points_dict = {"White": 50, "Yellow": 75, "Orange": 100}
# iterating. No .items() like in python.
for key in dict:
var value = dict[key]
doSomethingWith(key, value)
Strings
Godot has 3 different ways to format strings.
# Define a format string with placeholder '%s'
var string = "I have %s cats." % "3"
# The String.format() method
var string = "I have {} cats.".format([3])
# String concatenation
var string = "I have " + str(3) + " cats."
Flow Control
These flow control expressions exist:
Properties aka Get/Set
var milliseconds: int = 0
var seconds: int:
get:
return milliseconds / 1000
set(value):
milliseconds = value * 1000
Signals
Signals are a tool to emit messages from an object that other objects can react to.
Export (aka"make editable in inspector")
@export_group("My Properties") # Group exported variables
@export_subgroup("Integers") # Nested group within main group
@export var number = 5 # Expose variable in editor
@export var int_number: int # with defined type
@export_subgroup("Probs")
@export var resource: Resource # Resource, which can be preloaded
@export var node: Node # Node in scene as reference
@export_category("Strings") # Change name
@export_file var my_file # String as file...
@export_dir var my_path # ...and as path
@export_multiline var text # And some text
@export_range(-10, 20) var ranged_int # ranged integer and float
@export_range(-10, 20, 0.2) var ranged_float: float
@export_exp_easing var transition_speed
@export_subgroup("Colors")
@export var col: Color
@export_color_no_alpha var col_no_alpha: Color
@export_subgroup("Enums") # Enums can only be int or string
enum NamedEnum {THING_1, THING_2, ANOTHER_THING = -1}
@export var x: NamedEnum #
@export_enum("Warrior", "Magician", "Thief") var character_class: int
@export_enum("Rebecca", "Mary", "Leah") var character_name: String = "Rebecca"
@export_subgroup("Arrays")
@export var ints: Array[int] = [1, 2, 3]
@export var two_dimensional: Array[Array] = [[1.0, 2.0], [3.0, 4.0]]
@export var textures: Array[Texture] = [] # drag and drop arrays
@export var scenes: Array[PackedScene] = []
This script will look something like this in the inspector:
See also:
Commenting Code
Commenting GDScript code with ## Explanation will be interpreted as documentation by godot.
class_name SomeClass extends Node2D
## A brief description of the class's role and functionality.
##
## The description of the script, what it can do,
## and any further detail.
##
## @tutorial: https://example.com/tutorial_1
## @tutorial(Tutorial 2): https://example.com/tutorial_2
## @experimental
Math
| Func | Desc |
|---|---|
sin(angle) |
Sinus |
cos(angle) |
Cosinus |
tan(angle) |
Tangens |
asin(cord) |
Arcus Sinus |
acos(cord) |
Arcus Cosinus |
atan(cord) |
Arcus Tangens |
atan2(cord_y, cord_x) |
Arcus Tangens 2 |
clamp(var, max, min) |
Clamp a value |
max(var1, var2, var3, ...) |
Maximum of a range of values |
min(var1, var2, var3, ...) |
Minimum of a range of valus |
abs(var) |
Absolute of a value |
round(var) |
Round up |
sign(var) |
Sign of a value |
randi() |
Random Integer |
randf() |
Random Float |
randf_range(from, to) |
Random Float within a range |
lerp(from, to, weight) |
Lerp |
move_towards(from, to, amount) |