MZ Set value by it's name

I know

$gameVariables.setValue - set value by its ID, but I have named my variable and want to set it by name.
Is there way do it ?

Code:
$gameVariables.setValue("distance",12)
- not work
JavaScript:
$gameVariables.setValue($dataSystem.variables.indexOf("distance"), 12)
instead use RPG Maker API, I can use JavaScript global variables, in this case code looks like
window.distanse = 12

but not accesible from RPG Maker scripts.. pure JavaScript
yeahh
thx
I would very much advise against using window variables unless necessary, since they can pollute the namespace and clash with variables of the same name being created by plugins and other scripts.
but not accesible from RPG Maker scripts.. pure JavaScript
Aside from Trihan's advice about that not being good coding practice, what does this mean? There's no difference between "RPG Maker scripts" and "pure JavaScript" - you could enter that line of code in a Script event command and it would work just fine.

It's not recommended, but it would work 😉
I doubt my following suggestion is helpful, but who knows.
In case you have long Code and you don't want to Edit that Variable everywhere in case you want to
choose a different Variable later on, than you could try this in your Codeblock:

distance = 1
$gameVariables.setValue(distance ,12)

1 is the ID of the Variable.
This only makes sense if you have many lines of Code in this Script Command.
Also the temporar variable distance will be deleted after the codeblock ran.
If you need something shorter/easier to type, you could always make a utility plugin, in which you'd define a function named getVariableByName() or setVariableByName()

And already mentioned by others, avoid putting them directly in the window or document objects, since there's a risk of conflicting with other plugins.

So you could do something like this:
JavaScript:
window.jkeks = {
    getVarByName: name => $gameVariables.value($dataSystem.variables.indexOf(name)),
    setVarByName: (name, value) => $gameVariables.setValue($dataSystem.variables.indexOf(name), value)
};

Then you could do this:
JavaScript:
jkeks.setVarByName("distance", 12);
I know

$gameVariables.setValue - set value by its ID, but I have named my variable and want to set it by name.
Is there way do it ?

Code:
$gameVariables.setValue("distance",12)
- not work

Just made a quick plugin to allow also using strings 🙂
Use this plugin to use the exact script you posted 😉


Using that plugin this will now work:
1664995691777.png


& normal way of using a Variable Id will still work too but now you can also just use strings like you requested. ☺️
@jkeks
One thing that is worth mentioning is that MZ's editor allows you to use the same name for multiple variables, so if you wish to use an approach like this, you need to be diligent about ensuring that you are giving each variable a unique name.

The existing solutions in this thread are fine if you can guarantee that you will never make any mistakes in naming your variables, and that you will never make a typo in your script calls. However, if you happen to accidentally have two variables with the same name, or if you accidentally use the wrong capitalization or spelling in the variable name in your script call, then they will kinda just quietly let it happen and give you potentially unintended values, which could result in you having bugs in your code that are hard to trace or that you might not ever even notice.

Since I sometimes have a tendency to let myself get sleep deprived to the point where I turn into a muppet, I can't guarantee that I would never make such a mistake, so I'd personally prefer a solution that explicitly lets me know when I'm doing something wrong. That being the case, I'd go for something a bit more robust, along these lines:

Spoiler: Nolonar's Solution Modified
JavaScript:
var jkeks = jkeks || {};

jkeks.getVarIndex = function(name) {
    let index = 0;
    for (const [key, value] of Object.entries($dataSystem.variables)) {
        if (value === name) {
            if (index !== 0) {
                throw new Error(`There are multiple variables with the name: ${name}`);
            }
            index = key;
        }
    }
    if (index === 0) {
        throw new Error(`There is no variable with the name: ${name}`);
    }
    return index;
};

jkeks.getVarByName = function(name) {
    return $gameVariables.value(this.getVarIndex(name));
};

jkeks.setVarByName = function(name, value) {
    $gameVariables.setValue(this.getVarIndex(name), value);
};

@ct_bolt
I'm in sleep-deprived-muppet-mode right now, so I could be looking at it wrong, but it seems to me that you've got an error in your plugin, when it comes to switches. I think you've got the Game_Switches methods trying to pull an index out of $dataSystem.variables.
@jkeks
One thing that is worth mentioning is that MZ's editor allows you to use the same name for multiple variables, so if you wish to use an approach like this, you need to be diligent about ensuring that you are giving each variable a unique name.

The existing solutions in this thread are fine if you can guarantee that you will never make any mistakes in naming your variables, and that you will never make a typo in your script calls. However, if you happen to accidentally have two variables with the same name, or if you accidentally use the wrong capitalization or spelling in the variable name in your script call, then they will kinda just quietly let it happen and give you potentially unintended values, which could result in you having bugs in your code that are hard to trace or that you might not ever even notice.

Since I sometimes have a tendency to let myself get sleep deprived to the point where I turn into a muppet, I can't guarantee that I would never make such a mistake, so I'd personally prefer a solution that explicitly lets me know when I'm doing something wrong. That being the case, I'd go for something a bit more robust, along these lines:

Spoiler: Nolonar's Solution Modified
JavaScript:
var jkeks = jkeks || {};

jkeks.getVarIndex = function(name) {
    let index = 0;
    for (const [key, value] of Object.entries($dataSystem.variables)) {
        if (value === name) {
            if (index !== 0) {
                throw new Error(`There are multiple variables with the name: ${name}`);
            }
            index = key;
        }
    }
    if (index === 0) {
        throw new Error(`There is no variable with the name: ${name}`);
    }
    return index;
};

jkeks.getVarByName = function(name) {
    return $gameVariables.value(this.getVarIndex(name));
};

jkeks.setVarByName = function(name, value) {
    $gameVariables.setValue(this.getVarIndex(name), value);
};

@ct_bolt
I'm in sleep-deprived-muppet-mode right now, so I could be looking at it wrong, but it seems to me that you've got an error in your plugin, when it comes to switches. I think you've got the Game_Switches methods trying to pull an index out of $dataSystem.variables.
You're correct. He's using the same value function for both, so switches will be looking at the wrong system array.
@ct_bolt
I'm in sleep-deprived-muppet-mode right now, so I could be looking at it wrong, but it seems to me that you've got an error in your plugin, when it comes to switches. I think you've got the Game_Switches methods trying to pull an index out of $dataSystem.variables.

Omg woops! (that's rather embarrassing) :blush:
Thank you! Gosh can't believe I did that... I wasn't originally going to do switches and totally overlooked that lol... anyways fixed now. Thank you for catching that.

Edit:
One thing that is worth mentioning is that MZ's editor allows you to use the same name for multiple variables

so I'd personally prefer a solution that explicitly lets me know when I'm doing something wrong.

Yeah probably a good idea huh 🙂👍
Rewrote a bit and also adding said error checking now. Now has parameter settings for the "error type".
1665024910939.png



[v1.11] [RMMZ] ObjectsEx (Variables & Switches) by CT_Bolt

Original thread: https://forums.rpgmakerweb.com/threads/set-value-by-its-name.151782/