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

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.
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. 🙂
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);
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...? 😅

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. 🙏
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.
I don't know how Ruby works, never really got into it... 😴

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. 🙂
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
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. 🙂
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. 🙂
Ahh, makes sense
@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.

============================================================================
@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);
Oh. Drat. 😔

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);
Thanks, @caethyril Unfortunately, that's still not doing the trick. I tried turning everything else off, but it's still giving me 401=0.
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? 😅

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/

Original thread: https://forums.rpgmakerweb.com/threads/set-the-value-of-a-variable-within-another-variable.110021/