MV Help with creating a new command in a menu.

I make a little script to add a new menu command.
This command turn on the 10th switch. But the games frezzes after being selected
Anyway to prevent the code to frezzes the game?
1680197508047.png

1680197555569.png

JavaScript:
(function() {
   Window_MenuCommand.prototype.addOriginalCommands = function() {
       this.addCommand("Teste", "comando");
   };

   Scene_Menu.prototype.createCommandWindow = function() {
       this._commandWindow = new Window_MenuCommand(0, 0);
       this._commandWindow.setHandler('comando', this.commandcomando.bind(this));
       this.addWindow(this._commandWindow);
   };

   Scene_Menu.prototype.commandcomando = function() {
       $gameSwitches.setValue(10, true);
   };
})();
there are serveral, as "addOriginalCommands" should be aliased,
instead of overwriting, secondly, the commandcando keeps ON,
as it doesn't trigger anything or turn it off.

if that switch is bound to a common event, you can better use
$gameTemp.reserveCommonEvent(ID)
ID being the common event ID you use.

where you call that to toggle it on and off, depending what it does.

while there is very low information what this command should do,
we cannot really help you to correct what needs to be done correctly.
Add this._commandWindow.activate() to your commandcomando method.

(By default Window_Selectable deactivates itself when it processes the OK input.)
there are serveral, as "addOriginalCommands" should be aliased,
instead of overwriting, secondly, the commandcando keeps ON,
as it doesn't trigger anything or turn it off.

if that switch is bound to a common event, you can better use
$gameTemp.reserveCommonEvent(ID)
ID being the common event ID you use.

where you call that to toggle it on and off, depending what it does.

while there is very low information what this command should do,
we cannot really help you to correct what needs to be done correctly.
The command should turn on the 10th switch when is turned off, and vice versa
Add this._commandWindow.activate() to your commandcomando method.

(By default Window_Selectable deactivates itself when it processes the OK input.)
Where part of the code i should I put it exactly? Im Kinda confused
JavaScript:
Scene_Menu.prototype.commandcomando = function() {
    if ($gameSwitches.value == false) {
        $gameSwitches.setValue(10, true);
        this._commandWindow.activate()
    } else {
        $gameSwitches.setValue(10, false)
        this._commandWindow.deactivate()
    }
};
this might work, but there might be a cleaner way to do it though
JavaScript:
Scene_Menu.prototype.commandcomando = function() {
    if ($gameSwitches.value == false) {
        $gameSwitches.setValue(10, true);
        this._commandWindow.activate()
    } else {
        $gameSwitches.setValue(10, false)
        this._commandWindow.deactivate()
    }
};
this might work, but there might be a cleaner way to do it though
The command works, but the screen still frezzing after selecting the command (making it stuck at the game menu).
I meant like this:
JavaScript:
   Scene_Menu.prototype.commandcomando = function() {
       $gameSwitches.setValue(10, true);
       this._commandWindow.activate();
   };
If you want the command to toggle switch 10 on/off instead of just turning it on, try this instead:
JavaScript:
   Scene_Menu.prototype.commandcomando = function() {
       $gameSwitches.setValue(10, !$gameSwitches.value(10));
       this._commandWindow.activate();
   };
its cleaner, but I also think calling a common event is better as you
can control the switch there to toggle switch ON/OFF whatever
should happen.

so in the common event, you can do something like:

if: switch 10 = OFF
switch 10 = ON
else
switch 10 = OFF
end

but whatever happend can be set below the commond, but if you just need
to turn it on for the ingame mechanic, you can do caethyril solution which will
work, but it will be hard to pinpoint why freezes.

you could also hard code in the command list where you have the
other commands that you simply overwrite to add the command
whereever you want between item and game end without using the
addOriginalCommands.

another way is to use a menu customiser, like YEP_MainMenuManager
or CGMV_MenuCommands, so you can eval the commands for the
switch for an easy to toggle switch there.

but the possibility are endless, depending what the switch does or what
it is calling.
You should probably alias Scene_Menu.prototype.createCommandWindow instead of overwriting it: currently you've removed the functionality of all the other buttons. You might want to do something like this:

JavaScript:
let _Scene_Menu_createCommandWindow = Scene_Menu.prototype.createCommandWindow;
Scene_Menu.prototype.createCommandWindow = function() {
    _Scene_Menu_createCommandWindow.call(this);
    this._commandWindow.setHandler('comando', this.commandcomando.bind(this));
};

Also try to disable your other plugins and see if that affects anything - if one of them is also modifying these functions it could cause problems.
Solved the screen frezzing problem, the "SceneManager.push(Scene_Menu);" at the end of the script, thank for everyone for help me in this thread.

Original thread: https://forums.rpgmakerweb.com/threads/help-with-creating-a-new-command-in-a-menu.156274/