Weird Array Pop() Behaviour

Status
Not open for further replies.

Rink27

Veteran
Veteran
Joined
Jul 18, 2014
Messages
222
Reaction score
13
First Language
English
Primarily Uses
RMMV
I've been working on a plugin I created and noticed something odd.
The following is a representation of piece of my code:

Code:
var array = $gameVariables.value(x);
var array2 = $gameVariables.value(y); // x and y were given the same values

//EDIT: I meant x and y represent different numbers, but these game variables contain the same value.

var id = array.pop();
console.log("Array: "+$gameVariables.value(x));
console.log("Array: "+$gameVariables.value(y));
The arrays for BOTH game variables (x and y) are decreasing as I pop out values from "array".
Firstly, I thought only the new variable "array" would be affected, but $gameVariables.value(x) is also being affected.
Secondly, I don't understand why a different variable is also being affected.

I tested giving $gameVariables.value(y) a different array value and it was unaffected though. Does anyone recognise this issue?
 
Last edited:

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,062
Reaction score
10,363
First Language
Czech
Primarily Uses
RMMV
That's not weird at all. When x and y are given the same values, it means that x = y.
Therefore
$gameVariables.value(x) is the same as
$gameVariables.value(y)

and therefore since you pop one, you pop the other one as well.

As for why if you pop array, $gameVariables.value(x) also gets popped:

There are two major things to talk about: reference and value assignment.
Assignment happens when you either deal with immutable objects (numbers, letters) or with where you call the constructor function.
So you can freely say x = 3 and x = 5. Numbers are immutable, in other words 5 is always 5 and cannot be changed.
However, arrays are mutable. Therefore when you want to make a new one, you need to call a constructor function. There are multiple ways you create an array, one of them is via the new Array, but the most common is via these [] brackets.
When you do x = an already existing array, you reference the already existing array and therefore what influences the first one, influences the second one as well.
 

Rink27

Veteran
Veteran
Joined
Jul 18, 2014
Messages
222
Reaction score
13
First Language
English
Primarily Uses
RMMV
That's not weird at all. When x and y are given the same values, it means that x = y.
Therefore
$gameVariables.value(x) is the same as
$gameVariables.value(y)

and therefore since you pop one, you pop the other one as well.
Thank you for the quick reply. I was wondering if it had to do with references - I'm familiar with the concept.
Um, one mistake on my part. When I said x and y were the same, I meant, for example:
$gameVariables.value(1) and $gameVariables.value(2) were both given the same values.
My bad; I didn't make that clear at all.

Is this behaviour still normal with my updated explanation?
 

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,062
Reaction score
10,363
First Language
Czech
Primarily Uses
RMMV
And how did you make the $gameVariables.value(1) and $gameVariables.value(2) arrays?
 

Rink27

Veteran
Veteran
Joined
Jul 18, 2014
Messages
222
Reaction score
13
First Language
English
Primarily Uses
RMMV
And how did you make the $gameVariables.value(1) and $gameVariables.value(2) arrays?
A prior piece of code performs:
$gameVariables.setValue(1,theArray);
$gameVariables.setValue(2,theArray);

Edit: So I'm using the slice() method to clone the array, eliminating the need of the second variable ($gameVariables.value(2)) - I can preserve the array after popping it. Still curious where my understanding is flawed though with my updated explanation.
 
Last edited:

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,062
Reaction score
10,363
First Language
Czech
Primarily Uses
RMMV
And is theArray an already created array?
Like is it this format:
Code:
var theArray = [1,2,3];
$gameVariables.setValue(1, theArray);
$gameVariables.setValue(2, theArray);
if yes, it is reference, not assignment and therefore it would be normal.
 

Rink27

Veteran
Veteran
Joined
Jul 18, 2014
Messages
222
Reaction score
13
First Language
English
Primarily Uses
RMMV
@Poryg

The code goes like:
Code:
var theArray = [];

// segment of code that pushes values into it

$gameVariables.setValue(1,theArray);
$gameVariables.setValue(2,theArray);
So I guess it's a reference still - thanks. I also edited my previous comment stating that I'm using the slice() method to solve my issue. Thanks for your time.
 

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,062
Reaction score
10,363
First Language
Czech
Primarily Uses
RMMV
Yup, slice method returns a new array, so that is completely fine. And that is the difference between reference and assignment.
 

Kes

Veteran
Veteran
Joined
Aug 3, 2012
Messages
21,844
Reaction score
11,059
First Language
English
Primarily Uses
RMVXA
[closed]IgnoreMe[/closed]
 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

I feel that I should cut down on my order queue so I can get some more time for my projects, its been quite a longggg while since I worked on them LOL
I friend of mine insisted on practicing dubbing things with him... and its quite funny, yet, it really takes a huge amount of practice!
Gotta save this for my next thread later...
Finsh all of the walking dead game in a week, such an amazing emotional trip
I really need to stop sidetracking sometimes

Forum statistics

Threads
97,872
Messages
947,517
Members
129,107
Latest member
waybinary
Top