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 duplication issue with @tool #98912

Open
JekSun97 opened this issue Nov 7, 2024 · 9 comments
Open

Node duplication issue with @tool #98912

JekSun97 opened this issue Nov 7, 2024 · 9 comments

Comments

@JekSun97
Copy link
Contributor

JekSun97 commented Nov 7, 2024

Tested versions

4.2, 4.3 stable

System information

Godot v4.3.stable - Windows 10.0.19045 - GLES3 (Compatibility) - Radeon RX 560 Series (Advanced Micro Devices, Inc.; 31.0.14001.45012) - Intel(R) Core(TM) i5-4570 CPU @ 3.20GHz (4 Threads)

Issue description

I noticed a strange problem when duplicating nodes in the scene tree that were added by a plugin, I first encountered this problem in my plugin developed using GDExtension, now I decided to test this problem locally in the editor itself using GDScript plugins, and this problem repeated.

If we duplicate a node added using GDExtension or a plugin and @tool, then the node is duplicated twice, the more we duplicate this node, the more descendants it has

When reloading a project, extra nodes are sometimes shown in the editor, sometimes not, which is also quite strange.

Steps to reproduce

  1. Open MRP
  2. Create a new node added by the "newnode" plugin
  3. Duplicate it
  4. You will see extra descendants in the output window

Minimal reproduction project (MRP)

bugdublicate.zip

@fire
Copy link
Member

fire commented Nov 7, 2024

I haven't investigated, but if the owner is not set correctly, you'll be essentially making sub-scenes.

@JekSun97
Copy link
Contributor Author

JekSun97 commented Nov 7, 2024

@fire I was told about this, and I tried really different owners, or not using set_owner at all, but it didn't solve the problem

@KoBeWi
Copy link
Member

KoBeWi commented Nov 7, 2024

Removing owner helps. Your problem is that you are adding a new child and set owner to the scene, so it gets saved. Then when you reload, the _ready() code runs again, adding a new child, next to the previous one that was saved in the scene.

Though there is a bug here that when you add a new child with owner, it does not appear in scene tree until you reload the scene. Not sure why.

@fire
Copy link
Member

fire commented Nov 7, 2024

Though there is a bug here that when you add a new child with owner, it does not appear in scene tree until you reload the scene. Not sure why.

@KoBeWi can you report the issue on the tracker here?

@KoBeWi
Copy link
Member

KoBeWi commented Nov 7, 2024

The owner is null inside _ready(), so your spr.set_owner(get_owner()) does nothing.
Apparently there is a difference between adding a node to scene and instantiating a node with scene. In the latter case the owner does exist in _ready().
Not sure if it's even a bug.

@JekSun97
Copy link
Contributor Author

JekSun97 commented Nov 8, 2024

@KoBeWi In the GDExtension plugin I deleted the descendants, but it did not solve the problem :(
Could you please describe what exactly I need to change to fix the duplication of nodes, if I fixed this error, I could continue to develop my plugin.
The C++ code looks like this:

Tree3D::Tree3D() {
	trunk_inst = memnew(MeshInstance3D);
	twig_inst = memnew(MeshInstance3D);	
	this->add_child(trunk_inst);
	this->add_child(twig_inst);
}

void Tree3D::_exit_tree() {
	remove_child(trunk_inst);
	trunk_inst=nullptr;
	if(twig_inst)
	{
	remove_child(twig_inst);
	twig_inst=nullptr;
	}
}

When I opened MRP today I also noticed another strange behavior, when selecting descendants in the node tree, all descendants are selected, and after renaming the editor closes

1108.mp4

@KoBeWi
Copy link
Member

KoBeWi commented Nov 8, 2024

I think #89442 will solve your issue.
Until then you can probably use force_parent_owned(), if it's C++.

When I opened MRP today I also noticed another strange behavior, when selecting descendants in the node tree, all descendants are selected, and after renaming the editor closes

Your nodes have the same name, hence the selection bug.

@JekSun97
Copy link
Contributor Author

I noticed that when building the plugin, an error occurs that the force_parent_owned identifier is not found, my node is inherited from Node3D, but the force_parent_owned method is in the Node class, and Node3D is inherited from Node, what could be the problem?

23

@KoBeWi
Copy link
Member

KoBeWi commented Nov 10, 2024

Maybe GDExtension has no access to unexposed members. In that case you'd have to make a module instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants