RPG MAKER MV Shuffle Script Problem

Dear javascript guru,


I write these code in common event but the result is weird;


Script:


$gameVariables.setValue(31,[2,3,4,5,6,7,8,9,10,11,12,13,14,15]);
$gameVariables.setValue(9,$gameVariables.value(31));
var j, temp;
for (i = $gameVariables.value(9).length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
temp = $gameVariables.value(9);
$gameVariables.value(9) = $gameVariables.value(9)[j];
$gameVariables.value(9)[j] = temp;
}


 


Then I check the values of variable 31 and 9 in message


Variable 31 is \V[31]


Variable 9 is \V[9]


 


The result is same. I mean the "Variable 31" was shuffled, too.


Please help how to retain original value of "Variable 31" while "Variable 9" was shuffled.
When you say


a = [1,2,3,4,5]


b = a


b is not a duplicate of a, but it points to the same object.  If you add another element or two to a, then look at b, it will also have the new elements.


$gameVariables.setValue(9,$gameVariables.value(31).slice(0));


will put a copy of the array into the second variable.
Thank you very much, Shaz.
did you read my post?  You seem to have ignored it completely.  Or you just double posted, which is against the forum rules (if you need to make a change/addition, edit your post instead of posting immediately under your last post).
Sorry Shaz, the 2nd post is delivered next to 1st continuously (without refresh the browser). I'll delete it after this post.
I assumed you just posted without seeing that I'd replied 🙂   They were pretty close together in time.


Hope it worked for you without any further problems.
It works pretty cool. Thanks again.

Original thread: https://forums.rpgmakerweb.com/threads/rpg-maker-mv-shuffle-script-problem.71386/