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.)
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
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:
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
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
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.)
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().
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:
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?