drummyfish-s_based--data--/cheatsheets/gdscript cheatsheet.md

2.9 KiB

work in progress

GDScript

Comparison with Python

Only the differences between the languages are mentioned.

feature/construct Python GDScript
garbage collection yes no (only reference counting)
1st class citizen functions yes no (possible: o.call("func_name",[]))
exceptions try and except keywords no
built-in types basic:
bool, int, float, complex, str
container:
list, dict, tuple, set, ...
basic:
null, bool, int, float, String
container:
Array, Dictionary, PoolByteArray, ...
vector:
Vector2, Vector3, Rect2, Transform2D,Transform, Basis, Quat, AABB
engine:
Color, NodePath, RID, Object
basic constants None, True, False null, true, false
constructor name __init__ _init
module importing import keyword preload keyword
self variable only in class functions, has to be an explicit argument (e.g. __init__(self)) is always implicitly present (in non-static functions)
variable definition x = 5 var x = 5
or to assign when the node is ready:
onready var x = 5
or to export to GUI interface and save:
export var x = 5
call superclass method super(SelfClass,self).method() .method()
enumerations enum.Enum class enum MyEnum {C1, C2, C3} (syntactic sugar for constants)
π math.pi PI
τ = 2π no TAU
math.inf or float("inf") INF
not a number float("nan"), math.isnan(x) NAN
is keyword test for identity test for (sub)class
setters/getters @property and @x.setter decorators var x = 5 setget x_setter, x_getter
switch/case no match expression:
patterns:
code # no break!
...
_:
default code # no break!
signals no declare: signal sig(a,b)
conn.:o.connect("sig",o2,"cb",[1,2])
emit:emit_signal("sig",3,4)
coroutines no yield and resume keywords

Script template

# here there can possibly be the "tool" keyword

# The script is a class, so it has to inherits from something:
extends ClassName     # ClassName = class or subclass of the node the script is attached to

# class variables go here, e.g.:
# var memberVar = 5
# const MEMBER_CONST = 10

func _init():
    # constructor, called when the object is initialised 
    pass
    
func _ready():
    # called when the node is added to scene
    pass
    
#func _process(delta):
#    # called each frame
#    pass