Can someone be my saviour and help me with this simple code,that just seem doesn't work as intended

Kazimoria

Warper
Member
Joined
Jan 30, 2019
Messages
3
Reaction score
1
First Language
English
Primarily Uses
RMMV
Evening guys,i making a a generator for assigning matches for each team,long story short,this was supposed to be the final part of completing it,i made some lines of code,i spend more than 4 hours,andi just can't figured out why the life of me the result of this,i narrow down the issue and is the code i'm having problem with:

◆Script:$gameVariables.setValue(14, [1, 2, 3]);
◆Script:$gameVariables.setValue(16, $gameVariables.value(14)[1]);
◆Script:$gameVariables.value(14)[1] = $gameVariables.value(14)[2];
◆Script:$gameVariables.value(14)[2] = $gameVariables.value(16);
◆Script:$gameVariables.setValue(61, $gameVariables.value(14));
◆Comment:stop
◆Script:$gameVariables.setValue(16, $gameVariables.value(14)[1]);
◆Script:$gameVariables.value(14)[1] = $gameVariables.value(14)[2];
◆Script:$gameVariables.value(14)[2] = $gameVariables.value(16);
◆Script:$gameVariables.setValue(62, $gameVariables.value(14));

so i made an array in variables 14,as you can see,before the comment "stop",array 14 will be assign to array 61 which is (1,3,2),when i execute the code before the stop comment,there's no issue,
but execute as a whole,then that's the problem,the 2nd one is true,variable 14 return (1,2,3) and yes just like the code intended,but,when opening console log(not sure what it's named) by pressing F9,both variable 61and 62 are (1,2,3) ,any last variable will replace all the previous assign variable,let add another one on above

◆Script:$gameVariables.setValue(14, [1, 2, 3]);
◆Script:$gameVariables.setValue(16, $gameVariables.value(14)[1]);
◆Script:$gameVariables.value(14)[1] = $gameVariables.value(14)[2];
◆Script:$gameVariables.value(14)[2] = $gameVariables.value(16);
◆Script:$gameVariables.setValue(61, $gameVariables.value(14));
◆Comment:stop
◆Script:$gameVariables.setValue(16, $gameVariables.value(14)[1]);
◆Script:$gameVariables.value(14)[1] = $gameVariables.value(14)[2];
◆Script:$gameVariables.value(14)[2] = $gameVariables.value(16);
◆Script:$gameVariables.setValue(62, $gameVariables.value(14));
◆Comment:stop
◆Script:$gameVariables.setValue(16, $gameVariables.value(14)[1]);
◆Script:$gameVariables.value(14)[1] = $gameVariables.value(14)[2];
◆Script:$gameVariables.value(14)[2] = $gameVariables.value(16);
◆Script:$gameVariables.setValue(63, $gameVariables.value(14));

now i'm assign into 3 variables,which is 61,62,63

at variable 61,the value of variable 14 is (1,3,2) = variable 61 is now (1,3,2)
at variable 62,the value of variable 14 is (1,2,3) = problem is both variable 61 and 62 is (1,2,3) despite i did not assign it to variable 62
at variable 63,the value of variable 14 is (1,3,2) = now all 3 variable ,variable 61,variable 62, and variable 63 has same value (1,3,2) and not just variable 63

what am i doing it wrong,if this is indeed a bug,then i'm really am screwed,this is the last step for me to finish creating league table for sports,if anyone have better code or find flaws on the code above,that would really really helps me a lot.I will reply as soon as i can get since i might be at work.
 

mlogan

Global Moderators
Global Mod
Joined
Mar 18, 2012
Messages
15,373
Reaction score
8,536
First Language
English
Primarily Uses
RMMV

I've moved this thread to Learning Javascript. Please be sure to post your threads in the correct forum next time. Thank you.

 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,115
Reaction score
1,525
First Language
EN
Primarily Uses
RMMZ
It's these lines causing the confusion:
Code:
$gameVariables.setValue(61, $gameVariables.value(14));
$gameVariables.setValue(62, $gameVariables.value(14));
JS uses pointers for assignment of non-primitive types. This means that if you have, say, a variable A and an array B and you try to set one equal to another:
Code:
A = B;
This will result in A and B "pointing" to the same object. Any changes to one will result in the other being changed, because technically they are both looking at the same object. :kaoback:

To get around this with arrays, use the Array.slice method:
Code:
A = B.slice();
This basically makes a new array with the same element values as B, then assigns A to that new array. Since the objects are now distinct, you can change one without affecting the other. :kaojoy:
Code:
$gameVariables.setValue(61, $gameVariables.value(14).slice());
Be warned that slice is only a "shallow" copy, i.e. it doesn't make a copy of any elements of the array that are objects themselves. Not an issue in this case, since your array is purely numbers, but I thought I'd mention it. :kaothx:
 

Kazimoria

Warper
Member
Joined
Jan 30, 2019
Messages
3
Reaction score
1
First Language
English
Primarily Uses
RMMV
It's these lines causing the confusion:
Code:
$gameVariables.setValue(61, $gameVariables.value(14));
$gameVariables.setValue(62, $gameVariables.value(14));
JS uses pointers for assignment of non-primitive types. This means that if you have, say, a variable A and an array B and you try to set one equal to another:
Code:
A = B;
This will result in A and B "pointing" to the same object. Any changes to one will result in the other being changed, because technically they are both looking at the same object. :kaoback:

To get around this with arrays, use the Array.slice method:
Code:
A = B.slice();
This basically makes a new array with the same element values as B, then assigns A to that new array. Since the objects are now distinct, you can change one without affecting the other. :kaojoy:
Code:
$gameVariables.setValue(61, $gameVariables.value(14).slice());
Be warned that slice is only a "shallow" copy, i.e. it doesn't make a copy of any elements of the array that are objects themselves. Not an issue in this case, since your array is purely numbers, but I thought I'd mention it. :kaothx:
Thank you so much!
 

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

Latest Threads

Latest Profile Posts

Our latest feature is an interview with... me?!

People4_2 (Capelet off and on) added!

Just beat the last of us 2 last night and starting jedi: fallen order right now, both use unreal engine & when I say i knew 80% of jedi's buttons right away because they were the same buttons as TLOU2 its ridiculous, even the same narrow hallway crawl and barely-made-it jump they do. Unreal Engine is just big budget RPG Maker the way they make games nearly identical at its core lol.
Can someone recommend some fun story-heavy RPGs to me? Coming up with good gameplay is a nightmare! I was thinking of making some gameplay platforming-based, but that doesn't work well in RPG form*. I also was thinking of removing battles, but that would be too much like OneShot. I don't even know how to make good puzzles!
one bad plugin combo later and one of my followers is moonwalking off the screen on his own... I didn't even more yet on the new map lol.

Forum statistics

Threads
106,034
Messages
1,018,446
Members
137,820
Latest member
georg09byron
Top