Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
| gdscript:cheatsheet [2025/10/14 20:14] – silversurfer | gdscript:cheatsheet [2025/10/14 20:51] (aktuell) – [Fehlerbehandlung & Debug] silversurfer | ||
|---|---|---|---|
| Zeile 10: | Zeile 10: | ||
| ===== Variablen & Typen ===== | ===== Variablen & Typen ===== | ||
| - | ==== Dynamisch / untypisiert: ==== | + | ==== Dynamisch / untypisiert ==== |
| <codedoc code: | <codedoc code: | ||
| - | ==== Getypt (empfohlen): ==== | + | ==== Getypt (empfohlen) ==== |
| <codedoc code: | <codedoc code: | ||
| Zeile 20: | Zeile 20: | ||
| <codedoc code: | <codedoc code: | ||
| - | ==== export (sichtbar/ | + | ==== export (sichtbar/ |
| <codedoc code: | <codedoc code: | ||
| Zeile 26: | Zeile 26: | ||
| ===== Funktionen ===== | ===== Funktionen ===== | ||
| - | ==== Definition: ==== | + | ==== Definition ==== |
| <codedoc code: | <codedoc code: | ||
| - | return str(param) | + | return str(param)</ |
| - | - Lebenszyklus-Hooks: | + | ==== Lebenszyklus-Hooks |
| - | - func _ready(): | + | <codedoc code:python>func _ready(): |
| - | | + | func _process(delta): |
| - | | + | func _physics_process(delta): |
| - | Sichtbarkeits-/ | + | ==== Sichtbarkeits-/ |
| - | - @tool # Skript läuft auch im Editor (Godot 3/4) | + | <codedoc code: |
| - | - export, onready, class_name siehe oben | + | |
| - | Knoten & Szenen | + | ===== Knoten & Szenen ===== |
| - | - Zugriff: | + | |
| - | - var player | + | |
| - | - var p = $Player/ | + | |
| - | - Instanziieren: | + | |
| - | - var Packed | + | |
| - | - var enemy = Packed.instantiate() | + | |
| - | - var enemy = Packed.instance() | + | |
| - | - add_child(enemy) | + | |
| - | - Entfernen: | + | |
| - | - queue_free() | + | |
| - | Ressourcen laden | + | ==== Zugriff ==== |
| - | - preload | + | <codedoc code: |
| - | | + | var p = $Player/ |
| - | - load (runtime): | + | ==== Instanziieren ==== |
| - | | + | <codedoc code: |
| + | var enemy = Packed.instantiate() # Godot 4 | ||
| + | var enemy = Packed.instance() # Godot 3 (alte Methode) | ||
| + | add_child(enemy)< | ||
| + | ==== Entfernen ==== | ||
| + | <codedoc code: | ||
| - | Signale | + | ===== Ressourcen laden ===== |
| - | - Definieren / Emittieren: | + | ==== preload |
| - | - signal hit(damage) | + | <codedoc code:python> |
| - | | + | ==== load (runtime) ==== |
| - | - Verbinden: | + | <codedoc code:python> |
| - | - connect("hit", self, " | + | |
| - | - $Button.connect(" | + | |
| - | - Godot 4: Typed signal syntax möglich: | + | |
| - | - signal hit(damage: int) | + | |
| - | Eingabe (Input) | + | ===== Signale ===== |
| - | - _input(event) | + | ==== Definieren / Emittieren ==== |
| - | - func _input(event): | + | <codedoc code: |
| - | if event is InputEventKey and event.pressed: | + | emit_signal(" |
| - | if event.scancode | + | ==== Verbinden ==== |
| - | jump() | + | <codedoc code: |
| - | - Input-Mapping (Project Settings -> InputMap): | + | $Button.connect("pressed", |
| - | - if Input.is_action_pressed("ui_right" | + | ==== Godot 4: Typed signal syntax möglich ==== |
| + | <codedoc code: | ||
| - | Await / Coroutines / Timer | + | ===== Eingabe |
| - | - Godot 3 yield: | + | |
| - | - yield(get_tree().create_timer(1.0), | + | |
| - | - Godot 4 await: | + | |
| - | - await get_tree().create_timer(1.0).timeout | + | |
| - | - Timer-Node: | + | |
| - | - $Timer.start(2.0) | + | |
| - | Collections | + | ==== _input(event) ==== |
| - | - Array: | + | <codedoc code: |
| - | - var a = [1, 2, 3] | + | if event is InputEventKey and event.pressed: |
| - | - a.append(4) | + | if event.scancode |
| - | - for i in a: ... | + | jump()</ |
| - | - Dictionary: | + | ==== Input-Mapping (Project Settings -> InputMap) ==== |
| - | - var d = {" | + | <codedoc code: |
| - | - if d.has("hp"): ... | + | ...</ |
| - | Kontrollstrukturen | + | ===== Await / Coroutines |
| - | - if/elif/else: | + | |
| - | - if x > 0: | + | |
| - | pass | + | |
| - | - match (Godot 4 / GDScript 2.0): | + | |
| - | - match value: | + | |
| - | 1: | + | |
| - | ... | + | |
| - | _: | + | |
| - | ... | + | |
| - | - for/ | + | |
| - | - for i in range(5): ... | + | |
| - | - while cond: ... | + | |
| - | Fehlerbehandlung & Debug | + | ==== Godot 3 yield ==== |
| - | - print(), push_error(), push_warning() | + | <codedoc code: |
| - | - assert(condition, " | + | ==== Godot 4 await ==== |
| + | <codedoc code: | ||
| + | ==== Timer-Node ==== | ||
| + | <codedoc code: | ||
| - | Best Practices (Kurz) | + | ===== Collections ===== |
| - | - Typisieren wo möglich | + | ==== Array ==== |
| - | - Export für Editor-Bearbeitbarkeit | + | <codedoc code: |
| - | - onready für Knoten-Referenzen | + | a.append(4) |
| - | - Verwende preload für häufig genutzte Ressourcen | + | for i in a: |
| - | - Trenne Gameplay-Logik in übersichtliche Methoden | + | ...</ |
| - | - Nutze Signals statt eng gekoppelter Node-Referenzen | + | ==== Dictionary: ==== |
| + | <codedoc code: | ||
| + | if d.has(" | ||
| + | ... </ | ||
| - | Nützliche One-Liner | + | ===== Kontrollstrukturen ===== |
| - | - Position setzen (Node2D): | + | |
| - | - position | + | |
| - | - Bewegung: | + | |
| - | - velocity | + | |
| - | - Prüfen ob in Gruppe: | + | |
| - | - if is_in_group(" | + | |
| - | - Connect per Code: | + | |
| - | - some_node.connect(" | + | |
| - | Kurze Beispiele | + | ==== if/ |
| + | <codedoc code: | ||
| + | # die Zahl ist größer Null | ||
| + | | ||
| + | elif x < 0: | ||
| + | # die Zahl ist kleiner Null | ||
| + | | ||
| + | else: | ||
| + | # die Zahl ist Null | ||
| + | | ||
| - | Player | + | ==== match (Godot 4 / GDScript 2.0): ==== |
| - | ```gdscript | + | <codedoc code: |
| - | extends CharacterBody2D | + | 1: |
| - | class_name Player | + | ... |
| + | _: | ||
| + | ...</ | ||
| + | |||
| + | ==== for/while: ==== | ||
| + | <codedoc code: | ||
| + | ... | ||
| + | |||
| + | while cond: | ||
| + | | ||
| + | |||
| + | ===== Fehlerbehandlung & Debug ===== | ||
| + | * print(), push_error(), | ||
| + | * assert(condition, | ||
| + | * | ||
| + | |||
| + | ===== Best Practices (Kurz) ===== | ||
| + | * Typisieren wo möglich (besseres Autocomplete & Fehlercheck) | ||
| + | * Export für Editor-Bearbeitbarkeit | ||
| + | * onready für Knoten-Referenzen | ||
| + | * Verwende preload für häufig genutzte Ressourcen | ||
| + | * Trenne Gameplay-Logik in übersichtliche Methoden | ||
| + | * Nutze Signals statt eng gekoppelter Node-Referenzen (lockere Kopplung) | ||
| + | |||
| + | ==== Nützliche One-Liner==== | ||
| + | === Position setzen (Node2D) === | ||
| + | <codedoc code: | ||
| + | === Bewegung === | ||
| + | <codedoc code: | ||
| + | === Prüfen ob in Gruppe === | ||
| + | <codedoc code: | ||
| + | | ||
| + | === Connect per Code: === | ||
| + | <codedoc code: | ||
| + | |||
| + | ===== Kurze Beispiele ===== | ||
| + | ==== Player (Grundgerüst) ==== | ||
| + | <codedoc code: | ||
| export var speed: float = 200.0 | export var speed: float = 200.0 | ||
| Zeile 143: | Zeile 160: | ||
| if Input.is_action_pressed(" | if Input.is_action_pressed(" | ||
| velocity.x = dir.x * speed | velocity.x = dir.x * speed | ||
| - | move_and_slide() | + | move_and_slide()</ |