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,798
Reaction score
15,921
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

I finished Stray this morning :(
yo i made a post but it hasnt really gotten any answers so uhhhh
Finished switching to a new PC. Now I could potentially start working at projects or plugins again if I feel the motivation to do so.
If you don't read the news, you are uninformed. If you read the news, you are misinformed.
Custom slip rates and custom ailment durations for enemies really opens up the floodgates for status effect use. I can have Poison and Time Stop affect bosses without it breaking their balance! Finally, a healthy middle ground.

(Especially after I cooked up a thing that prevents you from re-applying deadly states more than once in specific occassions.)

Forum statistics

Threads
124,536
Messages
1,164,284
Members
163,366
Latest member
kurutami
Top