MV [SOLVED] How to make a switchable function

Hi,
Yesterday my friend share me the code to change menu when specific Switch was ON.

This is the code:

I am trying to hide some Menu Commands when switch was on by adding some commented code like this:

Code:
Window_MenuCommand.prototype.addFormationCommand = 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("Units", 'status', enabled);
    }
  };

It works, but it doesn't follow the Switch as intended, so the change in menu are permanent instead of switchable.

How to make the code above switchable?

Thanks.
your code is commenting the add main command code thus make it permanent... if you want it switchable you can either add extra condition in the if or modify the needsCommand function...

by adding extra condition in the ifs example:
Code:
Window_MenuCommand.prototype.addMainCommands = function() {
    var enabled = this.areMainCommandsEnabled();
    if (this.needsCommand('item') && $gameSwitches.value(1)) {
        this.addCommand(TextManager.item, 'item', enabled);
    }
    if (this.needsCommand('skill')  && $gameSwitches.value(2)) {
        this.addCommand(TextManager.skill, 'skill', enabled);
    }
    if (this.needsCommand('equip')  && $gameSwitches.value(3)) {
        this.addCommand(TextManager.equip, 'equip', enabled);
    }
    if (this.needsCommand('status')) {
        this.addCommand("Units", 'status', enabled);
    }
  };
you can change $gameSwitches.value(x) with any switch id you want... in my example above
switch 1 is for command item, switch 2 is for command skill, switch 3 is for command equip.
the command will only shown if the switch is ON.

BUT above is not a good way to do this though... because it's an overwrite thus possibility of incompatibility with other menu altering plugin is high...
better way is to alias the needsCommand function like this:
Code:
var Window_MenuCommand_needsCommand_switchCond = Window_MenuCommand.prototype.needsCommand;
Window_MenuCommand.prototype.needsCommand = function(name) {
  switch (name){
   case 'item':
    return Window_MenuCommand_needsCommand_switchCond.call(this,name) && $gameSwitches.value(1);
   case 'skill':
    return Window_MenuCommand_needsCommand_switchCond.call(this,name) && $gameSwitches.value(2);
   case 'equip':
    return Window_MenuCommand_needsCommand_switchCond.call(this,name) && $gameSwitches.value(3);
   case 'status':
    return Window_MenuCommand_needsCommand_switchCond.call(this,name) && $gameSwitches.value(4);
   case 'formation':
    return Window_MenuCommand_needsCommand_switchCond.call(this,name) && $gameSwitches.value(5);
   case 'save':
    return Window_MenuCommand_needsCommand_switchCond.call(this,name) && $gameSwitches.value(6);
  }
  return true;
};
change the switch id inside $gameSwitches.value(x) to switch you want... above example will use:
item -> switch 1
skill -> switch 2
equip -> switch 3
status -> switch 4
formation -> switch 5
save -> switch 6

hope this help.
Hi, Mr. @estriole .

The code above works like charm and I can finish my custom menu because of your help.

Thank you very much for the big help. 😂

Original thread: https://forums.rpgmakerweb.com/threads/solved-how-to-make-a-switchable-function.129521/