RPG Maker MV

RPG Maker MV

33 ratings
How to make a variable hold more than 1 value (aka Fun with Object variables)
By MAKAIROSI
How many times didn't you wish there was a way to store many values inside a single variable, and just call it whenever you needed it?

This little guide is an explanation of how to create an object type variable (in simpler terms "a variable with more than 1 value stored"), and call its properties (its different values) whenever you need them.
   
Award
Favorite
Favorited
Unfavorite
Introduction
Before i begin, i made a plugin that automates pretty much everything. You can find it here:
https://forums.rpgmakerweb.com/index.php?threads/makai-object-variables.114287/

I came upon this problem when i was creating a game where each one of the four characters had 5 custom attributes along with their minimum and maximum values. This means i would need 15 variables per character which in turn means i needed 60 variables total.

If you have perfectly designed your game you can devote whole sections of variables to a single character. But what happens if you then come up with an idea to add to that character, and the section isn't enough? You end up with variables 1-5 devoted to a character, and then variables 124, 210 and 213 holding information about the same character.

Soon enough your variable list will become a huge mess. But there is a way to solve this, and that's object type variables.

NOTE: This relies heavily on script commands but don't be afraid, i'm a noob too. I'll explain it in a really simple manner.
Declaring an Object
So let's say that the variable we want to turn into an object is Variable 1. Unfortunately, unless i'm mistaken, there is no way to do this via "control variables->script" so we'll do it directly through script:

Create an event, double-click inside it to issue a new command, go to page 3,
Select "script"

Now type this:

$gameVariables.setValue(1, {
a:1,
b:2
});

Now our variable 1 has 2 properties. a, which is the number 1 and b, which is the number 2.

a and b are now called "properties" of variable 1.
Changing the properties
The most important thing is, obviously, to be able to change the properties of the variable at will. This can easily be done at any time when you open 'script' and type:

$gameVariables.value(1).x = y;

where 'x' is the NAME of the property and 'y' is the new value. So let's try this with our variable:

$gameVariables.value(1).a = 4;

This means that property 'a' of variable 1 is now the number 4, instead of the initial number 1.

Similarly you can add:

$gameVariables.value(1).x += y;

or in general do any operation you want. You can even add the value of an existing variable:

$gameVariables.value(1).x += $gameVariables.value(3);

or another variable's property(!):

$gameVariables.value(1).x += $gameVariables.value(3).y;

I general, with properties, you can do whatever you want. Remember the syntax of calling the value of the property described above.
Adding a property
So now we have learned how to make a variable with properties and how to change those properties. But our initial goal was to add properties at will. This is simpler than you can imagine. From our initial example we have written so far:

$gameVariables.setValue(1, {
a:1,
b:2
})

So now let's say we want a third property that is a boolean one. Open script again and write:

$gameVariables.value(1).c = true;

and you're done!

Now calling that property will return a 'true'. You can even use it with conditions!

if ($gameVariables.value(1).c == true){
$gameVariables.value(1).a++
};

Removing a property is just as easy:

delete $gameVariables.value(1).c;

Now the variable will no longer have that property. Remember that whatever code relies on that property will not work as intended anymore.
Calling a property through a text code
Unfortunately there is no text code to call a property of a variable. You could either make a plugin for that or use a different variable to store the property's value that you need shown in the text. So if we wanted our text to show the property 'b' of variable 1 we would open script and type:

$gameVariables.setValue(2, $gameVariables.value(1).b);

Then in our text we would show that property by typing \v[2].

HOWEVER

There is another solution, by tweaking some other plugins. This is more of a "cheating" method but it works!

1. Get Yanfly's plugins: MessageCore, ExtMesPack2 and MessageMacros1.
2. Enable them in plugins in that order. Read their help files in case you need something else too.
3. Open ExtMesPack2 through the plugin manager

In its parameters (the menu on the right) you will see some "compare" texts (compare1, compare2 etc.) Let's open \Compare1

4. add a line on the top of the text and write /*
5. add a line on the bottom of the text and write */

Now we have bypassed the code of that (you could just delete it but i like keeping it intact)

6. Add a line under the */ you typed and write your own code:

if (y==1){
text = $gameVariables.value(x).a;
};

//Similarly, you can do all the conditions for the other properties of the variables.

if (y==2){
text = $gameVariables.value(x).b;
};

if (y==3){
text = $gameVariables.value(x).c;
};

7. Now this code is stored inside \Compare1<x:y>

If you type \Compare1<1:1> It will return the value of the property 'a' of variable 1. Similarly \Compare1<1:2> returns property 'b'. Also note that the "x" we put instead of the variable's ID means that whatever we write for x inside the text code will return the property of the variable that has that ID. So for example

\Compare1<5:3> will return the property 'c' of the variable whose ID is 5.

8. The reason we needed 'macros' is because we're going to make it even simpler. If you simply want variable 1, property a, you can write inside macro 1 this:

\Compare1<1:1>

Now, each time you write \m[1] you will get property a of variable 1. Similarly you can design the rest of the macros to work for you. For example:

Macro 11: \Compare1<1:1> returns property a of variable 1
Macro 12: \Compare1<1:2> returns property b of variable 1
Macro 21: \Compare1<2:1> returns property a of variable 2
Macro 33: \Compare1<3:3> returns property c of variable 3
Let's test it out!
So let's go into our game and do the following

1. Create an autorun event to set your starting variables

2. Open up 'script' and write the following:

$gameVariables.setValue(1, {
a: "Johnnie",
b: 24,
c: "Here's Johnnie!",
d: 0
});

3. Set a self-switch to A.
4. Add a second page, condition: self-switch=A,trigger: action, leave it empty.

5. Now make an area for our actor to move in and make an action event on a billboard or something.

6. Inside that action event do the following:

Open text window:

Name: \m[11]
Age: \m[12]
Catchphrase: \m[13]
Times said: \m[14]

and then script: $gameVariables.value(1).d++;

7. Go to ExtMesPack2, inside \Compare1, write the code we discussed, but also add this:

if (y==4){
text = $gameVariables.value(x).d;
};

7. Now go to the macros. Change their texts to the following:

Macro 11: \Compare1<1:1>
Macro 12: \Compare1<1:2>
Macro 13: \Compare1<1:3>
Macro 14: \Compare1<1:4>

8. We're done. Test the game.

If you go to the billboard it will show all the properties of that character, but every time the number "times said" will increase by 1. All this with 1 variable!
Final Notes
Pay attention to the script, in case i have forgotten a semi colon here and there or added one when it wasn't needed. Like i said, i'm a noob, so i might make noob mistakes.

I have tested all this, but if it doesn't work, revisit what you wrote. Is your code correct? Are your plugins in the order they need to be? Is one missing? Did you read their help files in case i made a mistake?

And always, feel free to correct me.

I hope this was informative and/or helpful! Happy scripting

PS: I'm in the process of making a plugin to automate all this, just as a project to learn more about plugin creation. I'll post it when it's finished :D
1 Comments
HamilaPlayzMC 7 Aug, 2021 @ 6:22am 
Messing around with Java is always fun. Well done! :csd2yay: