Since all objects that you create via script are Nodes, you can use
Node duplicate( bool use_instancing=false ) const
It will return a reference to a copy of the Node you are duplicating, so you can just store it on a var.
~EDIT~
As for the data structures, if there is no copy method embedded on it, I think you’d have to create a function yourself. Most of the basics Data Structures have a very straightforward copy mechanism, anyway.
Thank you!
Are they really always nodes also if I don’t write: extends Node ?
This is exactly what I was searching for.
Toger5 | 2016-02-25 12:32
I’m not too sure about this, but I think that when you are creating a script, you have to extend it from somewhere, UNLESS it is a “tool” tagged script (something you’ll use on the Editor).
henriquelalves | 2016-02-26 07:16
you don’t need to use the extends Anything line (I already tried it without). The question is if it extends something as default.
This answer is mostly compiled from the issues @kubecz3k linked.
With str2var(var2str(...)), you can effectively clone any object, at the high cost of speed.
Unfortunately, the following method doesn’t actually work…It would create the array in Copy-on-Write mode in Godot 1.x/2.x, which is rather useless for cloning. Better use @volzhs answer, or the str2var method.
The Array constructor accept a array to clone, so you can just do Array(my_arr). There are similar use for the Dictionary and typed array constructors as well. Unfortunately, this doesn’t work for everything (like custom object classes).
Nodes have a .duplicate method, but it is already covered in another answer.
For some reason, the Array constructor doesn’t work for me. It has the same effect as doing newArr = oldArr.
larvasapiens | 2016-07-10 15:21
Actually, it seems like it was an unintended feature in Godot 2.x. It would actually create an array in Copy-on-Write mode, and it would be fixed in 3.0. So, for now, better clone it by yourself or use @volzhs method.