Variables & Data Types
Learn GDScript's core data types, the difference between var and const, and how optional static typing works alongside dynamic typing.
Introduction
GDScript gives you a choice most languages don't: write fast, dynamically typed code while prototyping, then add type annotations exactly where you want extra safety and editor autocomplete. This lesson covers both sides of that choice.
- The difference between var and const, and when to reach for each.
- GDScript's core data types.
- How dynamic typing works by default, and how optional static typing changes that.
var and const
var declares a variable whose value can change; const declares a constant whose value is fixed at compile time and can never be reassigned.
var score = 0const MAX_LIVES = 3
score = score + 10print(score) # 10print(MAX_LIVES) # 3# MAX_LIVES = 5 # error: cannot assign to a constantClick Run to see what this code prints.
Core Data Types
GDScript's built-in types cover the basics you'd expect, plus game-specific types like vectors and colors, covered fully in lesson 10.
| Type | Example | Notes |
|---|---|---|
| int | 42 | Whole numbers. |
| float | 3.14 | Decimal numbers. |
| String | "hello" | Text, in double or single quotes. |
| bool | true, false | Logical true/false. |
| null | null | Represents "no value," similar to nil or None in other languages. |
| Array | [1, 2, 3] | An ordered, resizable list — covered further in lesson 10. |
| Dictionary | {"key": "value"} | A key-value map — covered further in lesson 10. |
Dynamic Typing by Default
Without a type annotation, a var can hold any type, and its type is determined at runtime by whatever value it currently holds — ideal for fast prototyping when you don't want type declarations slowing you down.
var value = 10print(value) # 10
value = "now a string"print(value) # now a stringClick Run to see what this code prints.
Optional Static Typing
Since Godot 4's GDScript 2.0, you can annotate a variable's type explicitly with a colon. Once typed, GDScript enforces that type, catches mismatched assignments as errors, and the editor offers accurate autocomplete for that variable.
var health: int = 100var player_name: String = "Aarav"var is_alive: bool = true
# health = "full" # error: cannot assign a String to a variable typed intStatic typing costs a little more typing up front but pays off as a project grows — typos and type mismatches become editor errors caught before you ever hit run, instead of confusing runtime bugs discovered mid-playtest.
Type Inference with :=
The := operator infers a variable's type from its initial value, giving you the safety of static typing without writing the type name out explicitly.
var speed := 200.0 # inferred as floatvar label := "Score" # inferred as String
print(speed, " ", label)Click Run to see what this code prints.
Common Mistakes
- Mixing typed and untyped variables inconsistently across a project, making it harder to predict which parts benefit from editor error-checking.
- Trying to reassign a const — Godot will raise a compile-time error immediately.
- Assuming := and = behave the same — := specifically triggers type inference; = alone on an already-typed variable just assigns.
Best Practices
- Use const for values that should never change, like MAX_LIVES or a physics constant.
- Adopt static typing (or := inference) for anything beyond a quick prototype — it pays for itself in caught bugs.
- Use SCREAMING_SNAKE_CASE for constants and snake_case for variables, matching Godot's own style conventions.
Frequently Asked Questions
No — it's entirely optional. You can mix typed and untyped variables freely within the same script or project.
It defaults to null, GDScript's representation of "no value" — similar to nil in Lua or None in Python.
Yes, typically — statically typed GDScript can be optimized more aggressively by the engine than fully dynamic code, on top of the error-catching benefits.
Key Takeaways
- var declares a reassignable variable; const declares a fixed constant.
- GDScript's core types include int, float, String, bool, null, Array, and Dictionary.
- Variables are dynamically typed by default, but can be explicitly typed for safety and autocomplete.
- The := operator infers a type from a variable's initial value.
Summary
You now know how to declare variables and constants, GDScript's core data types, and how to choose between dynamic and static typing as a project grows. Next, you'll meet nodes and the scene tree — the single most important concept in this entire course.