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

Mogumogu09

Warper
Member
Joined
Nov 27, 2019
Messages
4
Reaction score
0
First Language
Indonesian
Primarily Uses
RMMV
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.
 

Shaz

Global Moderators
Global Mod
Joined
Mar 2, 2012
Messages
42,483
Reaction score
14,818
First Language
English
Primarily Uses
RMMV
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.
 

ct_bolt

Creator
Veteran
Joined
May 3, 2012
Messages
967
Reaction score
556
First Language
Javascript
Primarily Uses
RMMZ
can i set variable not by using id variable but by using Variable name in RPG Maker MV?

Sure can :) It's like this :cutesmile:
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:
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:
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:
lol... soo... Ok this might seem a bit crazy but stay with me here :blink::cutesmile::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 :)
 
Last edited:

Latest Threads

Latest Posts

Latest Profile Posts

I hate doing voice over videos but people said they like hearing about the details.
Modern Town Overworld, World of Solaria Dungeon, 80's Retro-Futuristic Font | RPG Maker News #73

I been slowing down and not focusing on project currently.
I had this idea for VTubers vs MOTHER and its a bunch of Vtubers finding eight melodies to fight a psychic alien and a fat kid. IDK, kinda find it hard to use RPG Maker for more than 15 minutes
Working on some wizard/magic bases:
TestChara-10-hover.gif

I mean, technically it's a flying base but I haven't made any wings yet, so magic hovering is it for now.

Forum statistics

Threads
112,300
Messages
1,067,186
Members
145,928
Latest member
Sudrisch
Top