How to make a deep copy of a property of an instance of an object?

Godot Version

Godot 4.2.2

Question

Hello !

I’m becoming mad on something that seems so simple.

I need to do a deep copy of a property of an object.

To summarize, I made this little example :

Thing.gd

extends Object
var param = 100

Main.gd

extends Object
@export var thing_scene = preload(“res://Thing.tscn”)
var thing
var copy

func _ready():
thing = thing_scene.instantiate()
thing.param = 200
copy = thing.duplicate()
print("thing param : ",thing.param)
print("copy param : ",copy.param)

And the result is :

thing param : 200
copy param : 100

I always get the default value of the property but not the actual value.

Other things I tried :

If I try copy = thing.duplicate(true) I get "Invalid get index ‘param’ ". (sad, the documentation says that’s the way to make a deep copy)

If I try thing.param.duplicate() I get “Invalid call. Nonexistent function ‘duplicate’ in base ‘int’.”

And I didn’t found any other way to duplicate than the duplicate() method.

If that matter, in my game, it’s the direction (so a Vector2) of a CharacterBody2D that I try to duplicate because I have to register the direction of an object before it changes to adapt the direction of other objects.

Please, how can I deep copy a property ?

Thank you for your reading !

yes you cant, unless you pass the value from the duplicated scene to new dups after it done duplicated

this only works for Array and Dictionary

1 Like

Okay ! I just tried, I instantiate a new object, copy the values and send the new object.

Not gonna lie, that’s kinda ugly, but, eh, it works !

Thank you so much. I don’t know how I could have found this information alone ! Have a great day ! =D