Only storing the variable content, instead of the variable

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?
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()
@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
Monkey I don't know if this comment helps, but you are like WAY better at this than me, meow
@Sauteed_Onion A laugh is always helpful mate 😛
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.
@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.

Original thread: https://forums.rpgmakerweb.com/threads/only-storing-the-variable-content-instead-of-the-variable.95421/