GDScript Basics
Godot Fundamentals
Chapter 2 · GDScript Basics
Chapter 1 attached a first script with extends and touched _ready() without
explaining GDScript itself. This chapter covers the language properly: variables and typing, the built-in
types, functions, and control flow — the same territory a first Python chapter would cover, and for good
reason, since GDScript's own syntax was deliberately modeled on Python's.
func plays the
exact role def does. The differences worth tracking as you go: GDScript adds optional
static typing that Python doesn't have (without a separate tool), it has no elif-free
equivalent quirks — it actually spells it elif, same as Python — and its match
statement (covered below) predates Python's own match by several years, though the two look
similar today.
Variables & Type Hints
A variable is declared with var. Like Python, GDScript doesn't require a type at all — but
unlike Python, it lets you add one directly, and the editor will then catch a type mismatch before you ever
run the game.
A const works the same way but can never be reassigned after it's set — used for values that
are genuinely fixed, like a maximum speed or a gravity constant.
Built-in Types
| Type | Example | Notes |
|---|---|---|
int | var lives: int = 3 | Whole numbers |
float | var speed: float = 150.0 | Decimal numbers |
bool | var is_alive := true | true/false, lowercase |
String | var name := "Sam" | Text — capital S, unlike Python's lowercase str |
Array | var items = [1, 2, 3] | Like a Python list; can optionally be typed, e.g. Array[int] |
Dictionary | var d = {"hp": 100, "mp": 50} | Like a Python dict, key/value pairs |
Vector2 | var pos = Vector2(10, 20) | Godot-specific — an (x, y) pair used constantly for position/movement |
Array and Dictionary map directly onto Python's list and
dict — same square-bracket and curly-brace literal syntax, same mixed-type-by-default
flexibility. Vector2 has no direct Python equivalent in the standard library (it's closer to a
NumPy array of length 2) — it's Godot's own building block for anything with an x/y position, and it shows
up everywhere starting in Chapter 4.
Functions
Functions are declared with func, and — like variables — parameters and return values can
optionally be typed.
A function with no meaningful return value is typed -> void, GDScript's explicit way of
saying "this function doesn't return anything" — Python has no direct equivalent (a bare Python function
implicitly returns None, with nothing written to say so).
Control Flow
if / elif / else
for loops
while loops
match — GDScript's switch statement
Python only gained match in version 3.10; GDScript has had one from early on. The shape is
familiar if you've used Python's version: match a value against several patterns, with _ as the
catch-all default.
Coding Challenges
calculate_damage(base: int, multiplier: float) -> int that returns
the base damage multiplied by the multiplier, rounded down to a whole number. Call it with a few
different values and print the results.
Array of five enemy names as Strings. Using a for loop, print each
name along with its position in the array (e.g. "1: Slime").
while loop, subtracts 15
health per iteration. Inside the loop, use if/elif/else to print
"Critical!", "Hurt", or "Healthy" depending on the current health, and stop the loop once health
reaches 0 or below.
Quick Reference — GDScript Basics
var name: Type = value— typed;var name := value— inferred;var name = value— untypedconst NAME = value— never reassigned- Core types:
int,float,bool,String,Array,Dictionary,Vector2 func name(param: Type = default) -> ReturnType:if/elif/else,for x in range(n),for x in array,while,match- Indentation-based blocks, no semicolons — same as Python