The Resource Cache is a centralized, thread-safe global registry that tracks all loaded resources in the engine by their file paths. It enables resource reuse, prevents redundant disk I/O by avoiding duplicate loading of the same asset, and supports hot-reloading of resources during runtime. For information about the loading mechanism itself, see ResourceLoader core/io/resource_loader.cpp31-42
The resource cache serves as the engine's memory for previously loaded resources. When a resource is loaded from disk, it is registered in the cache so that subsequent load requests for the same path return the existing instance. This system is critical for:
Sources: core/io/resource.h57-80 core/io/resource.cpp1-29 core/io/resource_loader.h31-50
The following diagram associates the logical cache components with their specific C++ entities within the core/io and core/object modules.
Core Components:
| Component | Type | Purpose |
|---|---|---|
ResourceCache::resources | HashMap<String, Resource *> | Maps absolute resource paths to raw resource pointers. core/io/resource.h57-90 |
ResourceCache::lock | Mutex | Protects concurrent access to the global resource registry. |
Resource::path_cache | String | The internal storage of the resource's current registered path. core/io/resource.h171-172 |
ResourceCache::resource_path_cache | HashMap | Used primarily in editor contexts to track unique IDs within scenes. |
Sources: core/io/resource.h57-96 core/io/resource.h169-182 core/io/resource_loader.h58-70
The ResourceFormatLoader::CacheMode enum (aliased in ResourceLoader) defines strategies for cache interaction. These modes determine if the engine should return an existing object or force a fresh read.
Cache Mode Details:
| Mode | Behavior | Propagation | Use Case |
|---|---|---|---|
CACHE_MODE_IGNORE | Returns a unique instance; does not check or update cache. core/io/resource_loader.h60 | Sub-resources use REUSE. | One-off previews or temporary objects. |
CACHE_MODE_REUSE | Returns cached instance if available; otherwise loads and caches. core/io/resource_loader.h61 | Sub-resources use REUSE. | Standard gameplay and editor asset loading. |
CACHE_MODE_REPLACE | Forces a reload and updates the existing cached instance. core/io/resource_loader.h62 | Sub-resources use REUSE. | Hot-reloading a specific modified file. |
CACHE_MODE_IGNORE_DEEP | Not cached; dependencies also ignore cache. core/io/resource_loader.h63 | Propagates IGNORE. | Full resource isolation. |
CACHE_MODE_REPLACE_DEEP | Updates instance and forces reload of all dependencies. core/io/resource_loader.h64 | Propagates REPLACE. | Cascade hot-reload of complex scenes. |
Sources: core/io/resource_loader.h58-65 core/io/resource_loader.cpp165-182
When Resource::set_path() is called, the resource enters the global registry. If the p_take_over flag is set, the resource will displace any existing entry for that path.
Sources: core/io/resource.h171-182 core/io/resource_loader.cpp58-83
The ResourceCache stores raw pointers (Resource *) to avoid circular reference increments that would prevent RefCounted objects from being freed. When a Resource is destroyed, its destructor ensures it is removed from the global map.
The cache system is extensively used by scripting backends to manage script dependencies and class resolution:
get_shallow_script to retrieve scripts from the cache or load them if missing. modules/gdscript/gdscript_compiler.cpp161-166script_bindings to map managed C# objects to Godot objects, ensuring that the interop bridge does not duplicate wrappers for the same underlying resource. modules/mono/csharp_script.cpp152-163script_path. modules/gdscript/gdscript_analyzer.cpp127-130Sources: modules/gdscript/gdscript_compiler.cpp156-176 modules/mono/csharp_script.cpp140-165 modules/gdscript/gdscript_analyzer.cpp87-131
Beyond file paths, the system supports Scene-Unique IDs (scene_unique_id). These are used to identify resources internal to a specific scene file (e.g., a specific Mesh within a .tscn).
ResourceCache tracks these IDs to facilitate sub-resource retrieval within the ResourceLoader framework. core/io/resource.h74-76@onready nodes or preloaded sub-resources. modules/gdscript/gdscript_parser.cpp146-152Sources: core/io/resource.h74-76 modules/gdscript/gdscript_parser.cpp143-155
Refresh this wiki
This wiki was recently refreshed. Please wait 3 days to refresh again.