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,646
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,646
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,646
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,646
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
22,305
Reaction score
11,726
First Language
English
Primarily Uses
RMVXA

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

 
Status
Not open for further replies.

Latest Threads

Latest Posts

Latest Profile Posts

Next goal is to make pull & push + lift & throw systems with mouse support and have skill levels in them and action indicators!
>What's your avatar about? Is it yours?
I did not do that drawing. It's a screenshot taken from a comic featuring characters from Touhou such as Tenshi and Iku (the girl in the avatar). It's drawn by someone under the name of Yohane.
Link to the comic (SFW, but navigating around Dynasty Reader is NSFW)
Progress on my game is nearly done, so I figured I show you guys the title screen. I hope you like it!
Title Screen.png
Does this ever happen to anyone?: your making a battle sprite [or any sprite] then when you mess something up you then see it as something diff, then working with it? its hard to say in words but it happens to me all the time
found out how to change pfp, literally there's a hidden thingy on the bottom of it

Forum statistics

Threads
109,167
Messages
1,042,669
Members
141,665
Latest member
beauty143
Top