Array assigning itself where it isn't wanted?

I'm using arrays to create a random chest generation system. The idea is that the base array looks like this:

Code:
// Create Array
tmpArr = [false, 6, false, 6, false, 6, false, 6, false, 6, false, 6, false, 6];
// chest 1, loc, 2, loc, 3, loc, 4, loc, 5, loc, Ext1, loc, Ext2, loc

// Save Array to Game Variables
$gameVariables.setValue(46, tmpArr);
$gameVariables.setValue(47, tmpArr);
$gameVariables.setValue(48, tmpArr);
$gameVariables.setValue(49, tmpArr);
$gameVariables.setValue(50, tmpArr);
// what in the hell is this wizardry

So, this is called first, and sets whether or not they've been opened, and their location. Each chest has 5 possible spawn points. Then, each zone is assigned a copy of this array, so that each one has its own.. treasure map, so to speak. This is the blank map. After this, we generate the map further:

upload_2018-5-18_21-35-29.png


Here, we set a trash variable to 0, and 5 important variables to a random number 1-5. Then, we check what number the trash variable is at. For 0, we do this:

Code:
array = $gameVariables.value(46);

array[0] = $gameSwitches.value(56);
array[2] = $gameSwitches.value(57);
array[4] = $gameSwitches.value(58);
array[6] = $gameSwitches.value(59);
array[8] = $gameSwitches.value(60);
array[10] = $gameSwitches.value(61);
array[12] = $gameSwitches.value(62);
// this is black magic
// this is called to save to array
array = $gameVariables.value(46);

array[1] = $gameVariables.value(51);
array[3] = $gameVariables.value(52);
array[5] = $gameVariables.value(53);
array[7] = $gameVariables.value(54);
array[9] = $gameVariables.value(55);
array[11] = $gameVariables.value(56);
array[13] = $gameVariables.value(57);

// saves loc, split because limited

// length allowed in this window

So, this assigns the zone attached to variable 46 a set of locations for its chests, and ensures that the switches are off for it. Then, we add 1 to the trash var, and repeat this loop for the other zones. But, there's an issue.

For whatever reason, the above snippet is completely and utterly assigning itself overtime duty and attaching itself to everything in sight.

upload_2018-5-18_21-40-3.png


The first image is a check done right after the first bit, zeroing-out the arrays, making them blank.
The second image is after the code snippet above. I've tried removing any references to other variables in that event, but regardless it just.. does this. I can't for the life of me fathom why it's assigning itself to these variables.
Where do you have the snippet, and how are you calling it?

You may need to setValue(..., tmpArr.clone()) otherwise they may all point to the same memory location, and not duplicate the array (changing one variable will change them all)

Do you realise that using Control Variables to set the range 51-55 to a random number between 1 and 5 is going to set them ALL to the SAME random number? It's not going to calculate a new random number for each one.
Excuse the poorly cropped images, but the map has an autorun event that calls two common events, these control the arrays:

upload_2018-5-19_5-50-44.png

upload_2018-5-19_5-49-57.png

upload_2018-5-19_5-51-37.png


Snippet at the bottom of the final image is:
Code:
array = $gameVariables.value(46);

array[0] = $gameSwitches.value(56);
array[2] = $gameSwitches.value(57);
array[4] = $gameSwitches.value(58);
array[6] = $gameSwitches.value(59);
array[8] = $gameSwitches.value(60);
array[10] = $gameSwitches.value(61);
array[12] = $gameSwitches.value(62);
// this is black magic
// this is called to save to array
array = $gameVariables.value(46);

array[1] = $gameVariables.value(51);
array[3] = $gameVariables.value(52);
array[5] = $gameVariables.value(53);
array[7] = $gameVariables.value(54);
array[9] = $gameVariables.value(55);
array[11] = $gameVariables.value(56);
array[13] = $gameVariables.value(57);

// saves loc, split because limited

// length allowed in this window

Also, it doesn't generate the same number.
upload_2018-5-19_5-53-50.png


I don't understand what you mean by this:
and not duplicate the array (changing one variable will change them all)

When I zero out the arrays I'm using a different variable than when I generate the random numbers. I use tmpArr for ZeroOut, and array for RandGen.
Are you saying that the issue lies with how I'm initially setting the arrays? I've tried removing the ZeroOut common event, but the end result is still the same. Unless, you're saying to change the code to be like:

Code:
array = $gameVariables.value(46);

setValue($gameSwitches.value(56), array.clone[0]());

Sorry if I'm misunderstanding you here. I generally avoid code for this reason lol.
Double-post because a solution was found.
Changing stuff to go:
Code:
// Create Array
tmpArr = [false, 6, false, 6, false, 6, false, 6, false, 6, false, 6, false, 6];
// chest 1, loc, 2, loc, 3, loc, 4, loc, 5, loc, Ext1, loc, Ext2, loc

// Save Array to Game Variables
$gameVariables.setValue(46, tmpArr.clone());
$gameVariables.setValue(47, tmpArr.clone());
$gameVariables.setValue(48, tmpArr.clone());
$gameVariables.setValue(49, tmpArr.clone());
$gameVariables.setValue(50, tmpArr.clone());

Changed nothing, but it looks cooler so I kept it.

The change that worked was saving the arrays after setting them. Why? No clue. Don't care. Works now, not my problem.
There was an impression that there were many arrays but there was only ever one. A wizard was building a citadel but was a cheapskate and built only one toilet/bathroom and conjured up doors for each floor that all led to the one toilet. Zero Out Arrays did the same thing.

The clone() method creates doors that lead to unique toilets, so to speak.
clock out where were ur analogies when I needed them
ur my man now

Original thread: https://forums.rpgmakerweb.com/threads/array-assigning-itself-where-it-isnt-wanted.95448/