I’m trying to duplicate a node and its children (including scripts and variable states), but the script variable values always reset after duplication. My node structure:
main (Control, script main.gd)
└── Control (named “control”)
└── a (TextureRect, script a.gd)
└── b (Label, script b.gd)
Thats by design, you are creating another copy in code. Is there a reason why you are doing that and not through the editor? If you do it through the editor then the variable states will be preserved. (unless you make them unique)
When I started using Godot, coming from Unity, I wanted to be able to duplicate nodes just like in Unity, where I’d use Instantiate() on GameObjects. Learned the hard way that that’s definitely not the Godot way, and that you should make scenes out of the nodes you want to duplicate.
Only recently I learned how to do this per code for runtime use, so at least it’s possible to instantly create a PackedScene and instantiate it, without filling up my project with lots of small scenes.
I’m not sure why, but the structure I sent earlier isn’t working as expected, while the one below is correct.
Main (Control, script: main.gd)
= Control (named “control”)
== a (TextureRect, script: a.gd)
=== b (Label, script: b.gd)
In fact, this is a simple example—I’ve already saved its original version as a scene. And why am I doing this? Because in the game, I have many such scenes that I modify during runtime, and sometimes I need copies of them (including the modifications made during runtime). I don’t want to manually assign values to potentially modified variables, as there are simply too many and it’s quite difficult.
So I asked an AI for help and was told to use duplicate(), but during actual runtime, I found that duplicate()doesn’t work as expected.
Thank you for your reply, but creating a PackedScene and instantiating it isn’t what I need—it would only give me an unmodified original version. What I actually require is a copy that retains the changes made during runtime.
The code you posted is TBH unclear code. You shouldn’t be naming variables a and b. We have no idea what you’re actually trying to do because you’re either obfuscating your example, or it’s all in your head and you’re expecting us to be in there with you to understand what you’re doing.
Your responses to people’s recommendations so far demonstrate that you haven’t explained enough about what you’re doing and what you’ve tried. If you have already tried instantiating objects and that didn’t work, then that information should be in your initial post so you don’t waste people’s time trying to help you.
LLMs lie. Straight up. They will tell you to do things that won’t work with Godot. They will lie about functions existing.
This is my interpretation of what I’ve read so far:
You had a goal.
You decided the solution was to duplicate in-game objects.
You asked AI for help.
You got an incorrect answer.
You decided that the duplicate() function didn’t do what you wanted.
You asked for help with the code the AI wrote for you.
I would recommend you back up to step one and tell us specifically what you are trying to accomplish. Give us some context to work with. Tell us the problem you’re trying to solve - not the solution you came up with.
That definitely is the Godot way and is pretty much the same as Unity (other than the implementation is slightly different with Scenes , but in the end its pretty much the same).
But if you create a copy of the scene in the editor add the desired information to the variable and save it, and then instantiate scene that you absolutely will get a copy with the variable information intact. That’s the whole point of scenes.
You don’t understand what I’m saying. I know what prefabs and scenes are. I’m talking about duplicating GameObjects within a scene in Unity, and doing the same in Godot, duplicating nodes within the tree. You can do that in Unity just fine, but not so in Godot.
Just so we are clear, a ‘GameObject’ is in effect just a class for both GD script & c#) .
A scene is a collection of them (and other components) , with all the preset information you want in them.
If you want to have a class with variables that have an preexisting information in them, or pass information to them (which I think is what you are trying to achieve) then look at this:
And if a packed scene was duplicated that had a game object inside of it, which in turn has some predefined information in it that was set via the editor then that information would be duplicated as well.
If you duplicate a class (in code) and the original doesn’t have predefined information in and create a copy of that then neither will the duplicate, thats exactly how Unity works as well. Along with every other class based programming language.
You are right, I see what you mean now. Upon attempting to duplicate (using PackedScene or duplicate()), I noticed that all variables with @export correctly retained, whereas those without @export in their initial state.