Deep copy any object

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By supaiku
:warning: Old Version Published before Godot 3 was released.


I need to create a deep copy of any object. I’m already helped if I could somehow call the constructor of a class from a given object to create new instances from it.

How do I do that?

For Node types you can use .duplicate(). I can’t help on the «any object» part :frowning:

Not_a_Robot | 2016-07-17 21:51

I would have get the type of the object, but typeof() returns an int (wtf).
Then I would get the class name of the object with get_type(), but it returns a String (wtf) with which not much can be done AFAIK.
Then I would use dict2inst(inst2dict(obj)), however it looks weird and sometimes it doesn’t works if the object contains references.
Then… I would wait for a dev answer, or create an issue on Github :stuck_out_tongue:

Zylann | 2016-07-18 00:08

@Not_a_Robot Yeah. Unfortunately, my objects don’t inherit Node…

@Zylann I played a bit with it, but couldn’t produce anything helpful.

supaiku | 2016-07-18 08:10

@Zylann: Read the docs: @GDScript — Godot Engine (latest) documentation in English :slight_smile:

Akien | 2016-07-18 08:44

@Akien yeah, typeof(), I know that. But I don’t see any function that can create an instance from this int. I wrote “wtf” because in any other language I use, typeof usually returns a class object wether the type is a primitive or a class, and from which we can get a name, create instances or whatever.

Zylann | 2016-07-18 18:26