jkeks

Warper
Member
Joined
Oct 3, 2022
Messages
4
Reaction score
2
First Language
English
Primarily Uses
RMMZ
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
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
5,432
Reaction score
5,119
First Language
English
Primarily Uses
RMMZ
JavaScript:
$gameVariables.setValue($dataSystem.variables.indexOf("distance"), 12)
 

jkeks

Warper
Member
Joined
Oct 3, 2022
Messages
4
Reaction score
2
First Language
English
Primarily Uses
RMMZ
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
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
5,432
Reaction score
5,119
First Language
English
Primarily Uses
RMMZ
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.
 

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
6,288
Reaction score
4,237
First Language
English
Primarily Uses
RMMV
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 :wink:
 

Bex

Veteran
Veteran
Joined
Aug 2, 2013
Messages
1,816
Reaction score
653
First Language
German
Primarily Uses
RMMV
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.
 

Nolonar

Veteran
Veteran
Joined
Feb 18, 2018
Messages
461
Reaction score
645
First Language
French, German
Primarily Uses
RMMZ
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);
 

ct_bolt

Creator
Veteran
Joined
May 3, 2012
Messages
1,283
Reaction score
824
First Language
Javascript
Primarily Uses
RMMZ
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. :cutesmile:
 

Arthran

Veteran
Veteran
Joined
Jun 25, 2021
Messages
687
Reaction score
720
First Language
English
Primarily Uses
RMMZ
@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:

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.
 
Last edited:

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
5,432
Reaction score
5,119
First Language
English
Primarily Uses
RMMZ
@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:

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

Creator
Veteran
Joined
May 3, 2012
Messages
1,283
Reaction score
824
First Language
Javascript
Primarily Uses
RMMZ
@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 :):thumbsup-right:
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
 
Last edited:

Latest Threads

Latest Profile Posts

Hello! I’d like to ask where is a good place to start logging about my game dev progress as production of my game goes forth
In one of our campus classrooms was a bag full of stuff people forgot there... my bag. That I forgot there xD
You know your room is too tiny for how much stuff you have when it gives you literal insomnia. I just cleared a bunch of stuff out and man it feels good!
1670381555475.png
Meet the Kernel. You encounter him in cyberspace. He's important!
I released a test version of my game on itch.io. If anyone wants to check it out, here's the link (for now in Brazilian Portuguese only, sorry).

https://rafael-lobo.itch.io/insolita

Forum statistics

Threads
127,093
Messages
1,183,887
Members
167,007
Latest member
nayomitsu
Top