GDScript Cheat Sheet

Godot 4.6 Quick Reference — godotlearning.com

Variables & Types

Declaration

var speed = 300.0
var name: String = "Player"
const MAX_HP = 100
@export var damage: int = 10

Common Types

int, float, String, bool
Vector2, Vector3
Array, Dictionary
NodePath, PackedScene

Functions

Basic Function

func greet(name: String) -> String:
    return "Hello, " + name

Lifecycle

func _ready():      # Node entered tree
func _process(delta):  # Every frame
func _physics_process(delta): # Fixed step
func _input(event):   # Input events

Signals

Define & Emit

signal health_changed(new_hp)

func take_damage(amount):
    hp -= amount
    health_changed.emit(hp)

Connect

# In _ready():
button.pressed.connect(_on_pressed)

func _on_pressed():
    print("Clicked!")

Control Flow

If / Match

if hp <= 0:
    die()
elif hp < 30:
    warn()

match state:
    "idle": idle()
    "run": run()
    _: pass

Loops

for item in inventory:
    print(item)

for i in range(10):
    print(i)

while running:
    update()

Nodes & Scenes

Access Nodes

@onready var sprite = $Sprite2D
@onready var timer = $Timer

# Or by path:
get_node("Player/Sprite2D")

Instance Scenes

var scene = preload("res://bullet.tscn")
var bullet = scene.instantiate()
add_child(bullet)

Input

Input Actions

func _physics_process(delta):
    var dir = Input.get_vector("left", "right", "up", "down")
    velocity = dir * speed
    move_and_slide()

    if Input.is_action_just_pressed("jump"):
        velocity.y = jump_force

Physics Bodies

CharacterBody2D

extends CharacterBody2D

var speed = 300.0
var gravity = 980.0

func _physics_process(delta):
    velocity.y += gravity * delta
    move_and_slide()

Area Detection

extends Area2D

func _on_body_entered(body):
    if body.is_in_group("player"):
        collect()
        queue_free()

Save & Load

JSON Save System

func save_game():
    var data = {"hp": hp, "pos": position}
    var file = FileAccess.open("user://save.json", FileAccess.WRITE)
    file.store_string(JSON.stringify(data))

func load_game():
    var file = FileAccess.open("user://save.json", FileAccess.READ)
    var data = JSON.parse_string(file.get_as_text())

Generated by godotlearning.com — Print this page (Ctrl+P) to save as PDF