#how to clone a node/scene with all of the members intact?

11 messages · Page 1 of 1 (latest)

ivory fable
#

my cloning function

func clone()->BaseCard:
    var pack = PackedScene.new()
    pack.pack(self)
    return pack.instantiate()
#

how to clone a node/scene with all of the members intact?

ivory fable
#

i ended up just manually reassigning them to the clone, i also tried using get_property_list() but it's also getting other properties I don't want to assign

desert umbra
#

Do you want the members as they exist on disk or the members as they exist now at runtime?

#

(it looks like you're snapshotting now-at-runtime)

ivory fable
#

sorry, the solution with packed scene is working, some of the values are lost due to having a ready check in setters i made,

using duplicate does not clone the resources used, have some unintened effects.

#

i also have to reassign some reference types, doesn't seem like there is a easy way to clone those reference, so i have a clone function in those as well

ivory fable
#

some object also can't have a _init() with paramenters, due to duplicate() and instantiate() not having an option to parse arguments

#

so if the object has a _init() with parameter, the node instantiated or duplicated won't have a script attached

#

so i had to create a custom constructor
for example

class_name Person extends RefCounted
var name : String
var age : int

//_init must not use parameters
func _init()->void: pass

//custom constructor
func create(_name, _age)->RefCounted:
  var obj = get_script().new()
  obj.name = _name
  obj.age = _age
  return obj

edit: switched to extending RefCounted from Node because Node is not garbage collected

#

for the clone function

func clone()->Person:
  var clone = create(name, age)
  return clone

to clone

var john = Person.new().create("John", 50)
var johnClone = john.clone()

anyway, this is the solution i came up with for my project, the Person.new().create("John", 50) line is not the best looking line of code