MV Change Menu Background using Switches

Hi, looking for a plugin that let's you change the menu background using switches (the menu already made transparent beforehand) for Rpg Maker MV.

I coded something similar back when the game was in Vx Ace, but I know very little of MV.
ORIGINAL VX ACE VERSION:

class Scene_MenuBase < Scene_Base
#--------------------------------------------------------------------------
alias background_picture_create_background create_background
def create_background
background_picture_create_background
@background_picture_sprite = Sprite.new
if $game_switches[261]
@background_picture_sprite.bitmap = Cache.picture('MenuBackground')
elsif $game_switches[262]
@background_picture_sprite.bitmap = Cache.picture('MenuBackground2')
elsif $game_switches[263]
@background_picture_sprite.bitmap = Cache.picture('MenuBackground3')
elsif $game_switches[264]
@background_picture_sprite.bitmap = Cache.picture('MenuBackground4')
elsif $game_switches[265]
@background_picture_sprite.bitmap = Cache.picture('MenuBackground5')
end
end

#--------------------------------------------------------------------------
alias background_picture_dispose_background dispose_background
def dispose_background
@background_picture_sprite.dispose
end
end
Hi there!

I believe my Background Manager for MV can do that alreasdy. It's free(Inside the sample project, and need Eli Book as a core plugin):

You can either use it or look into the code to have an idea of how you can reproduce that.
Instead of mimicing the script literraly, why not try something like this :


JavaScript:
/*:
 * @plugindesc Change Menu Background based on Switches dynamically through plugin commands.
 * @help Use the following plugin command to set the switch and background:
 *   SetMenuBackground switchId imageName
 * Example:
 *   SetMenuBackground 261 MenuBackground
 * Simply use the image name from the /images folder
 */

(function () {
    var _Scene_MenuBase_createBackground = Scene_MenuBase.prototype.createBackground;
    var backgroundSwitch = 0;
    var backgroundImage = '';

    Scene_MenuBase.prototype.createBackground = function() {
        _Scene_MenuBase_createBackground.call(this);
        this._backgroundSprite = new Sprite();
        this.addChild(this._backgroundSprite);
        if ($gameSwitches.value(backgroundSwitch)) {
            this._backgroundSprite.bitmap = ImageManager.loadPicture(backgroundImage);
        }
    };

    var _Scene_MenuBase_terminate = Scene_MenuBase.prototype.terminate;
    Scene_MenuBase.prototype.terminate = function() {
        _Scene_MenuBase_terminate.call(this);
        if (this._backgroundSprite) {
            this.removeChild(this._backgroundSprite);
            this._backgroundSprite.bitmap = null;
            this._backgroundSprite = null;
        }
    };

    var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
    Game_Interpreter.prototype.pluginCommand = function(command, args) {
        _Game_Interpreter_pluginCommand.call(this, command, args);
        if (command === 'SetMenuBackground') {
            backgroundSwitch = Number(args[0]);
            backgroundImage = String(args[1]);
        }
    };

})();


You can then call the plugin command using SetMenuBackground SwitchID Image

Edit : Damn 😀 if it already exists it's fine too xD
Instead of mimicing the script literraly, why not try something like this :


JavaScript:
/*:
 * @plugindesc Change Menu Background based on Switches dynamically through plugin commands.
 * @help Use the following plugin command to set the switch and background:
 *   SetMenuBackground switchId imageName
 * Example:
 *   SetMenuBackground 261 MenuBackground
 * Simply use the image name from the /images folder
 */

(function () {
    var _Scene_MenuBase_createBackground = Scene_MenuBase.prototype.createBackground;
    var backgroundSwitch = 0;
    var backgroundImage = '';

    Scene_MenuBase.prototype.createBackground = function() {
        _Scene_MenuBase_createBackground.call(this);
        this._backgroundSprite = new Sprite();
        this.addChild(this._backgroundSprite);
        if ($gameSwitches.value(backgroundSwitch)) {
            this._backgroundSprite.bitmap = ImageManager.loadPicture(backgroundImage);
        }
    };

    var _Scene_MenuBase_terminate = Scene_MenuBase.prototype.terminate;
    Scene_MenuBase.prototype.terminate = function() {
        _Scene_MenuBase_terminate.call(this);
        if (this._backgroundSprite) {
            this.removeChild(this._backgroundSprite);
            this._backgroundSprite.bitmap = null;
            this._backgroundSprite = null;
        }
    };

    var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
    Game_Interpreter.prototype.pluginCommand = function(command, args) {
        _Game_Interpreter_pluginCommand.call(this, command, args);
        if (command === 'SetMenuBackground') {
            backgroundSwitch = Number(args[0]);
            backgroundImage = String(args[1]);
        }
    };

})();


You can then call the plugin command using SetMenuBackground SwitchID Image

Edit : Damn 😀 if it already exists it's fine too xD
It works alright, except for one little thing......If I save my progress and close the game, when I try to open the game again, the background image is gone....
It works alright, except for one little thing......If I save my progress and close the game, when I try to open the game again, the background image is gone....
The image is gone while the switch it still on?
Could you check if the switch is still ON but the screen image gone

If that's the case you could just call for the gamestate and check for the switches that should be on, and if so reset the image.
The image is gone while the switch it still on?
Could you check if the switch is still ON but the screen image gone

If that's the case you could just call for the gamestate and check for the switches that should be on, and if so reset the image.
Okay, seems the image stays there even after I close and open the game as long as I keep the switch as a parallel process. Was that supposed to happen?
Okay, seems the image stays there even after I close and open the game as long as I keep the switch as a parallel process. Was that supposed to happen?
The background image should only show so long as the switch is on, if the switch is off the background image will not show.
The fact you added this in a parrellel process means it simply pushes the switch on, which in terms shows the image.

This probably happens because the background is not saved during the save process, meaning you need to trigger the script again after loading, which you did through a parrallel event.

Try this script instead :

JavaScript:
(function () {
    var backgroundSwitch = 0;
    var backgroundImage = '';

    var _Scene_MenuBase_createBackground = Scene_MenuBase.prototype.createBackground;
    Scene_MenuBase.prototype.createBackground = function() {
        _Scene_MenuBase_createBackground.call(this);
        this._backgroundSprite = new Sprite();
        this.addChild(this._backgroundSprite);
        this.updateBackground();
    };

    Scene_MenuBase.prototype.updateBackground = function() {
        if ($gameSwitches.value(backgroundSwitch)) {
            this._backgroundSprite.bitmap = ImageManager.loadPicture(backgroundImage);
        } else {
            this._backgroundSprite.bitmap = null;
        }
    };

    var _Scene_MenuBase_terminate = Scene_MenuBase.prototype.terminate;
    Scene_MenuBase.prototype.terminate = function() {
        _Scene_MenuBase_terminate.call(this);
        if (this._backgroundSprite) {
            this.removeChild(this._backgroundSprite);
            this._backgroundSprite.bitmap = null;
            this._backgroundSprite = null;
        }
    };

    var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
    Game_Interpreter.prototype.pluginCommand = function(command, args) {
        _Game_Interpreter_pluginCommand.call(this, command, args);
        if (command === 'SetMenuBackground') {
            backgroundSwitch = Number(args[0]);
            backgroundImage = String(args[1]);
            // Save the plugin data
            $gameSystem._menuBackgroundData = { switchId: backgroundSwitch, image: backgroundImage };
        }
    };

    // Load plugin data when the game is loaded
    var _DataManager_loadGame = DataManager.loadGame;
    DataManager.loadGame = function(savefileId) {
        var result = _DataManager_loadGame.call(this, savefileId);
        if (result && $gameSystem._menuBackgroundData) {
            backgroundSwitch = $gameSystem._menuBackgroundData.switchId;
            backgroundImage = $gameSystem._menuBackgroundData.image;
        }
        return result;
    };

    // Save plugin data when the game is saved
    var _DataManager_saveGame = DataManager.saveGame;
    DataManager.saveGame = function(savefileId) {
        if (!$gameSystem._menuBackgroundData) {
            $gameSystem._menuBackgroundData = { switchId: backgroundSwitch, image: backgroundImage };
        }
        return _DataManager_saveGame.call(this, savefileId);
    };
})();

This should do the job, even without the parrallel event (as you should avoid using those if applicable).
The background image should only show so long as the switch is on, if the switch is off the background image will not show.
The fact you added this in a parrellel process means it simply pushes the switch on, which in terms shows the image.

This probably happens because the background is not saved during the save process, meaning you need to trigger the script again after loading, which you did through a parrallel event.

Try this script instead :

JavaScript:
(function () {
    var backgroundSwitch = 0;
    var backgroundImage = '';

    var _Scene_MenuBase_createBackground = Scene_MenuBase.prototype.createBackground;
    Scene_MenuBase.prototype.createBackground = function() {
        _Scene_MenuBase_createBackground.call(this);
        this._backgroundSprite = new Sprite();
        this.addChild(this._backgroundSprite);
        this.updateBackground();
    };

    Scene_MenuBase.prototype.updateBackground = function() {
        if ($gameSwitches.value(backgroundSwitch)) {
            this._backgroundSprite.bitmap = ImageManager.loadPicture(backgroundImage);
        } else {
            this._backgroundSprite.bitmap = null;
        }
    };

    var _Scene_MenuBase_terminate = Scene_MenuBase.prototype.terminate;
    Scene_MenuBase.prototype.terminate = function() {
        _Scene_MenuBase_terminate.call(this);
        if (this._backgroundSprite) {
            this.removeChild(this._backgroundSprite);
            this._backgroundSprite.bitmap = null;
            this._backgroundSprite = null;
        }
    };

    var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
    Game_Interpreter.prototype.pluginCommand = function(command, args) {
        _Game_Interpreter_pluginCommand.call(this, command, args);
        if (command === 'SetMenuBackground') {
            backgroundSwitch = Number(args[0]);
            backgroundImage = String(args[1]);
            // Save the plugin data
            $gameSystem._menuBackgroundData = { switchId: backgroundSwitch, image: backgroundImage };
        }
    };

    // Load plugin data when the game is loaded
    var _DataManager_loadGame = DataManager.loadGame;
    DataManager.loadGame = function(savefileId) {
        var result = _DataManager_loadGame.call(this, savefileId);
        if (result && $gameSystem._menuBackgroundData) {
            backgroundSwitch = $gameSystem._menuBackgroundData.switchId;
            backgroundImage = $gameSystem._menuBackgroundData.image;
        }
        return result;
    };

    // Save plugin data when the game is saved
    var _DataManager_saveGame = DataManager.saveGame;
    DataManager.saveGame = function(savefileId) {
        if (!$gameSystem._menuBackgroundData) {
            $gameSystem._menuBackgroundData = { switchId: backgroundSwitch, image: backgroundImage };
        }
        return _DataManager_saveGame.call(this, savefileId);
    };
})();

This should do the job, even without the parrallel event (as you should avoid using those if applicable).
Yes! Now it works just as intended!

Original thread: https://forums.rpgmakerweb.com/threads/change-menu-background-using-switches.170398/