RPG Maker MV

RPG Maker MV

Not enough ratings
Adding a new menu command in MV (JavaScript)
By MAKAIROSI
The default commands of RMMV (like "item", "skill" etc.) are more than enough to create an RPG game. However, this engine can be used for any genre, which means that you might want to use those scenes to implement different features, and might need new commands for those features. For example, you might need a "Build Structure" command that utilizes the "skill" scene with some additions. That's what we're going to cover in this guide!
   
Award
Favorite
Favorited
Unfavorite
Introduction
So i wanted to add a new menu command. I wanted something simple, to have a command same as the skill command, but 1)with my own name, 2)without changing the default skill command 3)with the addition of running another function before going to the skill scene and 4)bypassing the actor select phase of the default skill command.

The first thing i did was to get Yanfly's Main Menu Manager but even though i have JS experience, i couldn't get it to work. However, looking at his code made me realize which functions i was going to need to create my new command.

Before i begin i think we should also talk about how could that be used.
Introduction (why do that?)
The question is this: If we're going to utilize the same scene as the skill scene, why add a command and not just use the skill command?

1. Having a skill running a common event pops the scene (gets you back to the map scene - closes the entire menu scene). We can bypass that.

2. Having different skill types for different actions means you will have them ALWAYS active and visible in the skill scene. We can change that.

3. The skill command is grouped together with other commands that require the selection of an actor first. So changing it, means changing that entire function, which other plugins might rely on. We're going to make our own function for our new command, so we also bypass that.

I could come up with more reasons, but these 3 are enough. So let's do it!
1. Setting up our new command
First of all we have to think with what our new command is going to be adding to the skill scene. For this guide i will have a little example to play with. Let's say, that the command is to build a structure - like i said in the intro.

So for this we are going to need:
1) Two new *skill types*: "Production Structures" and "Defensive Structures" (just to have fun)
2) A new *state*: name it "structure sel" and have it add our two new skill types. Let's say its ID is 85 for this example.

Make sure our new skill types ARE NOT included in the character's class or whatever. They are only added via the state of 2).

3) Now make some skills for each of the skill types. For example, "turret" -> "defensive structures" and "miner"->"production structures"

Ok, i think we're ready to proceed to actual javascript now.
2. The name of our command
In this example the name will be "Build Structures". So how does the name get added in the menu?

In the "rpg_windows.js" you will find this function:

Window_MenuCommand.prototype.addMainCommands = function () {
var enabled = this.areMainCommandsEnabled();
if (this.needsCommand('item')) {
this.addCommand(TextManager.item, 'item', enabled);
}
if (this.needsCommand('skill')) {
this.addCommand(TextManager.skill, 'skill', enabled);
}
if (this.needsCommand('equip')) {
this.addCommand(TextManager.equip, 'equip', enabled);
}
if (this.needsCommand('status')) {
this.addCommand(TextManager.status, 'status', enabled);
}
};

So let's copy one of these to break it down and change it to our new command. Copy the following function just after the "var enabled" line:

if (this.needsCommand('skill')) {
this.addCommand(TextManager.skill, 'skill', enabled);
}

1. "this.needsCommand('skill')" This condition means if our 'skill' command is activated from the system. Since we want our command to always be visible, we can scrap this.

2. this.addCommand: This function adds the command, but now let's look at the arguments:

3. TextManager.skill: This takes the NAME of the command from textManager. Since we know our name is "Build Structure" we can just put it in there in quotes.

4. 'skill': Think of this as an alias to identify this command with. We will need it later. Since our command is new we will set it to "bns" (from "build new structure").

5. enabled: This basically evaluates some requirements of whether there can be commands at all. For now, leave it as it is.

So the copied lines of code became this:

this.addCommand('Build Structure', 'bns', enabled);

And if we now look at our changed function we have this:

Window_MenuCommand.prototype.addMainCommands = function () {
var enabled = this.areMainCommandsEnabled();

this.addCommand('Build Structure', 'bns', enabled);

if (this.needsCommand('item')) {
this.addCommand(TextManager.item, 'item', enabled);
}
if (this.needsCommand('skill')) {
this.addCommand(TextManager.skill, 'skill', enabled);
}
if (this.needsCommand('equip')) {
this.addCommand(TextManager.equip, 'equip', enabled);
}
if (this.needsCommand('status')) {
this.addCommand(TextManager.status, 'status', enabled);
}
};
3. Command handlers
Remember we used 'bns' as an "alias" to identify our command with? Now we will use this when setting up its handler.

Going to the "rpg_scenes.js" file now, we can find this function:

Scene_Menu.prototype.createCommandWindow = function () {
this._commandWindow = new Window_MenuCommand(0, 0);
this._commandWindow.setHandler('item', this.commandItem.bind(this));
this._commandWindow.setHandler('skill', this.commandPersonal.bind(this));
this._commandWindow.setHandler('equip', this.commandPersonal.bind(this));
this._commandWindow.setHandler('status', this.commandPersonal.bind(this));
this._commandWindow.setHandler('formation', this.commandFormation.bind(this));
this._commandWindow.setHandler('options', this.commandOptions.bind(this));
this._commandWindow.setHandler('save', this.commandSave.bind(this));
this._commandWindow.setHandler('gameEnd', this.commandGameEnd.bind(this));
this._commandWindow.setHandler('cancel', this.popScene.bind(this));
this.addWindow(this._commandWindow);
};

You might already see what we're going to use to set a handler for our command. Note that "this.commandPersonal.bind(this)" means that an actor must be selected beforehand - and we don't want that. Also note that the commands that don't require an actor selection, have their own functions. So that's what we're going to do. First of all, let's, again, copy the skill line to just under the "new window" construction, and transform it to suit our needs:

this._commandWindow.setHandler('bns', this.commandBNS.bind(this));

Note that "commandBNS" doesn't exist yet, i just made it up. We will declare it in the next section. For now, the new scene_menu function will look like this:

Scene_Menu.prototype.createCommandWindow = function () {
this._commandWindow = new Window_MenuCommand(0, 0);

this._commandWindow.setHandler('bns', this.commandBNS.bind(this));

this._commandWindow.setHandler('item', this.commandItem.bind(this));
this._commandWindow.setHandler('skill', this.commandPersonal.bind(this));
this._commandWindow.setHandler('equip', this.commandPersonal.bind(this));
this._commandWindow.setHandler('status', this.commandPersonal.bind(this));
this._commandWindow.setHandler('formation', this.commandFormation.bind(this));
this._commandWindow.setHandler('options', this.commandOptions.bind(this));
this._commandWindow.setHandler('save', this.commandSave.bind(this));
this._commandWindow.setHandler('gameEnd', this.commandGameEnd.bind(this));
this._commandWindow.setHandler('cancel', this.popScene.bind(this));
this.addWindow(this._commandWindow);
};
4. Declare that function!
Now we are at the peak! Let's declare the function we talked about earlier. You will note that when functions like "commandOptions" are declared they just push the corresponding scenes. So let's do that as well:

Scene_Menu.prototype.commandBNS = function () {
SceneManager.push(Scene_Skill);
}

Now this is our new function that simply leads us to the skill scene - does nothing more. Anything more we want it to do we can add before it pushes the skill scene. What did we want in the beginning? To add the state of ID 85, right? So let's do it now:

Scene_Menu.prototype.commandBNS = function () {
$gameActors.actor(1).addState(85);
SceneManager.push(Scene_Skill);
}

If you're using a party, you might probably want to use something like $gameParty or identify the leader or whatever. For this example, we assume there's only 1 actor, and that's actor 1.

The command is now ready and usable BUT you will note that when selecting this command and then going to the skills command, this state persists.

We can change this in two different ways, first of all there is an "onCancel" function that simply pops the scene - we can use this to remove our state OR add a new command that immitates the skills' command and simply change that state from there. Since we're learning how to add new commands, we'll do the latter.

So let's turn the skills off from the system first, and now remake them, just like we made our "Build Structure" command, but with the addition of removing that state.
5. Remaking the skills command (optional)
I'm not going to talk about all the steps again here, i'm just going to show what the lines of code are:

1. For the name:

this.addCommand('Skills', 'normalskills', enabled);

2. Setting up its handler:

this._commandWindow.setHandler('normalskills', this.commandNormalSkills.bind(this));

3. Declaring its function:

Scene_Menu.prototype.commandNormalSkills = function () {
$gameActors.actor(1).removeState(85);
SceneManager.push(Scene_Skill);
}

Et voila. Now selecting our own command will add the state, while selecting the skills command will remove it. Test it out! It's ready!

NOTE: In this manner, the skills' command will now NOT require an actor selection. If you're using a party this is a bad thing. However, you can look up what the default skill command does and copy it, so that your 'normalskills' command mirrors it.

Or you can simply follow the code from the 'cancel' command and add your code in there.
What can i do from here on?
You just learned how to create a new command, your very own! So now you can - as always - experiment.

Since we are adding our own code before pushing the scene, you can pretty much do anything in there. Look inside an array, manipulate objects, manually add events (like by using Yanfly's spawn event plugin), or even, setting up switches that will make selecting a skill behave differently, or display a different text.

Just remember that whatever you do, that is going to change how normal skills look like or behave, must be reverted when cancelling or when selecting the normal 'skills' command.

Since i personally find it difficult to create my own scene, i use the already made scenes with switches, so that they display different things depending on the command. For example, you can use the "status" scene to display your normal attributes, or, if a certain switch is on, display different attributes instead. Since we can now add a new command for anything, the sky is our limit!
Finally
Please notify me for any typos or bugs you may encounter during this guide. Everything i've typed worked out for me, but during copying the lines of code i might have maken some mistakes. So feel free to tell me :)

Also (and i will always say this) never stop experimenting!
1 Comments
FriKitty Oct 27, 2024 @ 7:20pm 
Thank you so so much for this guide. I didn't want to do anything super original, just add a Load (or Continue) command just like in the Title Menu, and thanks to this I could figure out the Handler thing (I had no idea and nowhere to look for this). It's now functional, thank you very much :)