MV Map running in the background, see map through menu

I made a plugin last year that preserve the map scene when you open a menu, a shop or other similar scene.

Now I have an idea, maybe the map can update in the background while the menu is open?

Here's the code I'm using, you need to install PreserveMapScene.js first.
You can now also download it in prevous link, along with PreserveMapScene.js
Spoiler: Code
JavaScript:
/*:
 * @target MV MZ
 * @plugindesc Allow map update in the background of the menu, requires PreserveMapScene.js
 * @author utunnels
 *
 * @help No plugin commands.
 *
 * @param scenes
 * @text Available scenes
 * @type struct<SceneItem>[]
 * @default ["{\"scene\":\"Scene_Menu\",\"globalObjects\":\"[]\",\"layerObject\":\"_backgroundSprite\",\"enable\":\"true\"}","{\"scene\":\"Scene_Item\",\"globalObjects\":\"[]\",\"layerObject\":\"_backgroundSprite\",\"enable\":\"true\"}","{\"scene\":\"Scene_Skill\",\"globalObjects\":\"[]\",\"layerObject\":\"_backgroundSprite\",\"enable\":\"true\"}","{\"scene\":\"Scene_Equip\",\"globalObjects\":\"[]\",\"layerObject\":\"_backgroundSprite\",\"enable\":\"true\"}","{\"scene\":\"Scene_Status\",\"globalObjects\":\"[]\",\"layerObject\":\"_backgroundSprite\",\"enable\":\"true\"}","{\"scene\":\"Scene_Options\",\"globalObjects\":\"[]\",\"layerObject\":\"_backgroundSprite\",\"enable\":\"true\"}","{\"scene\":\"Scene_Save\",\"globalObjects\":\"[]\",\"layerObject\":\"_backgroundSprite\",\"enable\":\"true\"}","{\"scene\":\"Scene_Load\",\"globalObjects\":\"[]\",\"layerObject\":\"_backgroundSprite\",\"enable\":\"true\"}","{\"scene\":\"Scene_GameEnd\",\"globalObjects\":\"[]\",\"layerObject\":\"_backgroundSprite\",\"enable\":\"true\"}","{\"scene\":\"Scene_Shop\",\"globalObjects\":\"[]\",\"layerObject\":\"_backgroundSprite\",\"enable\":\"true\"}","{\"scene\":\"Scene_Name\",\"globalObjects\":\"[]\",\"layerObject\":\"_backgroundSprite\",\"enable\":\"true\"}","{\"scene\":\"Scene_Debug\",\"globalObjects\":\"[]\",\"layerObject\":\"_backgroundSprite\",\"enable\":\"true\"}","{\"scene\":\"Scene_Battle\",\"globalObjects\":\"[\\\"$gameTemp=new Game_Temp()\\\",\\\"$gameScreen=new Game_Screen()\\\",\\\"$gameMessage=new Game_Message()\\\"]\",\"layerObject\":\"_spriteset._backgroundSprite\",\"enable\":\"false\"}"]
 *
 */
/*~struct~SceneItem:
 * @param scene
 * @text Which scene?
 * @type combo
 * @option Scene_Menu
 * @option Scene_Item
 * @option Scene_Skill
 * @option Scene_Equip
 * @option Scene_Status
 * @option Scene_Options
 * @option Scene_Save
 * @option Scene_Load
 * @option Scene_GameEnd
 * @option Scene_Shop
 * @option Scene_Name
 * @option Scene_Debug
 * @option Scene_Battle
 *
 * @param globalObjects
 * @text Global objects made private
 * @desc Global objects you wish to make private while the map is served as background
 * @type combo[]
 * @option $gameTemp=new Game_Temp()
 * @option $gameScreen=new Game_Screen()
 * @option $gameMessage=new Game_Message()
 *
 * @param layerObject
 * @text Place before?
 * @desc Place the map before which display object? Leave blank to place it behind everything.
 * @type combo
 * @option _backgroundSprite
 * @option _spriteset._backgroundSprite
 *
 * @param enable
 * @desc What to switch it off but don't want to delete it? Try this.
 * @default true
 *
 */

(function(){

const script = document.currentScript.src.match(/\/([^\/]+)\.js/i)[1];
const param  = PluginManager.parameters(script);
var scenes = JSON.parse(param.scenes);
var scenesRef = {};
for(var i=0;i<scenes.length;i++){
  scenes[i] = JSON.parse(scenes[i]);
  scenes[i].globalObjects = JSON.parse(scenes[i].globalObjects);
  scenes[i].scene = eval(scenes[i].scene);
  scenesRef[scenes[i].scene.name] = scenes[i];
}

window.BgMapScenes = scenes;
window.BgMapScenesRef = scenesRef;

function clearFade(scene){
  var _this = scene;
  _this._fadeDuration = 0;
  _this._fadeOpacity = 0;//MZ
  if(_this._fadeSprite) _this._fadeSprite.opacity = 0;//MV
  if(_this.updateFade) _this.updateFade();//MV
  if(_this.updateColorFilter) _this.updateColorFilter();//MZ
}

Spriteset_Map.prototype.showCharacters = function() {
  for (var i = 0; i < this._characterSprites.length; i++) {
      this._characterSprites[i].show();
  }
};

function registerBgSceneUpdate(sceneClass) {
  var sceneClassCreate = sceneClass.prototype.create;
  var sceneClassTerminate = sceneClass.prototype.terminate;
  sceneClass.prototype.create = function () {
    if (!SceneManager.preserveScene) {
      throw "Plugin PreserveMapScene.js is not installed or not turned on!";
    }
    sceneClassCreate.apply(this, arguments);
    var si = scenesRef[sceneClass.name];
    this._useBgMap = eval(si.enable);
    var sm = SceneManager._preserved;
    if (this._useBgMap && sm) {
      //add bg map above this display object
      var obj = si.layerObject? eval('this.' + si.layerObject):null;
      var ind = 0;
      var parent = this;
      if(obj){
        ind = obj.parent.getChildIndex(obj) + 1;
        parent = obj.parent;
      }
      parent.addChildAt(sm, ind);
      //replace global variables, e.g. $gameMessage
      var keys = [];
      for(var gv of si.globalObjects){
        var pk = gv.split('=');
        var k = pk[0].trim();
        keys.push(k);
        sm[k] = eval(pk[1]);// new Game_Message()
        sm['$'+k] = window[k];
      }
      sm._spriteset.showCharacters();
      sm.update = function () {
        var sms = SceneManager._scene;
        SceneManager._scene = this;
        SceneManager._realScene = sms;
        for(var k of keys){
          window[k] = sm[k];
        }
        //a bit hack for battle scene
        if(sceneClass===Scene_Battle && !parent._firstUpdate){
           clearFade(this);
           parent._firstUpdate = true;
        }
        this.__proto__.update.apply(this, arguments);
        for(var k of keys){
          window[k] = sm['$'+k];
        }
        SceneManager._scene = sms;
      };
    }
  };
  sceneClass.prototype.terminate = function () {
    if (this._useBgMap && SceneManager._preserved) {
      //just be safe, prevent GC from collecting it?
      this._backgroundSprite.removeChild(SceneManager._preserved);
    }
    sceneClassTerminate.apply(this, arguments);
  };
}

for(var si of scenes){
  registerBgSceneUpdate(si.scene)
}

})();//(function(){
It is also dangerous to run two scenes at the same time, because there are lots of hard coded things in the engine. You will surely run into troubles if you trigger a battle or game over while the menu is open.

Plugin parameters:
Usually, they are not supposed to be chagned, unless you know what you're doing.
But just in case you have custom scenes.
It is disabled in battle scene by default, if you want it you can turn it on by yourself.
1740468703745.png


Term of use:
You can use it in any project, credit is not required but appreciated.

Demo project:
MZ only for now: https://utstudio.itch.io/battleonmapmzdemo
Cool idea. I’ve been thinking about something similar for my 8-bit project (using MZ), but limiting the scope of the background updates to just water tiles to maintain an illusion of movement rather than snapping a static background.

Dunno though, I haven’t put much thought into it other than just thinking it would be cool to have the water tiles continue to animate behind the menu.
I'm going to try this out. Right now, I have to queue up actions when I equip weapons/armors from the menu and this might save me a lot of hassle.
your plugin would be similar to CTB_MenuOnMap, but your code is
far easier, maybe an array to registerBgSceneUpdate can take the
array instead of object of scene rather than call it multiple times?

it is cleaner if it works nice, but how about custom scenes?
moghunters menu, equip etc (not that I use it) would it be easy
to add or by parameter struct?

so they can add that and it works?
The function updateBgScene updates the preserved map scene and take a screenshot.
The screenshot's bitmap then replaces the scene's _backgroundSprite's bitmap.

So as long as the scene has a _backgroundSprite it should work. Otherwise you need to modify the code and provide the background sprite object as an argument. You can even do that inside a battle scene if you modify the code, in theory.
Spoiler
1740111287940.png

Yeah I saw ctb menu a while back. It is like making lots of gab windows inside the map scene. I was thinking about a similar solution but eventually I dropped the idea because so many people use custom menu plugins nowadays. And there are other gab window plugins, hud plugins, etc.
Sorry for the double post.
I've given it some more thought, it is possible to create another scene using a script and add it to the map scene as a child. Since a scene is a container, it can also use transform like a sprite.

But it seems this method has lots of bugs, for isntance, the face images are missing! I guess that is because the pictures are not cached, the scene is not managed by SceneManager.
Also if you press esc, the game will close. I guess that is because there's only one scene in the scene stack.

So this is just an idea. It can't be done without modifying the SceneManager heavily.


1740143205358.png
So I take it this request to have the battle scene and map scene running at the same time is a non-starter?
It should be possible. But I guess the hard part is creating plugin commands to interact with the battle scene. The engine is created with the mindset there's only one scene at a time, lots of tests are required to check if there are bugs.

Here's the code I'm using (for MZ).
It is quite tricky. First you need to clear fadeout of the map scene. By default the screen fadeout before transfering you to the battle scene. Then you need to unhide map events. By default the game makes a screenshots without map events. But you can write a plugin to change the transition effect.

Spoiler
JavaScript:
function clearFade(scene){
  var _this = scene;
  _this._fadeDuration = 0;
  _this._fadeOpacity = 0;//MZ
  if(_this._fadeSprite) _this._fadeSprite.opacity = 0;//MV
  if(_this.updateFade) _this.updateFade();//MV
  if(_this.updateColorFilter) _this.updateColorFilter();//MZ
}

SceneManager.updateScene = function () {
  if (this._scene) {
    if (this._scene.isReady() && !this._scene.isStarted()) {
      this.onBeforeSceneStart();
      this._scene.start();
      this.onSceneStart();
    }
    if (this._scene.isStarted()) {
      if (this.isGameActive()) {
        this._scene.update();
      }
    }
  }
};

Bitmap.fastSnap = function (stage) {
  var width = Graphics.width;
  var height = Graphics.height;
  if (!this.fsBitmap) {
    this.fsBitmap = new Bitmap(width, height);
    //this.fsBitmap._setDirty = function () { };
    //this.fsBitmap._dirty = false;
    this.fsTexture = PIXI.RenderTexture.create(width, height);
    this.fsBitmap._baseTexture = this.fsTexture.baseTexture;
  }
  if (stage) {
    Graphics.app.renderer.render(stage, this.fsTexture);
    stage.worldTransform.identity();
  }
};

function updateBgScene(bgSprite) {
  var scene = SceneManager._scene;
  if (SceneManager._preserved) {
    bgSprite = bgSprite || scene._backgroundSprite || scene._spriteset._backgroundSprite;
    SceneManager._scene = SceneManager._preserved;
    SceneManager._preserved.update();
    if(scene instanceof Scene_Battle && !scene._firstUpdate){
      clearFade(SceneManager._preserved);
      SceneManager._preserved._spriteset.showCharacters();
      scene._firstUpdate = true;
    }
    if (bgSprite) {
      Bitmap.fastSnap(SceneManager._preserved);
      bgSprite.bitmap = Bitmap.fsBitmap;
      bgSprite.opacity = 255;
      bgSprite.filters = [];
    }
    SceneManager._scene = scene;
  }
}

function registerBgSceneUpdate(sceneClass){
  var alias = sceneClass.prototype.update;
  sceneClass.prototype.update = function () {
    alias.apply(this, arguments);
    updateBgScene();
  };
}

registerBgSceneUpdate(Scene_Battle);
Nice. Well if you think you can do it you can take that commission and get your $25
Hi, I'm currently using your plugin "Prevent lag on close menu" and it work great, but when I get the game over screen or I try to go back to title it crashes:
"this._preserved.onSceneTerminate is not a function"

Spoiler: screenshot
Untitled.png

Any idea how to solve this issue ?
Thanks again for your work
Hi, I'm currently using your plugin "Prevent lag on close menu" and it work great, but when I get the game over screen or I try to go back to title it crashes:
"this._preserved.onSceneTerminate is not a function"

Spoiler: screenshot

Any idea how to solve this issue ?
Thanks again for your work
Thank you for reporting this bug.
You can change line 32:
else this._preserved.onSceneTerminate();
To
else this.onSceneTerminate();

I will post a fix on itch later.
Thank you for reporting this bug.
You can change line 32:
else this._preserved.onSceneTerminate();
To
else this.onSceneTerminate();

I will post a fix on itch later.
Thank u!
and is there a script call I can use to clear the preserved scene before changing scene ?
It is managed by the plugin automatically. If you move to another map or return to the title screen, the previous preserved map will be terminated.
It is managed by the plugin automatically. If you move to another map or return to the title screen, the previous preserved map will be terminated.
It's because I use CGMZ Game Over and when there is a gameover and I reload a save directly with it the last scene isn't well cleared so it crashs with other light plugins (events who need to be cleared at scene change)

Maybe there's a way to clear the preserved scene ?
Maybe you can change line 138 so it doesn't preserve the map in game over scene.
if(sceneClass!=Scene_Map&&sceneClass!=Scene_Gameover){

To manually clear it you can run SceneManager.preserveScene(null); But I don't think you have a place to use it before you go to the load screen?
This works perfectly thank u !
Nice. Well if you think you can do it you can take that commission and get your $25
Haha, maybe just once for his project.
The solution is too alien to the engine. Lots of global objects are shared like $gameTemp,
$gameScreen, $gameMessage, etc. If you display a message in the battle scene, the map scene wants its copy as well. When showing a picture in the map, it appears in the battle scene too. Many events don't work, because the engine thinks it is in battle, so events don't exist.
I got around it by giving the background map its own $ objects.
Just made an update on the first post.
The code is more refined now. I removed the fastSnap workround, and added the preserved scene as a child of the menu scenes.
The code also works both on MV and MZ.
OK I have made this a full plugin (see the first post).
Parameters are added so the code becomes ugly again! But it just works.
I already miss the long list of registerBgSceneUpdate.

Edit*
Just got an idea to move battlers into the map. Or move message window into the shop scene.
But I'm not ready to make something useful out of this idea. Just test if they can be done using a map event.
Spoiler
1740472399114.png

1740472796284.png
New feature:
Map as a battlefield.

Unlike Chrono trigger, this is just an alternative sideview battle scene. There're no advanced functions to move the sprites.

Spoiler
1740579724227.png

1740579537366.png

Original thread: https://forums.rpgmakerweb.com/threads/map-running-in-the-background-see-map-through-menu.175674/