godotengine / godotengine/godot
Duplicates in "unique" scene resource ID
- Dominant language
- C++
- Stars
- 117k
- Forks
- 26.8k
- PR merge metrics
- PR metrics pending
Description
### Tested versions
- Reproducible in v4.6.dev1.official [8d8041bd4]
- Reproducible in 4.5.1.stable.official [f62fdbde1]
- NOT reproducible in v4.4.1.stable.official [49a5bc7b6]
### System information
Windows 11 / Linux
### Issue description
There is a problem with duplicated resource IDs which is causing errors like this:
```
ERROR: Another resource is loaded from path '::heep2' (possible cyclic resource inclusion).
```
My first guess is that the problem started in pull https://github.com/godotengine/godot/pull/97362. In the file `core/io/resource.cpp`, the `String Resource::generate_scene_unique_id()` method should generate a new ID for each new resource. However, I have found that it keeps generating the same string repeatedly.
It turns out that the random number generator seed is constantly overwritten whenever a new resource is saved to disk.
When the editor is opened for the first time, a configuration file is saved (for example, `editor_settings-4.6.tres`) and the `ResourceFormatSaverTextInstance` (`scene/resources/resource_format_text.cpp`) sets the seed from its file name hash.
```
Error ResourceFormatSaverTextInstance::save(const String &p_path, const Ref &p_resource, uint32_t p_flags) {
Resource::seed_scene_unique_id(p_path.hash()); // Seeding for save path should make it deterministic for importers.
```
I guess seed is global for all resources, so when I'm creating a new resource it is no longer 0 as intended. Because of that, code `if (unique_id_gen.get_seed() == 0)` from `Resource::generate_scene_unique_id()` is never executed.
Good place to start investigation is `seed_scene_unique_id` in `core/io/resource.cpp`
```
static thread_local RandomPCG unique_id_gen = RandomPCG(0);
void Resource::seed_scene_unique_id(uint32_t p_seed) {
unique_id_gen.seed(p_seed);
}
```
Example log from custom build with `print_line` in `Resource`:
```
Godot Engine v4.6.dev.custom_build.0fdbf050e (2025-11-01 18:07:07 UTC) - https://godotengine.org
OpenGL API 3.3.0 NVIDIA 581.57 - Compatibility - Using Device: NVIDIA - NVIDIA GeForce GTX 960
Resource::seed_scene_unique_id 3674935510
(here i'm creating a new project)
Vulkan 1.4.312 - Forward+ - Using Device #0: NVIDIA - NVIDIA GeForce GTX 960
Resource::seed_scene_unique_id 3674935510
Resource::generate_scene_unique_id get_seed 3674935510
Resource::generate_scene_unique_id id heep2
(click new node, just before dialog window opens)
Resource::seed_scene_unique_id 3674935510
(add meshinstance3d, create new mesh)
Resource::generate_scene_unique_id get_seed 3674935510
Resource::generate_scene_unique_id id heep2
(click new node)
Resource::seed_scene_unique_id 3674935510
(add second meshinstance3d, create new mesh)
Resource::generate_scene_unique_id get_seed 3674935510
Resource::generate_scene_unique_id id heep2
ERROR: Another resource is loaded from path '::heep2' (possible cyclic resource inclusion).
```
For clarification:
- `3674935510` is the hash created from path to my config file, in my system "C:\(...)\editor_settings-4.6.tres"
- `heep2` is the id generated by `generate_scene_unique_id` when seed is `3674935510`
### Steps to reproduce
1. open editor, create new scene
2. create new node with resource slot, for example meshinstance3d
3. create new mesh - check it's resource path
4. (optional) open editor settings and click close - editor will save config and reset the seed
5. create new meshinstance3d
6. create new mesh - error `possible cyclic resource inclusion` will appear
### Minimal reproduction project (MRP)
no need
Contributor guide
Research direction
Start in core/io/resource.cpp at Resource::seed_scene_unique_id and Resource::generate_scene_unique_id, then inspect ResourceFormatSaverTextInstance::save in scene/resources/resource_format_text.cpp. Reproduce the editor workflow by creating mesh resources before and after saving editor settings, and verify that newly created resources receive distinct scene IDs without the cyclic resource inclusion error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- game-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100