Unexpected Behavior With Arrays - Alternative Options?

I'm not sure if this is a Java Script question or an MZ Support question. But I discovered an unexpected behavior with Arrays.

I wanted to preserve the values of an Array for a point in time so I could revert the Array back to those values if needed.

So I declared an empty Temp Array [ ] using the Variable Event Command.

Then, at the appropriate point in the Event, I used the Variable Event Command to say that the Temp Array = the Main Array.

I was expecting that when I removed values from the Main Array after that, the values of the Temp Array would remain as they were. I am using Scripts to remove the values from the Main Array if that matters.

But after things weren't working right and I started checking the values in the Arrays step by step, I noticed that removing a value from the Main Array with a script was also removing that same value from the Temp Array.

Set Main Array [1, 2, 3, 4]
Set Temp Array [ ]
Set Temp Array = Main Array
Result Temp Array [1, 2, 3, 4]
Remove 2 & 3 from the Main Array
Result Main Array [1, 4]
Result Temp Array [1, 4]

Is this normal, expected behavior? Once you say that one Array equals another, when you make a change to one, that apparently changes the other too?

If so, is there another way that I can store the values of an Array at different points in time so I can revert to an earlier set of values when necessary?

I am using arrays to store values and removing some of those values at different times. But there are also times where I need to revert back to what the values in the array were before a set of changes.

For example:

Main Array starts with values 1-10.
4, 5, & 6 are removed (need to save the Main Array values that are left at that point)
3 & 2 are removed but now I need to go back to the values of the Main Array just before that

The only other way I can think of doing this is to store each removed value in a separate Variable and then push all those Variables back into the Array when needed.

Any thoughts?

Thanks!
I'm wrong - because an array is an Object, making another variable and simply says it equals it is creating a reference, they are both manipulated that way.

So if you show us the actual code you have, we can help you find a different way to do it, or a different way to copy the original array. That's how you can use the v[] variable in damage formulae to edit the values of game variables.

For example,
Code:
var testArray = [1, 2, 3, 4, 5];
var tempArray = [...testArray];

will duplicate the values of testArray without creating a reference to the testArray Object.

1705108032513.png
When setting an array equal to another like that, what JS is doing is setting a reference to that place in memory. So all you've really done is created another way to access that same array. There's more than a few ways to create a copy of an array. I'm a fan of "concat" myself.

JavaScript:
let tempArray = $gameVariables.value(1)
let tempArray2 = [].concat[tempArray]
$gameVariables.setValue(2, tempArray2)

Where $gameVariable 1 and 2 are whatever you want them to be.

Here's other ways to do it.
The engine has a custom function for making shallow copies of arrays in .clone(), btw, which under the hood returns .slice(0)
Whether or not it's normal depends on how you're removing the elements from the array. The value of an array variable technically isn't an array--it's a reference to an array. You can think of it as an address that points to the location that the array is stored in memory.

So if you do tempArray = array1; then you aren't actually making a copy of array1 and storing it in tempArray--you're just making it so that tempArray points to the same memory location that array1 does. Since they're both pointing to the same array in memory, then any changes made to the array through one variable will be reflected in the other.

If your array only contains primitive types (i.e. numbers, strings, booleans), then RPG Maker has a built-in Array#clone method to make a copy of an array:
JavaScript:
let array1 = [1, 2, 3, 4, 5];
// store a shallow copy of array1 in array2
let array2 = array1.clone();

That would make it so that the second variable contains a reference to a new copy of the array, rather than a reference to the same array. That way, any changes made to one would not affect the other.

However, you could have some other issue at play here, because if you're using the slice method to remove elements from your array, you shouldn't be seeing the issue that you described. Slice technically doesn't modify the original array--instead, it creates a new copy of the array that doesn't contain the removed values. We'd need to see your code to be sure what's going on.
Thanks, everyone! This helps tremendously. I will post the short event that I made to replicate the problem. I usually use .splice, but I started using a different script to remove the value from the array after I checked to make sure I actually wanted to remove it. Before I was splicing it and then pushing it back if needed. Now I check before removing it. Which apparently created the problem.

I will play around with these different things to see how they work. The .clone() option seems intriguing.

Here is the simple Event that replicates the issue. Thanks for these explanations about making references rather than copying or cloning.

This is great!
Array Problem 1.pngArray Problem 2.png
When setting an array equal to another like that, what JS is doing is setting a reference to that place in memory. So all you've really done is created another way to access that same array. There's more than a few ways to create a copy of an array. I'm a fan of "concat" myself.

JavaScript:
let tempArray = $gameVariables.value(1)
let tempArray2 = [].concat[tempArray]
$gameVariables.setValue(2, tempArray2)

Where $gameVariable 1 and 2 are whatever you want them to be.

Here's other ways to do it.

This is very helpful, thanks!

I hadn't considered JSON.parse/JSON.stringify for deep copy though I have used it for other things. It makes sense and now I'm going to use it more often.

I've only used Array.from() to make copies of arrays (no worries to the OP - I ran into the same issue as well and took a bit of reading to find the issue for myself).
Thanks, everyone! This helps tremendously. I will post the short event that I made to replicate the problem. I usually use .splice, but I started using a different script to remove the value from the array after I checked to make sure I actually wanted to remove it. Before I was splicing it and then pushing it back if needed. Now I check before removing it. Which apparently created the problem.

I will play around with these different things to see how they work. The .clone() option seems intriguing.

Here is the simple Event that replicates the issue. Thanks for these explanations about making references rather than copying or cloning.

This is great!
View attachment 291677View attachment 291678
One of the fundamental things to keep in mind in programming is that with some exceptions you generally have two types of value: primitive values and reference values.

When you assign a value to a variable, it's allocated a block of memory on your system, and retrieves that value by going to the address the value is held in. For primitives like integers, this assignment will always be to a new block of memory. If you have two variables x and y and they both contain the value 1, those 1s are in different parts of memory. They're not the *same* 1, though they are equal in integer value.

Contrast that with a reference type like an array. When you create an array, it's also allocated a block of memory on your system. Then, if you assign it to a new variable, it creates a reference to the place in memory where that array is stored, rather than creating a new one. So if you have x = [1, 2, 3] and y = x, removing 2 from the array will result in y being [1, 3] because they pointed to the same array.

There are functions available to arrays in Javascript, like slice, which specifically return a new array, which is why the .clone() function works for making shallow copies. .slice(0) is taking all of the elements of the array starting at index 0 (which is the entire array) but returns it as a new one, leaving the original intact.
Thanks, everyone! This helps tremendously. I will post the short event that I made to replicate the problem. I usually use .splice, but I started using a different script to remove the value from the array after I checked to make sure I actually wanted to remove it. Before I was splicing it and then pushing it back if needed. Now I check before removing it. Which apparently created the problem.

I will play around with these different things to see how they work. The .clone() option seems intriguing.

Here is the simple Event that replicates the issue. Thanks for these explanations about making references rather than copying or cloning.

This is great!
View attachment 291677View attachment 291678
In this case, replacing all instances of Control Variables: TempArray = MainArray with Control Variables: TempArray = $gameVariables.value(19).clone() should do the trick.
Yeah! Something new to play around with! I won't be able to test this until tomorrow, but this is awesome!

It took a while to figure out where the problem was in the Event, but at least now I learned something new and can add that to my reference notes.

Thanks again, everyone! I will post back if I can't make it work right and need more help.
It's always a great feeling when something like this clicks, isn't it?
It's always a great feeling when something like this clicks, isn't it?
I'm still going crazy about .equals() for arrays in RM.

I was over here doing JSON.stringify and JSON.parse and what not to try to get an array of player inputs to match, and Trihan was like "use equals()" haha.
I'm still going crazy about .equals() for arrays in RM.

I was over here doing JSON.stringify and JSON.parse and what not to try to get an array of player inputs to match, and Trihan was like "use equals()" haha.
What's crazy to me is that there is basically no documentation about the little QoL and shortcut extension functions the devs added to MV and MZ's core scripts.
I hadn't considered JSON.parse/JSON.stringify for deep copy though I have used it for other things. It makes sense and now I'm going to use it more often.
It's worth mentioning that there are some limitations to this. The JSON methods won't work right if the object you're trying to copy (or any objects contained within said object) contains functions, symbols, Map or Set objects, or any properties with a value of undefined.
It's worth mentioning that there are some limitations to this. The JSON methods won't work right if the object you're trying to copy (or any objects contained within said object) contains functions, symbols, Map or Set objects, or any properties with a value of undefined.
True - I just finished reading a few articles on this. It really is interesting - Thanks!

What are your thoughts on structuredClone()? Seems promising as well though I have yet to test it.
True - I just finished reading a few articles on this. It really is interesting - Thanks!

What are your thoughts on structuredClone()? Seems promising as well though I have yet to test it.
No problem. I've actually never used structuredClone(). It's not available in the versions of NW.js that ship with MV or MZ.
No problem. I've actually never used structuredClone(). It's not available in the versions of NW.js that ship with MV or MZ.

Did a quick test (using nwjs 0.72) - it does the job and I think it is supposed to do a deep copy without the issues that JSON and the other methods have.
In this case, replacing all instances of Control Variables: TempArray = MainArray with Control Variables: TempArray = $gameVariables.value(19).clone() should do the trick.
So I just tested this in my sample event and it works great! Very easy. I even tested going back and forth. Meaning I set the TempArray to the clone of the MainArray and then I reset the MainArray to the clone of the TempArray.

At least I think that is what I did. I just reversed the Variable Event Command.

Because the whole point is I need to revert the MainArray to a point in time and then continue on. This could be repeated multiple times.

So a follow up question since I obviously don't completely understand how all this works.

Will going back and forth cause any potential problems?

So I clone the MainArray and then clone the clone to reset the MainArray. Then I clone the MainArray again and maybe clone the clone to reset.

Does my question make sense? Or am I missing the point of what is happening behind the mysterious, dark curtain of programmingdom?

Thanks again for the help!
So I just tested this in my sample event and it works great! Very easy. I even tested going back and forth. Meaning I set the TempArray to the clone of the MainArray and then I reset the MainArray to the clone of the TempArray.

At least I think that is what I did. I just reversed the Variable Event Command.

Because the whole point is I need to revert the MainArray to a point in time and then continue on. This could be repeated multiple times.

So a follow up question since I obviously don't completely understand how all this works.

Will going back and forth cause any potential problems?

So I clone the MainArray and then clone the clone to reset the MainArray. Then I clone the MainArray again and maybe clone the clone to reset.

Does my question make sense? Or am I missing the point of what is happening behind the mysterious, dark curtain of programmingdom?

Thanks again for the help!
As long as you're using clone in both directions, there won't be an issue.

However, just to reiterate, clone will only work as expected if the array contains primitive types (i.e. numbers, strings, booleans). If, for example, you have an array of arrays, then clone will only make a copy of the top level array, and the nested arrays will still be susceptible to the same issue that you previously experienced. I know that's not relevant in this case, but I figured that I should go ahead and explain it, since I just casually mentioned it earlier.

Original thread: https://forums.rpgmakerweb.com/threads/unexpected-behavior-with-arrays-alternative-options.164746/