Overview
GDScript is Godot’s built-in, high-level scripting language designed specifically for game development. It features a Python-like syntax that’s easy to learn while being tightly integrated with the Godot Engine.GDScript is gradually typed, meaning type hints are optional but recommended for better performance and safety.
Language Features
Python-like Syntax
GDScript uses indentation-based syntax similar to Python, making it readable and accessible:player.gd
Type System
GDScript supports both dynamic and static typing:- Dynamic typing
- Static typing
Variables and Constants
Variable Declaration
Constants
Constants are defined withconst and must be assigned at declaration:
Functions
Function Definition
Built-in Lifecycle Methods
Every Node has special methods that are called during its lifecycle:node_lifecycle.gd
Classes and Inheritance
Class Definition
Every.gd file implicitly defines a class. Use extends to inherit from a base class:
Inner Classes
Define classes within other classes:Inheritance Chain
enemy.gd
boss.gd
Signals
Signals are Godot’s implementation of the observer pattern:Connecting Signals
Annotations
Annotations (formerly called “tool mode” and “export”) modify how scripts and variables behave:@export
Expose variables to the Inspector:@onready
Initialize variables when the node enters the scene tree:@tool
Run the script in the editor:Other Annotations
Built-in Functions
GDScript provides many utility functions:Common Built-ins
Assertions and Debugging
Key Differences from Python
While GDScript is similar to Python, there are important differences:Type hints are different
Type hints are different
Python:
def func(x: int) -> int:GDScript: func func_name(x: int) -> int:No self parameter
No self parameter
GDScript methods don’t require an explicit
self parameter:Different string formatting
Different string formatting
GDScript uses
% for formatting:Arrays and dictionaries
Arrays and dictionaries
No list comprehensions
No list comprehensions
GDScript doesn’t support Python’s list comprehensions. Use loops instead:
Best Practices
Use static typing
Enable type hints for better performance and error detection.
Prefer signals over polling
Use signals for event-driven architecture instead of checking states every frame.
Use @onready for node references
Initialize node references with @onready instead of in _ready().
Keep scripts focused
Each script should have a single, clear responsibility.
Next Steps
C# Scripting
Learn about C# support in Godot
GDExtension
Create native extensions with C++
Visual Scripting
Explore node-based programming