godotengine / godotengine/godot
[3.x] load() duplicating contents of PackedScene
- Dominant language
- C++
- Stars
- 117k
- Forks
- 26.8k
- PR merge metrics
- PR metrics pending
Description
### Tested versions
Tested in v3.6.stable.official [de2f0f147]
### System information
Linux Mint 22.1 Xia Xfce - Godot 3.6 stable - Apple MacBook Pro (late 2012) - Intel Core i5-3210M
### Issue description
When using `load()` to open an `.scn` file as a `PackedScene` during runtime, I am coming across unexpected behaviour. The `PackedScene` can be instanced normally, but all the children of the `PackedScene` are duplicated. The duplicates have exactly the same names (I didn't think this was possible) but appear to have all their private variables set to default. For example, the duplicate `polygon` has an empty array for its `data` variable.
The method `get_node()` appears to return the duplicate child, instead of the original. This means I cannot access the original children without first freeing the duplicates.
I can't find any explanation of this online, so I think it is a bug.
The script of interest is simply:
```gdscript
extends Node2D
func _ready():
$FileDialog.popup()
func _on_FileDialog_file_selected(path):
var new = load(path).instance()
add_child(new)
```
The `.scn` file has been generated in my game as part of a save-game system. This is the tree of the `.scn` file when opened as a scene in the editor.

This is the remote tree once the `.scn` file has been opened during runtime.

### Steps to reproduce
1. Open project
2. Open `test_save.scn` as a scene and inspect the scene tree.
3. Run project (the main scene is `Node2D.tscn`)
4. Load `test_save.scn` through the file dialog.
5. Pause game.
6. Go to editor and look at the remote scene tree.
### Minimal reproduction project (MRP)
I was unable to replicate the bug when starting with a fresh project, so the MRP is based on a stripped-down version of my game. Most of the scripts and scenes are included in `test_save.scn`, so have been left in so it can be opened without throwing lots of errors. You can ignore these. The script of interest is `mrp.gd`, which is run in `Node2D.tscn`.
[github_mrp.zip](https://github.com/user-attachments/files/19982838/github_mrp.zip)
Contributor guide
Research direction
Start with the provided MRP, especially mrp.gd and Node2D.tscn, then inspect test_save.scn and the runtime path using load(path).instance(). Run the listed reproduction steps and compare the editor scene tree with the remote tree; done means loading the PackedScene no longer creates duplicate children and get_node() reaches the intended originals.
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
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100