RMMZ Using Variable and Switch Names Instead of Index Numbers

CyborgPrime

Villager
Member
Joined
Feb 16, 2020
Messages
18
Reaction score
6
First Language
English
Primarily Uses
RMMZ
Having to remember the number of the variable is a huge pain - is there a way to access the variable using its name instead of its index number?

For instance, instead of having to remember that the number of cats is held in Variable 247 (\V[247]) can I just say \V[numCats], assuming I have already named the variable that.
 

Solar_Flare

Veteran
Veteran
Joined
Jun 6, 2020
Messages
715
Reaction score
307
First Language
English
Primarily Uses
RMMV
This is very possible, although you of course need a plugin. As an example, here's a quick plugin that does what you want for MV; a glance at the MZ source code suggests it should work equally well there. Just create a file called NamedVariables.js (or whatever else you want to call it) in the plugins folder and paste the code in there.

JavaScript:
(function() {
    const old_convert = Window_Base.prototype.convertEscapeCharacters
    Window_Base.prototype.convertEscapeCharacters = function(text) {
        text = text.replace(/\\/g, '\x1b');
        for(let i = 0; i < 2; i++) {
            text = text.replace(/\x1bV\[([a-zA-Z_-][a-zA-Z0-9_-]*)\]/gi, function()
                let varId = $dataSystem.variables.indexOf(arguments[1]);
                if(varId <= 0) return '';
                return $gameVariables.value(varId);
            });
        }
        text = old_convert.call(this, text);
        return text
    }
})()

Pay special attention to this part: [a-zA-Z0-9_-]

That means it will only work for variable names that contain letters (a-zA-Z), numbers (0-9), underscores, and hyphens. If you use other characters in your variable names, you can add them there. Just make sure the hyphen is last if present.

For example, if you add space and dollar sign it might become: [a-zA-Z0-9_$ -]

Also note: this code does need to appear twice with the first case excluding 0-9. Otherwise, using variables by number will stop working.
 
Last edited:

Shaz

Global Moderators
Global Mod
Joined
Mar 2, 2012
Messages
44,789
Reaction score
15,917
First Language
English
Primarily Uses
RMMV

I've moved this thread to Plugin Requests. Thank you.



You posted this in MZ Support, but your profile says you are using MV. Can you confirm which one you are using, and what plugins you are using?
 

CyborgPrime

Villager
Member
Joined
Feb 16, 2020
Messages
18
Reaction score
6
First Language
English
Primarily Uses
RMMZ
Oops sorry - I wasn't requesting a plugin - I was wondering if the functionality already existed.

Yes, I use both MV and MZ, both with a variety of plugins. This was a question about MZ but it looks like the functionality is not available in MV either.
 

Latest Threads

Latest Posts

Latest Profile Posts

As you get older you notice how things you always used to do is starting to cause pain XD
I've always sitted on my feet for exemple and now I've started to develop knee pains that always occur after I sit in a way that I've ALWAYS done.
And am I going to stop doing it? Eh, likely not before it is causing me massive pain....:kaoswt:
Yay me...
Taking screenshot from later part of the game, to use as foreshadowing in earlier parts. :kaoblush:
So close to being finished with this portion, feels like it's been taking forever
Tukold2.PNGTukold3.PNGTukold1.PNG

Forum statistics

Threads
124,451
Messages
1,163,755
Members
163,272
Latest member
Krull
Top