Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node.duplicate() creates extra nodes under certain conditions #76593

Closed
radlotus opened this issue Apr 29, 2023 · 3 comments · Fixed by #68560
Closed

Node.duplicate() creates extra nodes under certain conditions #76593

radlotus opened this issue Apr 29, 2023 · 3 comments · Fixed by #68560

Comments

@radlotus
Copy link

radlotus commented Apr 29, 2023

Godot version

3.6 beta 1 and 4.0.3 RC1

Issue description

Node.duplicate() doubles the children nodes under certain conditions.
It doesn't occur otherwise.

Steps to reproduce

  1. Take a node that has some children.
  2. Pack this node and its children into a PackedScene
  3. Save the PackedScene
  4. Load the PackedScene and instantiate it
  5. Set the owner property of the instance's children to null
  6. Duplicate the node

Minimal reproduction project

node_duplicate_creates_extra_nodes_godot3.zip
node_duplicate_creates_extra_nodes_godot4.zip

@radlotus
Copy link
Author

radlotus commented Apr 29, 2023

Some notes:

  • this only occurs if the PackedScene instance was loaded using load() or ResourceLoader.load().
  • the doubling only occurs on children that had their owner property changed from the initial value
  • this occurs only if DUPLICATE_USE_INSTANCING is passed to Node.duplicate() which is the default

Also DUPLICATE_USE_INSTANCING is documented as such:
An instance stays linked to the original so when the original changes, the instance changes too.

This doesn't really work at all. Is it broken?

@KoBeWi
Copy link
Member

KoBeWi commented Apr 30, 2023

That's a weird description. What DUPLICATE_USE_INSTANCING does is it calls instantiate() on the original PackedScene if it's available. This causes the scene to be re-instantiated.

When you duplicate a scene instance, its children are not duplicated. Instead, the scene is re-instantiated with new children. Whether a node belongs to scene depends on its owner property, so when you remove owner, the nodes are no longer part of the scene and get duplicated normally. Thus your duplicate includes the unowned nodes and the nodes from re-instantiation.

tl;dr this is not a bug.
But maybe the documentation can be improved somewhere.

@Mickeon
Copy link
Contributor

Mickeon commented Sep 17, 2023

This would be documented by #68560.
"Duplicate using PackedScene.instantiate. If the node comes from a scene saved on disk, re-uses PackedScene.instantiate as the base for the duplicated node and its children.". (direct link)

@akien-mga akien-mga added this to the 4.3 milestone Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants