Set the value of a variable "within" another variable.

JDevain

Veteran
Veteran
Joined
Oct 21, 2018
Messages
163
Reaction score
76
First Language
English
Primarily Uses
RMMV
Sorry if the title doesn't make sense, I'm not sure how to word it. But here's what I want to do:

First set the value of variable 1 to the ID of the current event, like:
Control Variables : #0001 Self Var generic_1 = this.eventId()

Next, add 400 to variable 1, like
Control Variables : #0001 Self Var generic_1 += 400

Here's where I'm lost. Let's say the value of variable 1 is now 435. So now I want to set a value to variable 435. It would have to be a script call, I just don't know how to do that.
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,473
Reaction score
1,866
First Language
EN
Primarily Uses
RMMZ
The general form of the script call is:
Code:
$gameVariables.setValue(id, value);
So you can do something like this in a Script command:
Code:
var id = $gameVariables.value(1);
var value = 123;
$gameVariables.setValue(id, value);
If variable 1 = 435, this will set variable #435's value to 123. :)
 

Bex

Veteran
Veteran
Joined
Aug 2, 2013
Messages
1,502
Reaction score
412
First Language
German
Primarily Uses
RMMV
Not easy to understand those lines. I hope its ok i make it more readable for non scripters like me =).
a = $gameVariables.value(1);
b = 123;
$gameVariables.setValue(a, b);

or

$gameVariables.setValue($gameVariables.value(1), 123);
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,473
Reaction score
1,866
First Language
EN
Primarily Uses
RMMZ
Not easy to understand those lines. I hope its ok i make it more readable for non scripters like me =).
a = $gameVariables.value(1);
b = 123;
$gameVariables.setValue(a, b);
I just think it's easier to keep track of things if they're given meaningful names...? :kaoswt:

I would not recommend omitting the var keyword like this: it will create two persistent global variables a and b. You can confirm this by opening the console and typing window['a'] after running the script call. Generally harmless though, unless some other place in the code (script call or plugin) uses those variables.

$gameVariables.setValue($gameVariables.value(1), 123);
This is OK! No reminder of which argument is which, though, which is why I prefer the "id, value" thing instead. :kaothx:
 

Bex

Veteran
Veteran
Joined
Aug 2, 2013
Messages
1,502
Reaction score
412
First Language
German
Primarily Uses
RMMV
Yes i agree... but question, arent those a and b variables just temporar if used in/with Event Scriptline and delete themself after the code ended?
In Ruby it was like that, is it different in java script?

But yes i agree meaningful terms are helpful, me as not scripter just often struggles to see if some words are needed or are just fency named.
But since i learned that the fency named word before the = is just a name for a variable which is storing the data after the =, it became much easier. After reading your code again, i self facepalmed myself.
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,473
Reaction score
1,866
First Language
EN
Primarily Uses
RMMZ
I don't know how Ruby works, never really got into it... :kaoslp:

In JS, though, there are keywords like var and let. These allow you to define a variable that exists only on the enclosing block. A block is designated by curly brackets {}, e.g. any function.

Quoting the MDN article for var (source with further examples):
The scope of a variable declared with var is its current execution context, which is either the enclosing function or, for variables declared outside any function, global. If you re-declare a JavaScript variable, it will not lose its value.

Assigning a value to an undeclared variable implicitly creates it as a global variable (it becomes a property of the global object) when the assignment is executed.
For an RPG Maker MV project, the "global object" is the game window. :)
 

bazrat

Pixel Game Maker MV
Veteran
Joined
Oct 15, 2018
Messages
241
Reaction score
179
First Language
English
Primarily Uses
Other
The only thing I would wonder is if the way you both mentioned would set a self var or the actual variable itself (OPs example is a self var from yanfly plugin i assume). Yanfly has lunatic mode script calls in the help file to set self vars, you might have to use that to set it instead of the normal $gameVariables.setValue
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,473
Reaction score
1,866
First Language
EN
Primarily Uses
RMMZ
The only thing I would wonder is if the way you both mentioned would set a self var or the actual variable itself (OPs example is a self var from yanfly plugin i assume). Yanfly has lunatic mode script calls in the help file to set self vars, you might have to use that to set it instead of the normal $gameVariables.setValue
Good call, but it looks like Yanfly's plugin in question aliases the setValue function and checks the variable scope there, so I think it should be OK. I believe the script calls are intended for switching other events' self-switches/-variables. :)
 

JDevain

Veteran
Veteran
Joined
Oct 21, 2018
Messages
163
Reaction score
76
First Language
English
Primarily Uses
RMMV
@caethyril and @bazrat

Actually, there is a problem with Yanfly's plugin SelfSwVar when using that form. When I turn the plugin off and treat it like a normal variable, your solution works, but not when it's on. I read through the lunatic mode and tried this (among other things), but it's still not working:

var id = $gameVariables.value(1);
var value = 123;
var evid = this.eventId();

this.setSelfVariableValue($gameMap._mapId, evid, id, value);


Here's what it says in the plugins instruction file:

============================================================================
Lunatic Mode - Script Calls
============================================================================

For those who'd rather deal altering self switches and/or self variables
inside of the script call event instead, you can use these script calls:

Script Call:

this.getSelfSwitchValue(mapId, eventId, switchId)
- Replace mapId with the map ID the event exists on. Replace eventId with
the ID of the event. And replace the switchId with the ID of the switch.
This will get the true/false value of that event's self switch.

this.getSelfVariableValue(mapId, eventId, varId)
- Replace mapId with the map ID the event exists on. Replace eventId with
the ID of the event. And replace the varId with the ID of the variable.
This will get the value of that event's self variable.

this.setSelfSwitchValue(mapId, eventId, switchId, true)
this.setSelfSwitchValue(mapId, eventId, switchId, false)
- Replace mapId with the map ID the event exists on. Replace eventId with
the ID of the event. And replace the switchId with the ID of the switch.
This will set that self switch to true or false.

this.setSelfVariableValue(mapId, eventId, varId, value)
- Replace mapId with the map ID the event exists on. Replace eventId with
the ID of the event. And replace the varId with the ID of the variable.
This will set that self variable to the value inserted.

============================================================================
 

bazrat

Pixel Game Maker MV
Veteran
Joined
Oct 15, 2018
Messages
241
Reaction score
179
First Language
English
Primarily Uses
Other
@caethyril and @bazrat

Actually, there is a problem with Yanfly's plugin SelfSwVar when using that form. When I turn the plugin off and treat it like a normal variable, your solution works, but not when it's on. I read through the lunatic mode and tried this (among other things), but it's still not working:

var id = $gameVariables.value(1);
var value = 123;
var evid = this.eventId();

this.setSelfVariableValue($gameMap._mapId, evid, id, value);


Here's what it says in the plugins instruction file:

============================================================================
Lunatic Mode - Script Calls
============================================================================

For those who'd rather deal altering self switches and/or self variables
inside of the script call event instead, you can use these script calls:

Script Call:

this.getSelfSwitchValue(mapId, eventId, switchId)
- Replace mapId with the map ID the event exists on. Replace eventId with
the ID of the event. And replace the switchId with the ID of the switch.
This will get the true/false value of that event's self switch.

this.getSelfVariableValue(mapId, eventId, varId)
- Replace mapId with the map ID the event exists on. Replace eventId with
the ID of the event. And replace the varId with the ID of the variable.
This will get the value of that event's self variable.

this.setSelfSwitchValue(mapId, eventId, switchId, true)
this.setSelfSwitchValue(mapId, eventId, switchId, false)
- Replace mapId with the map ID the event exists on. Replace eventId with
the ID of the event. And replace the switchId with the ID of the switch.
This will set that self switch to true or false.

this.setSelfVariableValue(mapId, eventId, varId, value)
- Replace mapId with the map ID the event exists on. Replace eventId with
the ID of the event. And replace the varId with the ID of the variable.
This will set that self variable to the value inserted.

============================================================================
For the varId portion of yanflys script call try the number of the variable (1 in this case), its not asking for the value its asking for var id

Code:
var value = 123;
var evid = this.eventId();

this.setSelfVariableValue($gameMap._mapId, evid, 1, value);
 
Last edited:

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,473
Reaction score
1,866
First Language
EN
Primarily Uses
RMMZ
Oh. Drat. :kaosigh:

Are they both self-variables? Maybe try:
Code:
var mapId = $gameMap.mapId();
var evId = this.eventId();
var varId = this.getSelfVariableValue(mapId, evId, 1);
var value = 123;
this.setSelfVariableValue(mapId, evId, varId, value);
 

JDevain

Veteran
Veteran
Joined
Oct 21, 2018
Messages
163
Reaction score
76
First Language
English
Primarily Uses
RMMV
Thanks, @caethyril Unfortunately, that's still not doing the trick. I tried turning everything else off, but it's still giving me 401=0.
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,473
Reaction score
1,866
First Language
EN
Primarily Uses
RMMZ
Sorry, not sure what the issue might be, that kind of thing seems to work OK for me. Maybe check that self-var 1 is actually getting set correctly beforehand? E.g.
Code:
var mapId = $gameMap.mapId();
var evtId = this.eventId();
var varId = this.getSelfVariableValue(mapId, evtId, 1);
var value = 123;
console.info('map ID:', mapId, '\nevent ID:', evtId, '\nvariable ID:', varId);
this.setSelfVariableValue(mapId, evtId, varId, value);
^ This should list the map, event, and variable IDs in the console (F8 during playtest) immediately before setting the main self-variable. Might be enlightening? :kaoswt:

Otherwise you might also want to check your plugins, perhaps there's a conflict or your Yanfly plugins are mulfunctioning due to not being in the recommended order: http://yanfly.moe/yep/
 

Latest Posts

Latest Profile Posts

Quite the versatile cast so far :p

chars.PNG

Edit: Sprites are made by Alexdraws and TheMightyPalm. I just edited them.
Degica Games Turn Komodo | RPG Maker News #77

Well, rats. Was really looking forward to trying out FPS Creator, but trying to install and set it up was pretty much impossible for my tiny brain to comprehend. So much for that, then.
Ah, home once more! I think I can safely work on my games now.
Let's hope power remains on for the day

Forum statistics

Threads
112,412
Messages
1,068,122
Members
146,063
Latest member
laserdolphSHCH
Top