How can I manipulate Game variables through Script Command

Hi ^_^


How can I manipulate in-game variables through the script command. I would like to do some simple equation like this, but for some reason it just won't work for me Q_Q.


I want to assign a value to a variable that is the result of a mathematical equation of 2 other variables. I tried using:


v[1] = v[2] - v[3];  <----- didn't work


$gameVariables.value(1) = $gameVariables.value(2) - $gameVariables.value(3);    <----- didn't work


I even tried:


var x = $gameVariables.value(2);


var y = $gameVariables.value(3);


$gameVariables.value(1) = x - y;


but it didn't work, is there something I'm missing here? I'd rather use the shorter version of the script if possible.
$gameVariables.setValue(varId, value);
$gameVariables.setValue(varId, value);



but what do I write in "value"? Will  v[2] - v[3] do ?
I think it's like this:


$gameVariables.SetValue(1,10);


1 = variable ID


10 = the value of the variable


Also, this won't work:


var x = $gameVariables.value(2);


var y = $gameVariables.value(3);


$gameVariables.value(1) = x - y;


Because you named them "var x" and "var y" not "x" and "y".
@Celianna


By using var x; var y he declares the variables x and y (as local) and can absolutely call them with x and y after.


If you don't use the var statement it automatically declares the variables as global which you should avoid unless you know what you're doing.


@Avdul You can do it in several ways:

Code:
var x = $gameVariables.value(2);
var y = $gameVariables.value(3);
$gameVariables.setValue(1, x - y)
//Or you could just use
$gameVariables.setValue(1,$gameVariables.value(2) + $gameVariables.value(3))
//Or another way:
var v = $gameVariables._data
v[1] = v[2] - v[3]
@Celianna


By using var x; var y he declares the variables x and y (as local) and can absolutely call them with x and y after.


If you don't use the var statement it automatically declares the variables as global which you should avoid unless you know what you're doing.


@Avdul You can do it in several ways:



var x = $gameVariables.value(2);
var y = $gameVariables.value(3);
$gameVariables.setValue(1, x - y)
//Or you could just use
$gameVariables.setValue(1,$gameVariables.value(2) + $gameVariables.value(3))
//Or another way:
var v = $gameVariables._data
v[1] = v[2] - v[3]



So what I was missing is the need to delcare var v as a "$gameVariables._data" before using the shortened version of that equation, right?
Yup! It worked like a charm. Thank you guys ;_;


Hope this will someone else too.
You were telling the program to look for what was inside the array v. But you didn't tell the program what was v, triggering an undefined error.


That line of code defines v as the array of game variables data, and then by using v you can look and modify it as you wish.


$gameVariables.value(id) is a function that will return the value inside variable id.


It does not return the data, just its value, or 0 if the variable hasn't been set already. You can't assign a value to a game variable using $gameVariables.value(id) = x.


$gameVariables.setValue(id,value) actually modifies the data itself and does several other things, such as flooring the value if it's a number and refreshing the map. You should actually use that function and not the third way I showed because of that.


$gameVariables._data refers to the actual game variables data array for manipulating as you wish if you know what you're doing.
So what I was missing is the need to delcare var v as a "$gameVariables._data" before using the shortened version of that equation, right?



No.  $gameVariables._data is declared by the engine.


What was missing is that you were using the wrong function to set the value.  $gameVariables.value(n) returns the value of variable n.  You can't change the variable with that function.  $gameVariables.setValue(n, value) is what you use to change it.


You should generally avoid using $gameVariables._data for things - functions are provided to read and change the values, so you should use them.  They also run validation that would not happen if you access the _data property directly.
I'd really like to have absolute control over variables via script call and I'm sure I can get it.


Could someone create a great specific Variable Script Call List which could make eventing more easier?


I mean a list of this (hope you guys understand spanish a little bit and know which part of the event editor I'm referring to):


View attachment 54552


Thanks in advance! 😉
Hope my request can be easily fulfilled. It will help the entire rpgmv community.
@ABSTRVCT, please avoid double posting, as it is against the forum rules. You can review our forum rules here. Thank you.


If you have something to add and your own post is the last one, simply edit your post.


Additionally, please do not hijack topics from other people - make your own topic for your own ideas and questions.


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.

Original thread: https://forums.rpgmakerweb.com/threads/how-can-i-manipulate-game-variables-through-script-command.71553/