GDScript Cheat Sheet
Basic Syntax:
- Variables: var name = value
- Functions: func function_name():
- Body of the function
- Comments: # This is a comment
- Conditionals:
if condition:
# Code block
elif another_condition:
# Code block
else:
# Code block
- Loops:
for i in range(10):
# Code block
while condition:
# Code block
Data Types:
- int: Integer numbers (e.g., var x = 5)
- float: Floating-point numbers (e.g., var x = 5.5)
- bool: Boolean values (e.g., var is_true = true)
- String: Text values (e.g., var text = "Hello")
- Array: List of elements (e.g., var arr = [1, 2, 3])
- Dictionary: Key-value pairs (e.g., var dict = {"key": "value"})
Functions and Methods:
- Defining a function: func function_name():
- Code block
- Returning values: return value
- Built-in functions:
- print(value): Print to the console
- len(array): Get length of an array
- str(variable): Convert to string
Object-Oriented Programming:
- Classes: class ClassName:
- Constructor: func _init():
- Initialize variables
- Accessing members: object.member
- Inheritance: class Child extends Parent:
Signals and Events:
- Defining signals: signal signal_name
- Emitting signals: emit_signal("signal_name")
- Connecting signals: connect("signal_name", object, "method_name")
Built-in Nodes and Functions:
- Scene nodes: Node, Sprite, Camera2D, Area2D, etc.
- Accessing nodes: var node = get_node("path/to/node")
- Positioning: position, scale, rotation
- Movement: move_and_slide(), move_local_x(), etc.