How can I manipulate Game variables through Script Command

Status
Not open for further replies.

Avdul

Villager
Member
Joined
Jun 8, 2016
Messages
7
Reaction score
0
First Language
English
Primarily Uses
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.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,108
Reaction score
13,713
First Language
English
Primarily Uses
RMMV
$gameVariables.setValue(varId, value);
 

Celianna

Tileset artist
Veteran
Joined
Mar 1, 2012
Messages
10,557
Reaction score
5,592
First Language
Dutch
Primarily Uses
RMMV
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".
 

Astfgl66

Veteran
Veteran
Joined
Jan 5, 2016
Messages
722
Reaction score
578
First Language
French
Primarily Uses
@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]
 
Last edited by a moderator:

Avdul

Villager
Member
Joined
Jun 8, 2016
Messages
7
Reaction score
0
First Language
English
Primarily Uses
@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?
 

Avdul

Villager
Member
Joined
Jun 8, 2016
Messages
7
Reaction score
0
First Language
English
Primarily Uses
Yup! It worked like a charm. Thank you guys ;_;


Hope this will someone else too.
 

Astfgl66

Veteran
Veteran
Joined
Jan 5, 2016
Messages
722
Reaction score
578
First Language
French
Primarily Uses
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.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,108
Reaction score
13,713
First Language
English
Primarily Uses
RMMV
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.
 

ABSTRVCT

- Enlightened -
Veteran
Joined
Nov 19, 2016
Messages
38
Reaction score
1
First Language
Spanish
Primarily Uses
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! ;)
 

ABSTRVCT

- Enlightened -
Veteran
Joined
Nov 19, 2016
Messages
38
Reaction score
1
First Language
Spanish
Primarily Uses
Hope my request can be easily fulfilled. It will help the entire rpgmv community.
 

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
31,431
Reaction score
7,712
First Language
German
Primarily Uses
RMMV
@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.
 
Status
Not open for further replies.

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,038
Messages
1,018,466
Members
137,821
Latest member
Capterson
Top