Your First Script

Select the Sprite2D, click the attach script icon (scroll with a plus), keep the template, and save as res://sprite.gd.

5.1 Attaching a script

Select the Sprite2D, click the attach script icon (scroll with a plus), keep the template, and save as res://sprite.gd.

extends Sprite2D — this script is a Sprite2D (inheritance); it can use all Sprite2D properties directly (rotation, position).

_ready() runs once when the node and its children have entered the tree.

_process(delta) runs every rendered frame; delta is seconds since the last frame — multiply movement by it to be frame-rate independent.

GDScript uses tabs for indentation by convention (spaces work if consistent).

extends Sprite2D

var speed := 400.0            # pixels per second
var angular_speed := PI       # radians per second


func _ready() -> void:
	print("Hello, Godot! My name is ", name)


func _process(delta: float) -> void:
	rotation += angular_speed * delta
	var velocity := Vector2.UP.rotated(rotation) * speed
	position += velocity * delta

5.2 Printing and comments

# A single-line comment
## A documentation comment (shown in the editor's help)
print("Score: ", 10, " lives: ", 3)   # concatenates arguments
prints("a", "b")                      # separated by spaces
printt("a", "b")                      # separated by tabs
print_rich("[color=green]OK[/color]") # BBCode in the Output panel
push_warning("Low health")            # shows in the debugger as a warning
push_error("Something broke")

5.3 A standalone script (no scene)

# hello.gd — run with: godot --headless --script hello.gd
extends SceneTree

func _init() -> void:
	print("Hello from the command line!")
	quit()

5.4 The template structure

New scripts start from templates (*Project Settings → Editor → Script Templates*, or res://script_templates/). You can create your own templates for common node types.