iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Godot GDScript

Godot + GDScript: the cross-platform open-source engine. Scenes, nodes, signals, and the Python-flavoured scripting that runs everything.

Game dev — Godot + GDScript

EXAMPLE
# ===== The model =====
# Scene tree: every game object is a Node; scenes nest into other scenes.
# Signals: nodes emit events; scripts connect handlers.
# GDScript: Python-like; statically typed in 2026; fast for the engine.

# ===== A first scene + script =====
# Scene: Player (Node2D) with children Sprite2D + CollisionShape2D
# player.gd
extends Node2D

@export var speed: float = 200.0
@export var jump: float = 400.0

var velocity := Vector2.ZERO

func _ready() -> void:
    print("Player ready")

func _process(delta: float) -> void:
    var dir := Input.get_axis("move_left", "move_right")
    velocity.x = dir * speed
    if Input.is_action_just_pressed("jump"):
        velocity.y = -jump
    velocity.y += 980.0 * delta
    position += velocity * delta

# Lifecycle (most-used):
#   _ready()         once when entered the tree
#   _process(dt)     every frame
#   _physics_process(dt) fixed-step physics (60 Hz default)
#   _input(event)    raw input events
#   _exit_tree()     when leaving the tree

# ===== Signals (emit + connect) =====
# enemy.gd
extends Node2D
signal died(score: int)

func take_damage(n: int) -> void:
    hp -= n
    if hp <= 0:
        died.emit(100)
        queue_free()

# game.gd
func _ready() -> void:
    $Enemy.died.connect(_on_enemy_died)

func _on_enemy_died(score: int) -> void:
    $ScoreLabel.text = "Score: %d" % score

# ===== Resources (.tres / .res) =====
# Data files saved as resources; reusable across scenes.
# weapon.gd
extends Resource
@export var damage: int = 10
@export var range: float = 50.0

# Use: var weapon: Weapon = load("res://weapons/sword.tres")

# ===== Areas + bodies (collisions) =====
extends Area2D

func _ready() -> void:
    body_entered.connect(_on_body_entered)

func _on_body_entered(body: Node2D) -> void:
    if body.is_in_group("player"):
        body.take_damage(1)

# ===== AutoLoad (singletons) =====
# Project -> Project Settings -> AutoLoad
# Add a script as a global singleton (e.g. GameState).
# Access in any script: GameState.score += 1

# ===== Tilemaps =====
# Use the TileMap node + TileSet resource.
# Painting in the editor; collisions per tile.

# ===== 3D and shaders =====
# Godot also supports full 3D with PBR materials + shaders (GLSL-like).
# Cross-platform export: Windows, macOS, Linux, web (HTML5), iOS, Android, console (via add-ons).

# ===== Patterns to internalise =====
# - Scenes as reusable units; instance instead of duplicate
# - Signals for loosely coupled communication
# - Resources for data; scripts for behaviour
# - AutoLoad sparingly for genuinely global state

# ===== Pitfalls =====
# - Forgetting type hints (GDScript 2 is typed; types catch many bugs)
# - Tight coupling via direct node paths ($Parent/Sibling) — use signals
# - Heavy work in _process (move to threads or coroutines when needed)
# - Releasing without testing exports per target (mobile, web, console differ)

Why it matters

Godot uses a scene tree of typed nodes wired together by signals. GDScript reads like Python and runs fast; the editor is fast; cross-platform export is included. Lean on scenes-as-instances, signals for communication, resources for data, and AutoLoad sparingly.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
# Signals are GDScript's event system
signal jumped

func _physics_process(delta):
    if Input.is_action_just_pressed("jump"):
        emit_signal("jumped")
Try it Yourself »

Discussion

Loading…