NeptuneTron

The Salticid Scholar of the North
Veteran
Joined
Jan 13, 2018
Messages
75
Reaction score
44
First Language
English
Primarily Uses
RMMV
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!
 

frogs_r_cool

Villager
Member
Joined
May 11, 2021
Messages
16
Reaction score
8
First Language
English
Primarily Uses
RMMZ
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.
 

estriole

Veteran
Veteran
Joined
Jun 27, 2012
Messages
1,501
Reaction score
727
First Language
indonesian
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.
 

NeptuneTron

The Salticid Scholar of the North
Veteran
Joined
Jan 13, 2018
Messages
75
Reaction score
44
First Language
English
Primarily Uses
RMMV
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?
 

Capitán

kind of a big deal
Veteran
Joined
Jul 9, 2013
Messages
588
Reaction score
160
First Language
Engilsh
Primarily Uses
RMMV
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);
 

frogs_r_cool

Villager
Member
Joined
May 11, 2021
Messages
16
Reaction score
8
First Language
English
Primarily Uses
RMMZ
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)
 

NeptuneTron

The Salticid Scholar of the North
Veteran
Joined
Jan 13, 2018
Messages
75
Reaction score
44
First Language
English
Primarily Uses
RMMV
$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!
 

frogs_r_cool

Villager
Member
Joined
May 11, 2021
Messages
16
Reaction score
8
First Language
English
Primarily Uses
RMMZ
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
 

frogs_r_cool

Villager
Member
Joined
May 11, 2021
Messages
16
Reaction score
8
First Language
English
Primarily Uses
RMMZ
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]]
 
Last edited:

NeptuneTron

The Salticid Scholar of the North
Veteran
Joined
Jan 13, 2018
Messages
75
Reaction score
44
First Language
English
Primarily Uses
RMMV
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!
 

frogs_r_cool

Villager
Member
Joined
May 11, 2021
Messages
16
Reaction score
8
First Language
English
Primarily Uses
RMMZ
No problem. And sure, go for it!
 

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
1,159
Reaction score
636
First Language
English
Primarily Uses
RMMV
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?
 

frogs_r_cool

Villager
Member
Joined
May 11, 2021
Messages
16
Reaction score
8
First Language
English
Primarily Uses
RMMZ
@ATT_Turan - he's not, I provided an rpg-ish example for a code concept and he replied in kind
 

NeptuneTron

The Salticid Scholar of the North
Veteran
Joined
Jan 13, 2018
Messages
75
Reaction score
44
First Language
English
Primarily Uses
RMMV
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
 

Latest Threads

Latest Posts

Latest Profile Posts

Haven't used this website in so long, hope everyone here have a good day! Lately I've been crafting a dnd world, so I haven't got the time to use RPG maker sadly enough.
New MZ Update! Subfolders for Assets, Search in Plugin Help, Map Layer Buttons | RPG Maker News #78

A little WIP I decided to drop for something coming in 10-12 hours!
9.PNG
I just did some math and... apparently what seemed to be a little fun gimmick turned out to be an 80+ actors project :kaoswt2:
Yay! the original character contest 3 pack is here!
Both MV and MZ looks very good, congratulation for both RMW and Tkool participant!
I definitely can add these characters to use for my game project.

Steam link

Forum statistics

Threads
112,482
Messages
1,068,578
Members
146,149
Latest member
GlacierStangs
Top