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,125
Reaction score
10,639
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,125
Reaction score
10,639
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,125
Reaction score
10,639
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,125
Reaction score
10,639
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.
 
Status
Not open for further replies.

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

Latest Threads

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,867
Messages
1,017,062
Members
137,575
Latest member
akekaphol101
Top