What is a Resource?
A Resource is the base class for all Godot-specific resource types. Resources are reference-counted objects that can be saved to disk, loaded, and shared between multiple nodes. They inherit fromRefCounted, which means they’re automatically freed when no longer referenced.
Resources are primarily data containers. Unlike nodes, they don’t have a position in the scene tree and don’t receive processing callbacks.
Common Resource Types
Godot includes many built-in resource types:- PackedScene: Stores a complete scene hierarchy
- Texture2D: Image resources for sprites and UI
- AudioStream: Sound and music files
- Material: Shader and rendering properties
- AnimationLibrary: Animation data
- Script: GDScript, C#, and other script files
- Theme: UI styling information
Loading Resources
There are several ways to load resources in Godot:Using preload()
preload() loads resources at compile time:
Using load()
load() loads resources at runtime:
Using ResourceLoader
For more control over loading:PackedScene
PackedScene is a special resource type that stores an entire scene hierarchy:Instantiating PackedScenes
Creating PackedScenes
You can create PackedScenes programmatically:Only nodes with their
owner property set will be saved when packing a scene. This prevents temporary or runtime-generated nodes from being saved unintentionally.Saving Resources
UseResourceSaver to save resources to disk:
Custom Resources
You can create your own resource types for game data:Why Use Custom Resources?
Why Use Custom Resources?
Custom resources provide several benefits:
- Data Separation: Game data is separate from game logic
- Reusability: The same resource can be used by multiple nodes
- Editor Integration: Can be edited in the Inspector
- Serialization: Automatically saved and loaded with scenes
- Type Safety: Define clear data structures with type checking
Creating Resource Files
You can create resource files in the editor or via code:Resource Properties
Resource Path
Every resource has a path indicating where it’s stored:Resource Name
Local to Scene
Theresource_local_to_scene property makes each scene instance get its own copy:
When
resource_local_to_scene is true, each scene instance gets a duplicate of the resource. This is perfect for data that should be instance-specific.Resource Duplication
Resource Signals
Resources can emit signals when they change:Practical Example: Inventory System
Here’s a complete example using custom resources:Resource Preloading Best Practices
1
Use preload() for Essential Resources
Preload resources that are needed immediately at startup:
2
Use load() for Optional Content
Load resources dynamically when they might not be needed:
3
Consider Resource Pooling
For frequently instantiated scenes, maintain a pool:
Best Practices
1
Use Custom Resources for Game Data
Create custom resource types instead of using Dictionaries or hard-coded values.
2
Set Ownership When Packing Scenes
Always set the
owner property on nodes you want to save in PackedScenes.3
Leverage resource_local_to_scene
Use this property for resources that should be unique per scene instance.
4
Emit changed Signal
Call
emit_changed() in custom resources when data changes to notify dependents.5
Be Mindful of Resource Caching
Remember that loaded resources are cached. Modifications affect all users of that resource.
Next Steps
Nodes and Scenes
Learn how nodes use resources
Scene Tree
Understand scene management