Only storing the variable content, instead of the variable

Monkey BizNiz

Veteran
Veteran
Joined
Apr 22, 2017
Messages
229
Reaction score
29
First Language
Not English
Primarily Uses
RMMV
Hey guys,

Im experimenting with storing data in a variable, and ive stumbled upon a problem. Here is the situation:
Code:
◆Script:$gameVariables.setValue(81, {keyAllowed:{}, actionAllowed:{}, movementRequest:{}, movementOrder:{}, movementState:0});
:      :$gameVariables.value(81).keyAllowed = {space:false, down:false, left:false, right:false, up:false, esc:false};
:      :$gameVariables.value(81).movementRequest = {idle:true, frozen:false, walk:false, sprint:false, jump:false, tossed:false, driving:false, navigation:false, outsideMovement:false};
:      :$gameVariables.value(81).actionAllowed = $gameVariables.value(81).movementRequest); $gameVariables.value(81).actionAllowed.idle = "false";
:      :$gameVariables.value(81).movementOrder = {order:-1, routeCommand:-1, busy:false};
Basically i tried to shorten the data of "$gameVariables.value(81).actionAllowed" to contain the content of "$gameVariables.value(81).movementRequest", but it seems to me that its actually literally storing the "$gameVariables.value(81).movementRequest" variable, where Id only want to store the contents of "$gameVariables.value(81).movementRequest" variable. So basically im asking if there's a command or something to store the contents of a variable, instead of the actual variable?
 

Kuro DCupu

Trust me, I'm a veteran RMer
Veteran
Joined
Jul 6, 2014
Messages
480
Reaction score
1,468
First Language
Indonesia
Primarily Uses
RMMV
It seems you are trying to transform the game variable into an object... never know that actually works...?

Yea, sure... you should've use clone() to just retrieve the content of an object.

Code:
$gameVariables.value(81).actionAllowed = $gameVariables.value(81).movementRequest.clone()
 
Last edited:

Monkey BizNiz

Veteran
Veteran
Joined
Apr 22, 2017
Messages
229
Reaction score
29
First Language
Not English
Primarily Uses
RMMV
@Kuro DCupu Hmm clone() didnt seem to work, it gave me an error. But thanks to you I did find out that it comes down to cloning, which lead me to ''Object.assign({}, $gameVariables.value(81).movementRequest)'' solution, which does seem to do what I want it to do.

Also, ye I found out that you could turn rpg maker variables in objects or arrays by accident, more or less, but very useful to know - makes them 100 times more useful to me.

edit: apparently ''$gameVariables.value(81).movementAllowed = JSON.parse(JSON.stringify($gameVariables.value(81).movementRequest))''
Seems to work even better for cloning an object
 
Last edited:

Sauteed_Onion

Mmm Tasty
Veteran
Joined
Dec 13, 2017
Messages
554
Reaction score
3,665
First Language
English
Primarily Uses
RMMV
Monkey I don't know if this comment helps, but you are like WAY better at this than me, meow
 

Aloe Guvner

Walrus
Veteran
Joined
Sep 28, 2017
Messages
1,628
Reaction score
1,115
First Language
English
Primarily Uses
RMMV
You seem to have stumbled upon an important part of Javascript:
Objects are passed by reference
Primitives are passed by value

Try this example for fun:
Code:
var obj1 = {};
var obj2 = {sauteedOnion: "tasty"};
obj1.prop1 = obj2;

console.log(obj1.prop1 === obj2); // true

obj1.prop1.sauteedOnion = "not tasty";
console.log(obj2.sauteedOnion); // not tasty
In this case, 'obj2' is assigned as a property of 'obj1'. But not a clone or a copy, it is obj2. They are exactly the same, and changing one will change the other (because it's actually the same object just referenced in two different places.)

To clone an object, the best way is probably Object.assign() but if you aren't using MV v1.6.0+ it may not work because it's an ECMA2015 feature. Otherwise, JSON.parse(JSON.stringify()) works. Because 'stringify' turns the value into a string, which is a primitive, and the primitive is not passed by reference.
 

Monkey BizNiz

Veteran
Veteran
Joined
Apr 22, 2017
Messages
229
Reaction score
29
First Language
Not English
Primarily Uses
RMMV
@Aloe Guvner Object.assign() seems to work, but what i found weird is that people called it a shallow clone, when to me it seems to do exactly the same as as JSON.parse(JSON.stringify()) - which people call a deep clone.
 

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

Latest Threads

Latest Profile Posts

Day 9 of giveaways! 8 prizes today :D
He mad, but he cute :kaopride:

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.

Forum statistics

Threads
106,040
Messages
1,018,470
Members
137,821
Latest member
Capterson
Top