GDScript language preview
1. Player controller
annotations, signals, typed properties, and control flow
horizon-dark
extends CharacterBody2D
class_name Player
@export var speed: float = 300.0
@onready var sprite: Sprite2D = $Sprite2D
signal health_changed(new_health: int)
var _health := 100
func _ready() -> void:
sprite.play("idle")
func take_damage(amount: int) -> void:
_health -= amount
if _health <= 0:
queue_free()
emit_signal("health_changed", _health)
func _physics_process(delta: float) -> void:
var direction := Input.get_vector("left", "right", "up", "down")
velocity = direction * speed
move_and_slide() atom-one-dark
extends CharacterBody2D
class_name Player
@export var speed: float = 300.0
@onready var sprite: Sprite2D = $Sprite2D
signal health_changed(new_health: int)
var _health := 100
func _ready() -> void:
sprite.play("idle")
func take_damage(amount: int) -> void:
_health -= amount
if _health <= 0:
queue_free()
emit_signal("health_changed", _health)
func _physics_process(delta: float) -> void:
var direction := Input.get_vector("left", "right", "up", "down")
velocity = direction * speed
move_and_slide() github-dark
extends CharacterBody2D
class_name Player
@export var speed: float = 300.0
@onready var sprite: Sprite2D = $Sprite2D
signal health_changed(new_health: int)
var _health := 100
func _ready() -> void:
sprite.play("idle")
func take_damage(amount: int) -> void:
_health -= amount
if _health <= 0:
queue_free()
emit_signal("health_changed", _health)
func _physics_process(delta: float) -> void:
var direction := Input.get_vector("left", "right", "up", "down")
velocity = direction * speed
move_and_slide() dracula
extends CharacterBody2D
class_name Player
@export var speed: float = 300.0
@onready var sprite: Sprite2D = $Sprite2D
signal health_changed(new_health: int)
var _health := 100
func _ready() -> void:
sprite.play("idle")
func take_damage(amount: int) -> void:
_health -= amount
if _health <= 0:
queue_free()
emit_signal("health_changed", _health)
func _physics_process(delta: float) -> void:
var direction := Input.get_vector("left", "right", "up", "down")
velocity = direction * speed
move_and_slide() nord
extends CharacterBody2D
class_name Player
@export var speed: float = 300.0
@onready var sprite: Sprite2D = $Sprite2D
signal health_changed(new_health: int)
var _health := 100
func _ready() -> void:
sprite.play("idle")
func take_damage(amount: int) -> void:
_health -= amount
if _health <= 0:
queue_free()
emit_signal("health_changed", _health)
func _physics_process(delta: float) -> void:
var direction := Input.get_vector("left", "right", "up", "down")
velocity = direction * speed
move_and_slide() github
extends CharacterBody2D
class_name Player
@export var speed: float = 300.0
@onready var sprite: Sprite2D = $Sprite2D
signal health_changed(new_health: int)
var _health := 100
func _ready() -> void:
sprite.play("idle")
func take_damage(amount: int) -> void:
_health -= amount
if _health <= 0:
queue_free()
emit_signal("health_changed", _health)
func _physics_process(delta: float) -> void:
var direction := Input.get_vector("left", "right", "up", "down")
velocity = direction * speed
move_and_slide() 2. Export annotations and node paths
@export/@onready annotations and $NodePath access
horizon-dark
@export_range(0, 100) var health: int = 100
@export_category("Movement")
@export var speed: float = 250.0
@export_flags("Fire", "Water", "Earth", "Air") var elements := 0
@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var hitbox := $Hitbox/CollisionShape2D
@onready var label: Label = %HealthLabel atom-one-dark
@export_range(0, 100) var health: int = 100
@export_category("Movement")
@export var speed: float = 250.0
@export_flags("Fire", "Water", "Earth", "Air") var elements := 0
@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var hitbox := $Hitbox/CollisionShape2D
@onready var label: Label = %HealthLabel github-dark
@export_range(0, 100) var health: int = 100
@export_category("Movement")
@export var speed: float = 250.0
@export_flags("Fire", "Water", "Earth", "Air") var elements := 0
@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var hitbox := $Hitbox/CollisionShape2D
@onready var label: Label = %HealthLabel dracula
@export_range(0, 100) var health: int = 100
@export_category("Movement")
@export var speed: float = 250.0
@export_flags("Fire", "Water", "Earth", "Air") var elements := 0
@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var hitbox := $Hitbox/CollisionShape2D
@onready var label: Label = %HealthLabel nord
@export_range(0, 100) var health: int = 100
@export_category("Movement")
@export var speed: float = 250.0
@export_flags("Fire", "Water", "Earth", "Air") var elements := 0
@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var hitbox := $Hitbox/CollisionShape2D
@onready var label: Label = %HealthLabel github
@export_range(0, 100) var health: int = 100
@export_category("Movement")
@export var speed: float = 250.0
@export_flags("Fire", "Water", "Earth", "Air") var elements := 0
@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var hitbox := $Hitbox/CollisionShape2D
@onready var label: Label = %HealthLabel 3. Match statement
GDScript's pattern matching, distinct from Python's
horizon-dark
func _handle_input(action: String) -> void:
match action:
"jump":
velocity.y = -400.0
"crouch", "slide":
state = State.CROUCHING
_:
push_warning("unknown action: %s" % action) atom-one-dark
func _handle_input(action: String) -> void:
match action:
"jump":
velocity.y = -400.0
"crouch", "slide":
state = State.CROUCHING
_:
push_warning("unknown action: %s" % action) github-dark
func _handle_input(action: String) -> void:
match action:
"jump":
velocity.y = -400.0
"crouch", "slide":
state = State.CROUCHING
_:
push_warning("unknown action: %s" % action) dracula
func _handle_input(action: String) -> void:
match action:
"jump":
velocity.y = -400.0
"crouch", "slide":
state = State.CROUCHING
_:
push_warning("unknown action: %s" % action) nord
func _handle_input(action: String) -> void:
match action:
"jump":
velocity.y = -400.0
"crouch", "slide":
state = State.CROUCHING
_:
push_warning("unknown action: %s" % action) github
func _handle_input(action: String) -> void:
match action:
"jump":
velocity.y = -400.0
"crouch", "slide":
state = State.CROUCHING
_:
push_warning("unknown action: %s" % action)