godotengine / godotengine/godot
Duplicate orphan created when loading packedscene with dictionary/arrays storing node references
- Dominant language
- C++
- Stars
- 117k
- Forks
- 26.8k
- PR merge metrics
- PR metrics pending
Description
### Tested versions
tested in: v4.2.stable.official [46dc27791]
### System information
Godot v4.2.stable - Windows 10.0.22631 - GLES3 (Compatibility) - Intel(R) Iris(R) Plus Graphics (Intel Corporation; 27.20.100.9664) - Intel(R) Core(TM) i7-1065G7 CPU @ 1.30GHz (8 Threads)
### Issue description
When saving and loading with PackedScene, it is possible to preserve node reference in the form of variables, but attempting to do the same with node references stored in a Dictionary or Array results in unusual behavior:
- when the packed scene is loaded, the referenced node will be created and placed as expected
- however, references in dictionaries and arrays will not point to that node
- the references point instead to duplicates of that node which are orphans
Code of the node storing the references:
```
extends Sprite2D
@export var my_gamma : Node2D
@export var my_gamma_dict : Dictionary = {0 : null}
@export var my_gamma_array : Array = [null]
func _on_button_print_gamma_pressed():
#var g = my_gamma as Node2D ## this will correctly associate my_gamma with the loaded gamma node
#var g = my_gamma_dict[0] as Node2D ## this creates an orphaned gamma and another gamma visible in game
var g = my_gamma_array[0] as Node2D ## same as above
g.visible = not g.visible
print("g.get_path() = ", g.get_path())
```
### Steps to reproduce
Open minimal project, run:
1. click the button "spawn"
2. press "save"
3. close and reopen the game
4. press "load".
After this, duplicate orphans would have been created.
You can check the path of the "gamma" objects by pressing on the "print gamma" buttons. You can also modify the code in alpha.gd and beta.gd to try storing references to gamma in other ways
### Minimal reproduction project (MRP)
[packedscene save test.zip](https://github.com/godotengine/godot/files/14489598/packedscene.save.test.zip)
Contributor guide
Research direction
Start by opening the linked minimal reproduction project and reproducing the save/load sequence with the Dictionary and Array references. Trace the PackedScene serialization and loading path for node references; done means those references resolve to the loaded node rather than duplicate orphan nodes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- godot
- Domain
- game-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100