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:29] – silversurfer | gdscript:cheatsheet [2025/10/14 20:51] (aktuell) – [Fehlerbehandlung & Debug] silversurfer | ||
|---|---|---|---|
| Zeile 79: | Zeile 79: | ||
| ===== Await / Coroutines / Timer ===== | ===== Await / Coroutines / Timer ===== | ||
| - | ==== Godot 3 yield: ==== | + | ==== Godot 3 yield ==== |
| - | yield(get_tree().create_timer(1.0), | + | <codedoc code: |
| - | - Godot 4 await: | + | ==== Godot 4 await ==== |
| - | | + | <codedoc code: |
| - | - Timer-Node: | + | ==== Timer-Node |
| - | | + | <codedoc code: |
| - | Collections | + | ===== Collections |
| - | - Array: | + | ==== Array ==== |
| - | | + | <codedoc code: |
| - | | + | a.append(4) |
| - | | + | for i in a: |
| - | - Dictionary: | + | ...</ |
| - | | + | ==== Dictionary: |
| - | | + | <codedoc code: |
| + | if d.has(" | ||
| + | ... </ | ||
| - | Kontrollstrukturen | + | ===== Kontrollstrukturen |
| - | - if/ | + | |
| - | - if x > 0: | + | |
| - | pass | + | |
| - | - match (Godot 4 / GDScript 2.0): | + | |
| - | - match value: | + | |
| - | 1: | + | |
| - | ... | + | |
| - | _: | + | |
| - | ... | + | |
| - | - for/ | + | |
| - | - for i in range(5): ... | + | |
| - | - while cond: ... | + | |
| - | Fehlerbehandlung & Debug | + | ==== if/ |
| - | - print(), push_error(), | + | <codedoc code: |
| - | - assert(condition, | + | # die Zahl ist größer Null |
| + | | ||
| + | elif x < 0: | ||
| + | # die Zahl ist kleiner Null | ||
| + | | ||
| + | else: | ||
| + | # die Zahl ist Null | ||
| + | | ||
| - | Best Practices | + | ==== match (Godot 4 / GDScript 2.0): ==== |
| - | - Typisieren wo möglich (besseres Autocomplete & Fehlercheck) | + | <codedoc code: |
| - | - Export für Editor-Bearbeitbarkeit | + | 1: |
| - | - 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 | + | |
| + | ==== for/while: ==== | ||
| + | <codedoc code: | ||
| + | ... | ||
| - | Nützliche One-Liner | + | while cond: |
| - | - Position setzen (Node2D): | + | |
| - | - position = Vector2(100, | + | |
| - | - Bewegung: | + | |
| - | - velocity = velocity.move_toward(target, | + | |
| - | - Prüfen ob in Gruppe: | + | |
| - | - if is_in_group(" | + | |
| - | - Connect per Code: | + | |
| - | - some_node.connect(" | + | |
| - | Kurze Beispiele | + | ===== Fehlerbehandlung & Debug ===== |
| + | * print(), push_error(), | ||
| + | * assert(condition, | ||
| + | | ||
| - | Player (Grundgerüst) | + | ===== Best Practices (Kurz) ===== |
| - | ```gdscript | + | * Typisieren wo möglich (besseres Autocomplete & Fehlercheck) |
| - | extends CharacterBody2D | + | * Export für Editor-Bearbeitbarkeit |
| - | class_name Player | + | * 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 146: | 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()</ |