Overview
GDExtension is Godot’s system for creating native extensions using C++ or other compiled languages. It provides a C API for extending Godot’s functionality with high-performance code while maintaining full engine integration.GDExtension replaces GDNative from Godot 3.x with improved performance, better compatibility, and easier development.
Why Use GDExtension?
Maximum performance
Native compiled code for CPU-intensive algorithms, physics, and AI.
Existing C++ libraries
Integrate third-party C++ libraries directly into Godot.
Platform-specific code
Access platform APIs and SDKs not exposed by Godot.
Reusable modules
Share compiled extensions across projects without source code.
Getting Started
Prerequisites
1
Install a C++ compiler
Windows: Visual Studio 2019+ or MinGW-w64macOS: Xcode Command Line ToolsLinux: GCC or Clang
2
Install SCons build system
3
Clone godot-cpp
4
Build godot-cpp
<platform> with: windows, linux, macos, android, or iosProject Structure
A typical GDExtension project structure:Creating Your First Extension
1. Define a Custom Class
my_class.h
2. Implement the Class
my_class.cpp
3. Register the Extension
register_types.h
register_types.cpp
4. GDExtension Configuration
Createmy_extension.gdextension:
my_extension.gdextension
5. Build Configuration (SConstruct)
SConstruct
6. Build the Extension
Using Your Extension in Godot
In GDScript
In C#
Advanced Features
Working with Godot Types
Resource Management
Calling Godot Singletons
Performance Benefits
- CPU-intensive tasks
- Data processing
- Math operations
GDExtension provides near-native C++ performance, often 10-100x faster than GDScript for CPU-intensive operations.
Debugging
Print Debugging
Native Debugger
Attach a C++ debugger to the running Godot process:- Visual Studio
- GDB (Linux)
- LLDB (macOS)
- Build with debug symbols (
target=template_debug) - Run Godot
- Debug → Attach to Process
- Select the Godot process
- Set breakpoints in your C++ code
Platform-Specific Code
Best Practices
Profile first
Only use GDExtension for proven performance bottlenecks. GDScript is sufficient for most game logic.
Clear API boundaries
Design clean interfaces between GDScript and C++ code.
Error handling
Validate input from GDScript. Check for null pointers and invalid data.
Memory management
Use Godot’s reference counting (
Ref<T>) for resources. Don’t use raw new/delete.Build for all platforms
Test your extension on all target platforms early.
Documentation
Document your C++ API thoroughly for GDScript users.
Language Bindings
GDExtension isn’t limited to C++. Community bindings exist for:- Rust: gdext
- D: godot-dlang
- Nim: godot-nim
- Swift: SwiftGodot
- Zig: godot-zig
Example: Rust GDExtension
Next Steps
GDScript
Learn Godot’s built-in scripting language
C# Scripting
Use C# for managed code with .NET
godot-cpp Repository
Official C++ bindings repository
Performance Optimization
Learn more about optimizing Godot games