Duplicating a Node in Editor references original Node
Tested versions
4.2.1 Mono "Godot Engine v4.2.1.stable.mono.official [b09f793f5]"
System information
Windows 10 64 bit. Forward +. Godot 4.2.1 Mono from godot website
Issue description
When I duplicate a node that is a "prefab" node with functionality referenced in itself, the duplicate references the original node.
I didn't test GD Script.
Steps to reproduce
- Create a node scene.
- Add a script and export reference a node in the scene.
- create a new scene to put the first node in.
- duplicate the node
- notice the duplicate references the original.
Example of how to reproduce:
Create a new UI scene.
Add a label to the scene and give it the text "foobar"
Add a button to the scene.
Add a script to the root node.
Give the root node the following C# code:
[Export] Label myLabel; public void ChangeLabelText(){ myLabel.text = "Changed this node." }
Connect the Button to the function "ChangeLabelText"
Create a new scene. Drag the "prefab" into this scene. Click this instance, and press "Control + D" to duplicate. Duplicate as many times as you want.
Run current scene. (F6 hotkey on windows)
Click the button on a duplicate, and it changes the text of the "original" and not the duplicate as expected.
If you click the original, it still changes the text of the original as expected.
Minimal reproduction project (MRP)
Just basic Godot nodes. See "Steps to reproduce" for information.
Seems missing "set My Label to reference the prefab node itself" in the steps?
~~If that's the case, the duplicated node is expected to still referencing the original node. The value of myLabel is a certain node. Setting it to self is not different from others.~~ Update: No, the issue is about instantiated scene setting a property's default value to a node that it owns.
Regardless of whether this is a bug or intended behavior, I find this very unintuitive. If I duplicate a node that holds references to its own children nodes, I would not expect those references to still be of the original node's children.
Can confirm and reproduce with your steps.
Additionally, in my tests, I found the following:
In Step 3, after instancing the scene once, the exported field is already marked as "changed" and displays the revert-button next to it:
Then, when duplicating that node, a relative path pointing to the first node is used in the field:
There is a workaround to this problem, which makes me firm in my suspicion that this is indeed a bug:
- Before duplicating the node, click the "Revert"-Button next to the exported field on the first instance.
- Duplicate the node. You will observe that both the original and the duplicate do not show the "revert" button, and both fields will reference to their own child node
This workaround, however, is extreme error prone. It's easy to forget to revert all fields when instancing a scene. For this reason, I currently avoid exposing node fields on scene roots.
Oh, I see what you mean now. I thought setting the property happens from outside the scene.
This is indeed a bug of node exports. Exporting a NodePath works fine in this case.
I think it's fixed in 4.3.