GDScript Syntax and Data Types
GDScript Syntax and Data Types
GDScript is Godot's native scripting language, and understanding its syntax and data types is essential for game development. At the heart of GDScript is the Variant type, a flexible system that can represent and store any of Godot's core value types. This makes GDScript both powerful and beginner-friendly, as you don't always need to specify exact types when declaring variables.
The Four Classic Data Types
GDScript provides four fundamental data types that form the foundation of most programs:
- Boolean (bool): Stores true or false values, used for conditional logic and decision-making
- Integer (int): Whole numbers without decimals, ranging from -2^63 to 2^63 - 1 (64-bit signed integer)
- Float: Numbers with decimal points, stored as 64-bit double-precision floating-point numbers
- String: Text data, a sequence of characters enclosed in quotes
Beyond these basics, GDScript includes specialized types for game development such as Vector2, Vector3, Color, Rect2, and Transform3D, which provide built-in functionality for common game programming tasks.
Variable Declaration and Type Specification
Variables in GDScript can be declared with or without explicit type annotations. When you create a variable without specifying a type, it defaults to null:
var a # Data type is 'null' by default
var b = 5 # Inferred as integer
var c = 3.8 # Inferred as float
For stronger type safety, you can explicitly specify a variable's type using a colon after the variable name:
var health: int = 100
var character_name: String = "Godot"
When a type is specified, GDScript will enforce that the variable always maintains that type. Attempting to assign an incompatible value will raise an error, helping you catch bugs early in development.
Collections: Arrays and Dictionaries
Arrays are dynamic collections that can store multiple values of any type. You can create typed arrays to ensure all elements are the same type:
var scores: Array[int]
var nodes: Array[Node]
Dictionaries store key-value pairs, similar to hash maps in other languages. Typed dictionaries use the syntax Dictionary[KeyType, ValueType]:
var player_stats: Dictionary[String, int]
Type Casting
Casting is the process of converting one data type to another. This is useful when you need to transform data between different types—for example, converting an integer to a string for display purposes. GDScript allows you to cast variables to different types, though you must be careful to ensure the conversion makes sense.
Understanding Type Safety
When you declare a variable with a specific type, GDScript enforces type safety. For instance, if you declare var health: int = 100 and later try to assign text to it, your program will crash. This strictness is a feature, not a bug—it helps prevent errors by catching type mismatches before runtime.
Understanding these fundamentals gives you the tools to write reliable, maintainable GDScript code in Godot. Start with simple variables and gradually explore more complex data structures as your projects grow in complexity.