Copy objects by value


One of my students tried to copy a object for his project, only to find out that objects seem to be copied by reference. Is it possible to copy an object by value (or otherwise clone it), I can't seem to find this anywhere in the documentation? And now I am curious if this is supported. ;)

The "correct" way of creating new objects is by calling a constructor. Commonly, that means you need to write a copy constructor - a constructor that takes an instance of an object as argument and uses it to initialise all fields. This pattern solves the problem of shalow copy that clone usually creates.


I have a function that clones a class (object).

You create an object that will be the "source" for the second object.

   test = new Test( "Source" )
   (here you give values to the object's fields)
   clone = Test.clone( test )

The "clone" function iterates over all source fields and assigns the value from the source object to the object and target fields with the same names. All fields are rewritten automatically, so you do not need to enter them in the source code

  newObject.x = source.x (you don't have to assign it this way).

This will work for simple types like string and number . The list now needs to be copied differently. A list that can contain other lists and objects is a challenge and it is impossible to create one function for all cases. For example, for a list with objects, to clone it you would need to create the same objects as the original list. Here again there is a problem because these objects may contain lists......

You need to think carefully about the copying process. However, for very simple objects (klass), my method works and can be used as a universal method.

Hi @Martijn75 :)

Yes, objects are references to a collection of different properties. As @Sebastian mentioned you need a function that copies all those references into your new object.

There have been discussion on the Discord (Join & explore, lol).
For example @JmeJuniper created a library that contains a deepClone() function.

  if obj.type !== "Object" then
    return obj
  local retObj = object end
  for key in obj
    retObj[key] = deepClone(obj[key])
  return retObj

newObj = deepClone(obj)   // example use

I am a lazy guy and 'cheated' a helper function this way:

cloneObject = function(source)
  storage.set( "temp", source )
  return storage.get("temp")
// example use:  newObject = cloneObject(oldObject) 

What happens? I 'abuse' the microScript storage functionality. When you save (.set) an object it will be converted into a JSON file. Reading it back it now will be converted into a new (independant) object :)

Live Long and Tinker

SNAP .. and at the same time I post this @Loginus gives you anther option :)

Enough options to choose from, this will certainly do the job. I will forward this topic to my student so he can experiment with it. Thanks and thumbs up for your quick and accurate responses!

Post a reply



Validate your e-mail address to participate in the community