MV Is it possible to create new global variables?

RPGMaker MV uses some complex global variables, like $gameVariables, which are objects, or arrays, or arrays of objects. I'd like to do the same, but I can't figure out how to declare my own global variables to do the same thing.

I tried writing a script to do
var $Test = "value";
but when I tried to check the value of $Test, it came back undefined.

Is this even possible? Should I be looking for an alternative method for this? Have I run myself down a wild goose chase? Or is there actually a way to do this?

Any help is greatly appreciated!
Heya. TBH I'm brand new here and haven't looked at MV's codebase yet, but I'll try to help.

A variable being global simply means that it's not surrounded by any other function. I'm assuming MV must take your script and pass it as a string to eval() somewhere, so your variable will be wrapped at some point.

This is gross, but what happens if you simply make your value a property on one of their globals? Something like $gameVariables.test = 'value'

Does that work? You'd have to ensure the script assigning the value runs before another trying to access it.

I'll go look at the codebase in the meantime so I don't look silly.
RPGMaker MV uses some complex global variables, like $gameVariables, which are objects, or arrays, or arrays of objects. I'd like to do the same, but I can't figure out how to declare my own global variables to do the same thing.

I tried writing a script to do
var $Test = "value";
but when I tried to check the value of $Test, it came back undefined.

Is this even possible? Should I be looking for an alternative method for this? Have I run myself down a wild goose chase? Or is there actually a way to do this?

Any help is greatly appreciated!
where did you put the code?

there's a way to create your own global variable... but you will then need to make sure it saved on savefile especially if you're going to modify it later in game... thus... instead of creating your own global variable... i advice you hijack existing one such as $gameSystem... it already saved to savefile by default so no need for extra step...

so you can just later define your own variable as extension of $gameSystem like this:
Code:
$gameSystem._myVariable = "value";
so you can then access it later using
Code:
$gameSystem._myVariable;

hope this help.
Heya. TBH I'm brand new here and haven't looked at MV's codebase yet, but I'll try to help.

A variable being global simply means that it's not surrounded by any other function. I'm assuming MV must take your script and pass it as a string to eval() somewhere, so your variable will be wrapped at some point.

This is gross, but what happens if you simply make your value a property on one of their globals? Something like $gameVariables.test = 'value'

Does that work? You'd have to ensure the script assigning the value runs before another trying to access it.

I'll go look at the codebase in the meantime so I don't look silly.
Thanks for the idea!

So, that does work, as below, the problem is that I'm hoping to having multiple of these objects stored together, similar to how multiple variable values are stored together in $gameVariables.
1621058859754.png


And this, unfortunately, doesn't seem to work very well, since .test() is undefined as a method for $gameVariables, unlike something like .value() .
1621059197104.png

1621059228519.png


Thanks so much for answering though! Any other ideas?
as below, the problem is that I'm hoping to having multiple of these objects stored together, similar to how multiple variable values are stored together in $gameVariables
$gameVariables is an object, if you want to create something that's similar on top of it just create another object
$gameVariables.myObj = {}

Then you can add your own values
JavaScript:
$gameVariables.myObj.test = 1;
console.log($gameVariables.myObj.test);
So, looking at the codebase, I think there's some confusion going on here between variables (which are simple memory buckets in javascript) and variables inside of MV, which are specific values indexed by numeric ids.

If you just want to store a global, javascript variable, any of the solutions presented here will work. But keep in mind the type of value you're actually setting. If you set a string, like

$gameVariables.test = 'my string'

then no, you can't call it like $gameVariables.test() because a string is not a function.

If you want it to be a method (function), you need to do $gameVariables.test = function test() {
// do something here
}

But what you really want is what @Capitán said. You need to assign it as an object, but then access it as an object (not a function/method)
$gameVariables is an object, if you want to create something that's similar on top of it just create another object
$gameVariables.myObj = {}

Then you can add your own values
JavaScript:
$gameVariables.myObj.test = 1;
console.log($gameVariables.myObj.test);
Thanks for the excellent suggestion! By combining the powers of arrays and objects, I've been able to successfully do what I wanted to with this. It's kind of janky, so it would be nice if somebody had a suggestion for cleaner method, but this will get the job done if there isn't a better way

1621060210903.png

1621060223045.png


Thank you everyone for your ideas, and if anyone has any further suggestions, that would be awesome!
Here's an alternative way to do this by the way if you don't like going through $gameVariables:

window.myCustomVariables = {
test: 'value',
test2: 'another value'
};

then, in the other script, you can simply access these values with:

myCustomVariables.test
You should use an object for the "bucket" of values, not an array. Arrays are accessed via indexes, so you have to know what each index means. Consider the following:

window.myWeapons = {
sword: 15,
hammer: 20
}

If you want to get the sword's attack power, you would just do myWeapons.sword or myWeapons['sword']. This is much preferable to myWeapons[0].

And if you want to iterate over the weapons in my example, you still can!

Object.values(myWeapons) -> this will give you an array of values like [15, 20] so you can loop over them.

Object.keys(myWeapons) -> this will give you an array of the property names like ['sword', 'hammer']

Object.entries(myWeapons) -> this will give you an array of arrays like [['sword', 15], ['hammer', 20]]
You should use an object for the "bucket" of values, not an array. Arrays are accessed via indexes, so you have to know what each index means. Consider the following:

window.myWeapons = {
sword: 15,
hammer: 20
}

If you want to get the sword's attack power, you would just do myWeapons.sword or myWeapons['sword']. This is much preferable to myWeapons[0].

And if you want to iterate over the weapons in my example, you still can!

Object.values(myWeapons) -> this will give you an array of values like [15, 20] so you can loop over them.

Object.keys(myWeapons) -> this will give you an array of the property names like ['sword', 'hammer']

Object.entries(myWeapons) -> this will give you an array of arrays like [['sword', 15], ['hammer', 20]]
That's pretty solid advice!

However, my plan was to create an array of objects, with consistent properties. Something like
[ {name: "sword", attack:15}, {name: "hammer", attack: 20}, {name: "stick", attack:1} ]

The reason I wanted to do this is because the specific use case I have for them requires iterating through the whole list fairly frequently to do things like assigning distributions and amounts, so I need to be able to use the index mathematically. Kind of confusing, especially without more detail, but I think it's the way that makes the most sense for my specific use case. If you really want, I can post some of my code for it for background.

Thanks for your help!
No problem. And sure, go for it!
Thanks for the idea!

So, that does work, as below, the problem is that I'm hoping to having multiple of these objects stored together, similar to how multiple variable values are stored together in $gameVariables.

And this, unfortunately, doesn't seem to work very well, since .test() is undefined as a method for $gameVariables, unlike something like .value() .
So why don't you just define the functions that you'll want to use for your custom variable set and include it as a plugin?

Just as an aside...from one of your later posts, it looks kind of like you're redoing some sort of equipment system. Is there a reason you can't use the built-in weapons functionality? Or are you doing some kind of mini-game or something that just happens to be using weapon names?
@ATT_Turan - he's not, I provided an rpg-ish example for a code concept and he replied in kind
So why don't you just define the functions that you'll want to use for your custom variable set and include it as a plugin?

Just as an aside...from one of your later posts, it looks kind of like you're redoing some sort of equipment system. Is there a reason you can't use the built-in weapons functionality? Or are you doing some kind of mini-game or something that just happens to be using weapon names?
As @frogs_r_cool said, the thing I'm specifically using this for isn't actually related to weapons. It's actually a bridge-building puzzle (hence why some of the early screenshots included stuff like "pieceBudget" and "$bridgePieces" and the like). The weapons was using the example to keep things consistent between my reply and his previous explanation
I found this post useful. Many thanks!
To explain why the original script does not work because whenever you use a script call, it's called within the scope of the Game_Interpreter object belonging to the current scene. Game_Interpreter if you're not aware is the class that handles your game's events, so basically...
Code:
var $Test = "value";
Assigns a local variable to that game interpreter belonging to the current map.
Code:
$Test
returns
Code:
undefined
but it will show up if you entered
Code:
$gameMap._interpreter.$Test
(and the variable dissappears when you move to a new map)
@Gensun That's not correct. You're confusing scope with object properties (and not quite using the correct syntax).

whenever you use a script call, it's called within the scope of the Game_Interpreter object belonging to the current scene.
There's no such thing as "object scope." JavaScript has block, function, and global scopes.

Code:
var $Test = "value";
Assigns a local variable to that game interpreter belonging to the current map.
No it doesn't. It declares a variable within the function scope of eval() when it's run by that event.

Code:
$Test
returns
Code:
undefined
but it will show up if you entered
Code:
$gameMap._interpreter.$Test
No, it won't. If you tried this in a test project, you'd see undefined for both of those.

A variable is only made to be a property of an object if you explicitly declare it to be so. If your script call said:
Code:
this.$test = 4;
then you would be able to access it after the event was completed by looking at $gameMap._interpreter.$test

Also, the OP hasn't been on the site in a year and a half 😉

I'll go ahead and close this. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/is-it-possible-to-create-new-global-variables.136543/