Hi guys,
I'm having a real headache when duplicating objects.
I have an object called "Item". At a certain point in my game I duplicate the node via Duplicate()

public virtual Item Clone(NPC npc)
{
     var clonedItem = Duplicate() as Item;
     npc.AddChild(clonedItem);
     npc.RemoveChild(clonedItem);
     return clonedItem;
}

I quickly add the new item to the scene tree and then remove it again so that the _Ready() method is actually executed.
That seems to work. However, every item has a child that displays UI elements.
The problem: After cloning, the background texture and text label of the original item are ALSO displayed where the new item is.
The label of the new item is not displayed at all.

Both in the editor tree and via the debugger in the code, it seems as if the UI elements are also separate objects.
Could it have something to do with the fact that the label and children are displayed in a subviewport? But according to the inspector, these are also independent objects.

_By the way: I have the strong feeling that AddChild followed by RemoveChild is a very bad style for clean cloning... 😃 But I don't know how else to get a clean execution of Ready().

Can anyone help me here? I'm totally lost.

  • xyz replied to this.

    Mauschelbaer Why do you need to execute _ready() if you don't intend to keep the node in the tree?
    You can check the remote tree at runtime to see the actual node situation.

    Btw it's not clear from your scene tree screenshot to which node it the script attached.

    why do you add and remove if you dont even use it?

    I do this because I save the object in an NPC dictionary. This represents its inventory, so to speak. Later, an NPC can add it back into the scene, as shown in the screenshot.

    And the screenshot of the tree was created from the "remote"/ runtime view in the editor. BigGlas7 is the item in this case. This also has the script.

    I have just noticed that the problem always occurs when I have an object that has the following structure:

    ItemA (RidgedBody3D with script Item attached)
    |__Sprite3D
        |__SubViewport
            |__Label
    
    ItemB (clone)
    |__Sprite3D
        |__SubViewport
            |__Label

    The changes to the label are visible in the code (via GD.Print) and also in the editor at runtime in the Inspector. Only the display does not work. The subviewport of item B (the clone) cannot be seen. Instead, this subviewport also shows that of item A.

    It looks as if both subviewports are now displaying the same generated texture. As if they were sharing the resource.

    • xyz replied to this.

      Mauschelbaer You probably need to update any references to child nodes in the duplicate (viewport texture is probably the culprit here), and also duplicate any resources that may be shared between the duplicate hierarchies, if you want them to be unique per duplicate.
      Instead of duplicating nodes, it'd be much better if you just instantiate the Item scene. This will also automatically duplicate any resources that have local_to_scene flag set.

      Can you make a minimal reproduction project and attach it here?

      5 days later

      Ah OK! So I actually solved it exactly like that. I didn't duplicate it and manually re-instanced the scene. Then I adopted the values.

      To make it work generically, I saved the path to the scene in a field for each object so that I could create replications.

      Thanks for the help!