MZ Using Variable and Switch Names Instead of Index Numbers

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.
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.
Moved to: Plugin Requests

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?
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.

Original thread: https://forums.rpgmakerweb.com/threads/using-variable-and-switch-names-instead-of-index-numbers.138123/