MZ Is there a way you can turn a switch ON whenever the menu is opened?

Hiya everyone! Hope you're all doing fantastic!
I am here to ask another question: is there a way you can turn a switch ON whenever the menu is opened?
I think you have to modify the function that opens the main menu to also turn on a switch, because you can't normally run common events while the main menu is open. In other words, you need a plugin, or to write one.
I would've made the same suggestion as AquaEcho. But... what do you actually want to do with the switch? Like AquaEcho said, events don't normally run in the menu, so I'm curious what you're doing with the switch. Do you also want the switch turned off when the menu is closed? Or maybe you want a map event to fire after the menu has been used?
Save this as a Plugin with any name:

Code:
(function() {

  const switchId = 1;
  
  const alias = Scene_Map.prototype.callMenu;
  Scene_Map.prototype.callMenu = function() {
    $gameSwitches.setValue(switchId, true);
    alias.call(this);
  }

})()
Replace 1 with your Switch Id.

This plugin sets Switch 1 to ON when calling the menu from Map. Nothing more. It may not enable the Switch when you open the menu through an Event Command or other method. To make a plugin that would do the opposite (switching it to OFF when leaving the menu) is a bit more difficult, so let me know if you would need that as well.

Original thread: https://forums.rpgmakerweb.com/threads/is-there-a-way-you-can-turn-a-switch-on-whenever-the-menu-is-opened.160443/