MV How to copy an object from an array?

I have a .js file, containing some arrays with data I need for my game.

One of those arrays contains the coordinates of objects in my game. I will use this array as an example, but I have the same problem will all arrays from this file. (Or any file, I guess, but this is the first time I encounter this problem.)

Spoiler: JavaScript 1: Array
JavaScript:
window.CMVSYS || (window.CMVSYS = {});
(function() {
    const params        = PluginManager.parameters('JDN_Cards_MV_system');console.log(params);
    const pos    = ['NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL',
        {id: 21, x:  936, y: 816, scale: 100.0, type: 'Face'},
        {id: 22, x:  744, y: 816, scale: 100.0, type: 'Weapon'},
        {id: 23, x: 1128, y: 816, scale: 100.0, type: 'Armor'},
        {id: 24, x:  264, y: 624, scale:  38.0, type: 'CardBack'},
        {id: 25, x: 1608, y: 624, scale:  38.0, type: 'CardBack'},
        {id: 26, x:  552, y: 624, scale:  38.0, type: 'Minion'},
        {id: 27, x:  744, y: 624, scale:  38.0, type: 'Minion'},
        {id: 28, x:  936, y: 624, scale:  38.0, type: 'Minion'},
        {id: 29, x: 1128, y: 624, scale:  38.0, type: 'Minion'},
        {id: 30, x: 1320, y: 624, scale:  38.0, type: 'Minion'},
        {id: 31, x:  480, y: 984, scale:  25.5, type: 'Card'},
        {id: 32, x:  576, y: 984, scale:  25.5, type: 'Card'},
        {id: 33, x:  672, y: 984, scale:  25.5, type: 'Card'},
        {id: 34, x:  768, y: 984, scale:  25.5, type: 'Card'},
        {id: 35, x:  864, y: 984, scale:  25.5, type: 'Card'},
        {id: 36, x: 1008, y: 984, scale:  25.5, type: 'Card'},
        {id: 37, x: 1104, y: 984, scale:  25.5, type: 'Card'},
        {id: 38, x: 1200, y: 984, scale:  25.5, type: 'Card'},
        {id: 39, x: 1296, y: 984, scale:  25.5, type: 'Card'},
        {id: 40, x: 1392, y: 984, scale:  25.5, type: 'Card'},
        {id: 41, x:  936, y: 240, scale: 100.0, type: 'Face'},
        {id: 42, x:  744, y: 240, scale: 100.0, type: 'Weapon'},
        {id: 43, x: 1128, y: 240, scale: 100.0, type: 'Armor'},
        {id: 44, x:  264, y: 432, scale:  38.0, type: 'CardBack'},
        {id: 45, x: 1608, y: 432, scale:  38.0, type: 'CardBack'},
        {id: 46, x:  552, y: 432, scale:  38.0, type: 'Minion'},
        {id: 47, x:  744, y: 432, scale:  38.0, type: 'Minion'},
        {id: 48, x:  936, y: 432, scale:  38.0, type: 'Minion'},
        {id: 49, x: 1128, y: 432, scale:  38.0, type: 'Minion'},
        {id: 50, x: 1320, y: 432, scale:  38.0, type: 'Minion'},
        {id: 51, x:  480, y:  72, scale:  25.5, type: 'Card'},
        {id: 52, x:  576, y:  72, scale:  25.5, type: 'Card'},
        {id: 53, x:  672, y:  72, scale:  25.5, type: 'Card'},
        {id: 54, x:  768, y:  72, scale:  25.5, type: 'Card'},
        {id: 55, x:  864, y:  72, scale:  25.5, type: 'Card'},
        {id: 56, x: 1008, y:  72, scale:  25.5, type: 'Card'},
        {id: 57, x: 1104, y:  72, scale:  25.5, type: 'Card'},
        {id: 58, x: 1200, y:  72, scale:  25.5, type: 'Card'},
        {id: 59, x: 1296, y:  72, scale:  25.5, type: 'Card'},
        {id: 60, x: 1392, y:  72, scale:  25.5, type: 'Card'}
    ]
    Object.defineProperties(window.CMVSYS , {
        pos:       {get: function() { return pos;}},
    });
})();

Note: This plugin contains more arrays like this, for different kinds of data. The problem is the same for all arrays, but I left them out for the sake of keeping it simple.

This array contains objects, of which 1 parameter is "type"

I like to use copies of those entries, to use during a game, but without altering the original object.

However, when I try to change the copy, the original object changes as well. Even then I use .slice().
Spoiler: JavaScript 2: Script Call
Code:
var posSlice = CMVSYS.pos.slice();
$gameVariables.setValue(1, posSlice[21]);
$gameVariables.value(1).type = 'Card';
$gameVariables.setValue(2, $gameVariables.value(1).type);
$gameVariables.setValue(3, CMVSYS.pos[21].type);

As you can see in the array, the type of position 21 is originally 'Face'. The script call makes a copy of this object and changes the type of this copy to 'Card'. I expected the original object still to have 'Face' as type. However, the outcome looks like this:
1732661054020.png

1732661094583.png


I would expect the [object Object], and the first Card, but the second Card should have been Face.

So, the first thing I tried was to add .slice()but that did nothing. I hoped it would reset the original value when the script call is completed, and I would call upon the array again, but I still get the updated output.

Just to make sure, I also tried not changing the copy at all, which would show me the expected 'Face' in both v[2] and v[3].

What am I doing wrong? Isn't this exactly what .slice() should prevent?
Why wouldn't you declare a new variable as an index in the array, then push that variable into the array?
Why wouldn't you declare a new variable as an index in the array, then push that variable into the array?
I would... If I understood. I'm a total noob for javascript.

A new variables as an index and push that into the array? Could you maybe make me a little example? (I feel like such a noob when it comes to javascript)

Well, what I maybe should have mentioned, is that the copy of this objects will be pushed into another temporary array, which will be reset at the start of each game.

That would have been my next step. And after your reply, I wonder if pushing this into another array, (stored in a variable in MV) would solve this. I'm going to try this out right now.
Something like
Code:
let copy=array[i];

array.push(copy);


Or


array2.push(copy);
If you want it in a second array.
i is the index in the array of the element you want to copy


Edit: sorry the bb code messed it up
The simplest way to make a copy of an object (or an array) that's guaranteed to not affect the original in any way is JsonEx.makeDeepCopy(objectHere).
Ninja'd by Mac but I may as well post it~

Objects are passed by reference, you need to explicitly clone them to get an independent copy.
  • Shallow copy: Object.assign({}, yourObject)
    This returns a new object with independent top-level properties.

  • Deep copy: JsonEx.makeDeepCopy(yourObject)
    This returns a new object with completely independent properties.
E.g.
  • For { x: 1, y: 2 }, shallow copy is sufficient because there are no object properties.

  • For { test: { cake: true } }, shallow copy will create a new object, but the test will still refer to the original object's test property. A deep copy will create an object with an independent test property.
(This next part in the spoiler probably doesn't apply to your case.)
Spoiler: technical caveat re makeDeepCopy
MV/Z's makeDeepCopy method skips some non-stringifiable properties, because it works by recursively converting the object into a string and back. It's used for save data! Arrays and normal objects are fine, but stuff like Functions, Maps, Sets, etc won't be present on the clone. Also, any custom object with a non-global constructor will be converted into a generic object.
I just tried this:
1732663091614.png

And the outcome was still the same! 😛

I didn't see your reply till now, so I am going to try your method now.
1732663472543.png


Great! This works! Thank you so much!
1732663446951.png


The simplest way to make a copy of an object (or an array) that's guaranteed to not affect the original in any way is JsonEx.makeDeepCopy(objectHere).
Ninja'd by Mac but I may as well post it~

Objects are passed by reference, you need to explicitly clone them to get an independent copy.
  • Shallow copy: Object.assign({}, yourObject)
    This returns a new object with independent top-level properties.

  • Deep copy: JsonEx.makeDeepCopy(yourObject)
    This returns a new object with completely independent properties.
E.g.
  • For { x: 1, y: 2 }, shallow copy is sufficient because there are no object properties.

  • For { test: { cake: true } }, shallow copy will create a new object, but the test will still refer to the original object's test property. A deep copy will create an object with an independent test property.
(This next part in the spoiler probably doesn't apply to your case.)
Spoiler: technical caveat re makeDeepCopy
MV/Z's makeDeepCopy method skips some non-stringifiable properties, because it works by recursively converting the object into a string and back. It's used for save data! Arrays and normal objects are fine, but stuff like Functions, Maps, Sets, etc won't be present on the clone. Also, any custom object with a non-global constructor will be converted into a generic object.

Awesome! I feel like this will be important when I copy over a complete deck of cards (so it can be reused the next game)

The specific problem above, @AquaEcho 's solution using let instead of var seems to work. Is it still better to make a deep copy?


Thank you all, this helps a lot, not just for now, but also future projects.
The specific problem above, @AquaEcho 's solution using letinstead of var seems to work. Is it still better to make a deep copy?
The advantage of a deep copy is that is it rather foolproof (well, as long as you only copy data-like objects, as pointed out by @caethyril above). But if this solution works fine for you then there's no reason not to use it. Just keep in mind that both arrays are still pointing to the same objects - so if you ever modify one of the objects in the second one (e.g. change the x and y values), it will also change in the first one.
The advantage of a deep copy is that is it rather foolproof (well, as long as you only copy data-like objects, as pointed out by @caethyril above). But if this solution works fine for you then there's no reason not to use it. Just keep in mind that both arrays are still pointing to the same objects - so if you ever modify one of the objects in the second one (e.g. change the x and y values), it will also change in the first one.
Okay, that is something I need to prevent. That would mean that if 1 card loves health, all copies and copies in the future, would lose health too.

So I would need to use the deepcopy after all. I need each copy to be their own copy and not be tied to to original or any of the other copies at all.

Thanks for your help. I'll continue my journey tomorrow. It's napping time for me.
JsonEx.makeDeepCopy() will work for your needs as stated.

If you have a newer nwjs/chromium version and need another method, you can use structuredClone():
JavaScript:
const posSlice = structuredClone(CMVSYS.pos);

Some limitations:
StructuredClone Algorithm
I made a mistake in my earlier command...

The code that seemed to solve the issue:
Code:
let copy = CMVSYS.pos[21];
$gameVariables.value(63).push(copy);
$gameVariables.value(63).type = 'Card';
$gameVariables.setValue(2, $gameVariables.value(63).type);
$gameVariables.setValue(3, CMVSYS.pos[21].type);

What the code should look like:
Code:
let copy = CMVSYS.pos[21];
$gameVariables.value(63).push(copy);
$gameVariables.value(63)[0].type = 'Card';
$gameVariables.setValue(2, $gameVariables.value(63)[0].type);
$gameVariables.setValue(3, CMVSYS.pos[21].type);

Can you see what I did wrong in the first try?

I forgot to put the [0] behind the array.
When I do put the [0] behind the array, the problem is not solved at all.

@AquaEcho 's solution did not work after all.

So, I am going to try the deep copy.

1732754031283.png


I changed the code to:
Code:
let copy = JsonEx.makeDeepCopy(CMVSYS.pos[21]);
$gameVariables.value(63).push(copy);
$gameVariables.value(63)[0].type = 'Card';
$gameVariables.setValue(2, $gameVariables.value(63)[0].type);
$gameVariables.setValue(3, CMVSYS.pos[21].type);

This works.

Just to make one thing clear: This code is not part of the game, this is me figuring out how this all works, so I can use it right in my game.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-copy-an-object-from-an-array.173474/