How can i to set variable using variable name in RPG Maker MV?

Usually to set variable value i use this code:

$gameVariables.setValue(3, 4);

number 3 is id variable and 4 is value of 3 in RPG Maker MV.
can i set variable not by using id variable but by using Variable name in RPG Maker MV?
i already try this:

$gameVariables.setValue(idItem, 4);

but idItem's value doesn't change.
No, you have to use the variable id.

What would happen if you tried to do that, and you had more than one variable with the same name?

You could ask someone to write a plugin for it, but it would only work via plugin or script commands. Any event commands or event/troop conditions would still have to use the variable id. And having a plugin to look up a name to find the id, and then change the variable with that id, would add extra processing, so you'd lose a bit of efficiency.


You could do something like this:
Code:
var idItem = 3;
$gameVariables.setValue(idItem, 4);

but what's the point? You still have to define that idItem is variable 3.
can i set variable not by using id variable but by using Variable name in RPG Maker MV?

Sure can 🙂 It's like this ☺️
Code:
$gameVariables.setValue($dataSystem['variables'].indexOf('itemId'), 4);

Created a plugin for you 🙂
VarByName Plugin: Click to Download

Script Calls:
Code:
setVarByName(name, value, occurrence);
getVarByName(name, occurrence);
Examples:
Spoiler
Code:
setVarByName('idItem', 1); // sets the value first occurrence of "idItem" equal to 1
getVarByName('idItem'); // returns the value first occurrence of "idItem"
setVarByName('idItem', 5, 2); // sets second occurrence of "idItem" equal to 5

v0.2 Now you can name multiple variables the same name 🙂
v0.2+ Script Calls:
Spoiler
Code:
setVarByName('idItem', 1, 2);
getVarByName('idItem', 2);
That will set to find the second occurrence of the named variable 🙂
Code:
setVarByName('idItem', 2, 6);
getVarByName('idItem', 6);
That will set to find the sixth occurrence of the named variable 🙂

Essentially this would work kind of like an array of its own.

Extra Example:
Spoiler
lol... soo... Ok this might seem a bit crazy but stay with me here :blink:☺️:popcorn:
Code:
// This sets the third occurrence or "idItem" to the sum of the first and second occurrence of "idItem"
setVarByName('idItem', "getVarByName('idItem', 1)+getVarByName('idItem', 2)", 3);


Let me know how it goes, hope it works the way you need 🙂
Sure can 🙂 It's like this ☺️
Code:
$gameVariables.setValue($dataSystem['variables'].indexOf('itemId'), 4);

Created a plugin for you 🙂
VarByName Plugin: Click to Download

Script Calls:
Code:
setVarByName(name, value, occurrence);
getVarByName(name, occurrence);
Examples:
Spoiler
Code:
setVarByName('idItem', 1); // sets the value first occurrence of "idItem" equal to 1
getVarByName('idItem'); // returns the value first occurrence of "idItem"
setVarByName('idItem', 5, 2); // sets second occurrence of "idItem" equal to 5

v0.2 Now you can name multiple variables the same name 🙂
v0.2+ Script Calls:
Spoiler
Code:
setVarByName('idItem', 1, 2);
getVarByName('idItem', 2);
That will set to find the second occurrence of the named variable 🙂
Code:
setVarByName('idItem', 2, 6);
getVarByName('idItem', 6);
That will set to find the sixth occurrence of the named variable 🙂

Essentially this would work kind of like an array of its own.

Extra Example:
Spoiler
lol... soo... Ok this might seem a bit crazy but stay with me here :blink:☺️:popcorn:
Code:
// This sets the third occurrence or "idItem" to the sum of the first and second occurrence of "idItem"
setVarByName('idItem', "getVarByName('idItem', 1)+getVarByName('idItem', 2)", 3);


Let me know how it goes, hope it works the way you need 🙂
Thanks this helped me out greatly too! Any way to make the plugin do this for Switches too in MZ?
Thanks this helped me out greatly too! Any way to make the plugin do this for Switches too in MZ?
Yeah maybe I never posted this to the forums, hmm.. thought I did...
anyways.. here it is...

That should be able to do everything needed 🙂
Yeah maybe I never posted this to the forums, hmm.. thought I did...
anyways.. here it is...

That should be able to do everything needed 🙂
Perfect! Thanks a bunch!
Perfect! Thanks a bunch!
No prob. Happy to help ☺️

Original thread: https://forums.rpgmakerweb.com/threads/how-can-i-to-set-variable-using-variable-name-in-rpg-maker-mv.115957/