MZ Return Location from Menu

What controls the return location from the menu to the game map?


Details:

I am working on my plugin and find that if I use the menu and exit it changes my player's position on the map. I am not familiar enough with the menu to yet understand how it stores the player's location, which is obviously not in sync with the player's current position because of my manipulations of the gameMap. It is odd because it warps me back to the originally loaded map and that data doesn't exist anymore, so I guess the menu reloads the dataMap?
What controls the return location from the menu to the game map?


Details:

I am working on my plugin and find that if I use the menu and exit it changes my player's position on the map. I am not familiar enough with the menu to yet understand how it stores the player's location, which is obviously not in sync with the player's current position because of my manipulations of the gameMap. It is odd because it warps me back to the originally loaded map and that data doesn't exist anymore, so I guess the menu reloads the dataMap?
Quite simply it doesn't, but that's because it doesn't load another map.
So the $gameMap object & $gamePlayer objects data isn't changed & as you said the dataMap also remains the same, which means it doesn't need to store any information. It just pushes the scene list to become the menu scene while still keeping the map scene. Then when the menu is canceled it just pops the scene from the list and resumes with the map scene.

That said... I ran into an issue with this before also where I use the Scene_Map as a base & that of course caused all the issues you are running into now.

It's a bit tricky honestly but it definitely did the job for me...

...Basically we store temporary transfer back to point data for the player to transfer back to once we are done with our new map like scene.

I think it's just easier to show the code, explaining it all is kind of hard lol
Here is a demo of a solution to this issue.

..Below is just a simple blank map type of scene that creates a choice window and stores the chosen text to Game Variable #1...
Very basic example:
Spoiler: Code
Code:
//=============================================================================
// RPG Maker MZ - CT_Bolt's Map Scene Type Demo
//=============================================================================

/*:
 * @target MZ
 * @plugindesc [RPG Maker MZ] [Tier 1] [Version 1.00] [CT_Bolt - Map Scene Type Demo]
 * @author CT_Bolt
 * @url https://ct-bolt.itch.io/
 *
 * @help CTB_MapSceneTypeDemoMZ.js
 *
 */
 
var CTB = CTB || {}; CTB.MapSceneTypeDemo  = CTB.MapSceneTypeDemo || {};
var Imported = Imported || {}; Imported["CTB_MapSceneTypeDemo"] = 1.00;

(($_$) => {
    const NAMESPACE   = 'MapSceneTypeDemo';
    const PLUGIN_NAME = 'CTB_' + NAMESPACE;
 
    $_$['Game_Map.prototype.tileset'] = Game_Map.prototype.tileset;
    Game_Map.prototype.tileset = function() {
        var v = $_$['Game_Map.prototype.tileset'].apply(this, arguments);
        if (!v){v = {id: 0, flags: [], mode: 1, name: "", note: "", tilesetNames: [], meta: {}}};
        return v;
    };
 
    $_$['Scene_Map.prototype.create'] = Scene_Map.prototype.create;
    Scene_Map.prototype.create = function() {
        $_$['Scene_Map.prototype.create'].apply(this, arguments);
        $gameSystem._transferBackTo = null;
    };
 
    $_$['Window_MapName.prototype.open'] = Window_MapName.prototype.open;
    Window_MapName.prototype.open = function() {
        if (!$gameSystem._dontDisplayMapName){
            $_$['Window_MapName.prototype.open'].apply(this, arguments);
        };
        $gameSystem._dontDisplayMapName = false;
    };

    $_$['Scene_MapTypeDemo.prototype.onMapLoaded'] = Scene_MapTypeDemo.prototype.onMapLoaded;
    Scene_MapTypeDemo.prototype.onMapLoaded = function() {
    if (SceneManager._previousClass === Scene_Map){
        $_$['Scene_MapTypeDemo.prototype.onMapLoaded'].apply(this, arguments);
    }else{
        if (this._transfer) {
            $gamePlayer.performTransfer();
        }
        this.createDisplayObjects();
    };
};
 
})(CTB.MapSceneTypeDemo, this);

//-----------------------------------------------------------------------------
// Scene_MapTypeDemo
//
// The scene class of a map-like scene.

function Scene_MapTypeDemo() {
    this.initialize(...arguments);
}

Scene_MapTypeDemo.prototype = Object.create(Scene_Map.prototype);
Scene_MapTypeDemo.prototype.constructor = Scene_MapTypeDemo;

Scene_MapTypeDemo.prototype.initialize = function() {
    Scene_Map.prototype.initialize.call(this);
};

Scene_MapTypeDemo.prototype.create = function() {
    Scene_Message.prototype.create.call(this);
    $gameSystem._transferBackTo = $gameSystem._transferBackTo || {mapId: $gameMap._mapId, x: $gamePlayer._x, y: $gamePlayer._y};
    $gameSystem._dontDisplayMapName = true;
    $gamePlayer.reserveTransfer(-1, 1, 1, 0, 2);
    this._transfer = $gamePlayer.isTransferring();
    this._lastMapWasNull = !$dataMap;
    if (this._transfer) {
        DataManager.loadMapData($gamePlayer.newMapId());
        this.onTransfer();
    } else if (!$dataMap || $dataMap.id !== $gameMap.mapId()) {
        DataManager.loadMapData($gameMap.mapId());
    }
};

Scene_MapTypeDemo.prototype.onTransferEnd = function() {};

Scene_MapTypeDemo.prototype.updateMain = function() {
    $gameMap.update(this.isActive());
    $gameScreen.update();
};

Scene_MapTypeDemo.prototype.isPlayerActive = function() {
    return false;
};

Scene_MapTypeDemo.prototype.isFastForward = function() {
    return false;
};

Scene_MapTypeDemo.prototype.updateMenuButton = function() {};

Scene_MapTypeDemo.prototype.isMenuEnabled = function() {
    return false;
};

Scene_MapTypeDemo.prototype.isMapTouchOk = function() {
    return false;
};

Scene_MapTypeDemo.prototype.processMapTouch = function() {};

Scene_MapTypeDemo.prototype.isAnyButtonPressed = function() {
    return false;
};

Scene_MapTypeDemo.prototype.onMapTouch = function() {};

Scene_MapTypeDemo.prototype.isSceneChangeOk = function() {
    return this.isActive() && !$gameMessage.isBusy();
};

Scene_MapTypeDemo.prototype.createDisplayObjects = function() {
    this.createSpriteset();
    this.createWindowLayer();
    this.createAllWindows();
    this.createButtons();
 
    this.createChoiceWindow();
    this._itemWindow.activate();
};

Scene_MapTypeDemo.prototype.createChoiceWindow = function() {
    this._itemWindow = new Window_ChoiceList();
    $gameMessage.setChoices(['Choice A', 'Choice B', 'Choice C', 'Cancel'], 0, 3);
    $gameMessage.setChoiceBackground(0);
    $gameMessage.setChoicePositionType(1);
    $gameMessage.setChoiceCallback(n => {
        $gameVariables.setValue(1, $gameMessage.choices()[n]);
        this.onItemCancel();
    }, this);
    this.addWindow(this._itemWindow);
};

Scene_MapTypeDemo.prototype.onItemCancel = function() {
    if (SceneManager._previousClass === Scene_Map){
        $gamePlayer.reserveTransfer($gameSystem._transferBackTo.mapId, $gameSystem._transferBackTo.x, $gameSystem._transferBackTo.y, 0, 2);
    }else{
        SceneManager.goto(SceneManager._previousClass)
    };
};

For this small demo example i've just called from an event on the map like this:
Spoiler
1652672984975.png
(of course same thing can be done from a command on the menu, just have to setup the menu command)

Looks like this in-game:
Spoiler: In-Game Demo Video


Attach this plugin for convenience 😉

Hope that helps and at least provides some insight for ya.
Quite simply it doesn't, but that's because it doesn't load another map.
So the $gameMap object & $gamePlayer objects data isn't changed & as you said the dataMap also remains the same, which means it doesn't need to store any information. It just pushes the scene list to become the menu scene while still keeping the map scene. Then when the menu is canceled it just pops the scene from the list and resumes with the map scene.

That said... I ran into an issue with this before also where I use the Scene_Map as a base & that of course caused all the issues you are running into now.

It's a bit tricky honestly but it definitely did the job for me...

...Basically we store temporary transfer back to point data for the player to transfer back to once we are done with our new map like scene.

I think it's just easier to show the code, explaining it all is kind of hard lol
Here is a demo of a solution to this issue.

..Below is just a simple blank map type of scene that creates a choice window and stores the chosen text to Game Variable #1...
Very basic example:
Spoiler: Code
Code:
//=============================================================================
// RPG Maker MZ - CT_Bolt's Map Scene Type Demo
//=============================================================================

/*:
 * @target MZ
 * @plugindesc [RPG Maker MZ] [Tier 1] [Version 1.00] [CT_Bolt - Map Scene Type Demo]
 * @author CT_Bolt
 * @url https://ct-bolt.itch.io/
 *
 * @help CTB_MapSceneTypeDemoMZ.js
 *
 */
 
var CTB = CTB || {}; CTB.MapSceneTypeDemo  = CTB.MapSceneTypeDemo || {};
var Imported = Imported || {}; Imported["CTB_MapSceneTypeDemo"] = 1.00;

(($_$) => {
    const NAMESPACE   = 'MapSceneTypeDemo';
    const PLUGIN_NAME = 'CTB_' + NAMESPACE;
 
    $_$['Game_Map.prototype.tileset'] = Game_Map.prototype.tileset;
    Game_Map.prototype.tileset = function() {
        var v = $_$['Game_Map.prototype.tileset'].apply(this, arguments);
        if (!v){v = {id: 0, flags: [], mode: 1, name: "", note: "", tilesetNames: [], meta: {}}};
        return v;
    };
 
    $_$['Scene_Map.prototype.create'] = Scene_Map.prototype.create;
    Scene_Map.prototype.create = function() {
        $_$['Scene_Map.prototype.create'].apply(this, arguments);
        $gameSystem._transferBackTo = null;
    };
 
    $_$['Window_MapName.prototype.open'] = Window_MapName.prototype.open;
    Window_MapName.prototype.open = function() {
        if (!$gameSystem._dontDisplayMapName){
            $_$['Window_MapName.prototype.open'].apply(this, arguments);
        };
        $gameSystem._dontDisplayMapName = false;
    };

    $_$['Scene_MapTypeDemo.prototype.onMapLoaded'] = Scene_MapTypeDemo.prototype.onMapLoaded;
    Scene_MapTypeDemo.prototype.onMapLoaded = function() {
    if (SceneManager._previousClass === Scene_Map){
        $_$['Scene_MapTypeDemo.prototype.onMapLoaded'].apply(this, arguments);
    }else{
        if (this._transfer) {
            $gamePlayer.performTransfer();
        }
        this.createDisplayObjects();
    };
};
 
})(CTB.MapSceneTypeDemo, this);

//-----------------------------------------------------------------------------
// Scene_MapTypeDemo
//
// The scene class of a map-like scene.

function Scene_MapTypeDemo() {
    this.initialize(...arguments);
}

Scene_MapTypeDemo.prototype = Object.create(Scene_Map.prototype);
Scene_MapTypeDemo.prototype.constructor = Scene_MapTypeDemo;

Scene_MapTypeDemo.prototype.initialize = function() {
    Scene_Map.prototype.initialize.call(this);
};

Scene_MapTypeDemo.prototype.create = function() {
    Scene_Message.prototype.create.call(this);
    $gameSystem._transferBackTo = $gameSystem._transferBackTo || {mapId: $gameMap._mapId, x: $gamePlayer._x, y: $gamePlayer._y};
    $gameSystem._dontDisplayMapName = true;
    $gamePlayer.reserveTransfer(-1, 1, 1, 0, 2);
    this._transfer = $gamePlayer.isTransferring();
    this._lastMapWasNull = !$dataMap;
    if (this._transfer) {
        DataManager.loadMapData($gamePlayer.newMapId());
        this.onTransfer();
    } else if (!$dataMap || $dataMap.id !== $gameMap.mapId()) {
        DataManager.loadMapData($gameMap.mapId());
    }
};

Scene_MapTypeDemo.prototype.onTransferEnd = function() {};

Scene_MapTypeDemo.prototype.updateMain = function() {
    $gameMap.update(this.isActive());
    $gameScreen.update();
};

Scene_MapTypeDemo.prototype.isPlayerActive = function() {
    return false;
};

Scene_MapTypeDemo.prototype.isFastForward = function() {
    return false;
};

Scene_MapTypeDemo.prototype.updateMenuButton = function() {};

Scene_MapTypeDemo.prototype.isMenuEnabled = function() {
    return false;
};

Scene_MapTypeDemo.prototype.isMapTouchOk = function() {
    return false;
};

Scene_MapTypeDemo.prototype.processMapTouch = function() {};

Scene_MapTypeDemo.prototype.isAnyButtonPressed = function() {
    return false;
};

Scene_MapTypeDemo.prototype.onMapTouch = function() {};

Scene_MapTypeDemo.prototype.isSceneChangeOk = function() {
    return this.isActive() && !$gameMessage.isBusy();
};

Scene_MapTypeDemo.prototype.createDisplayObjects = function() {
    this.createSpriteset();
    this.createWindowLayer();
    this.createAllWindows();
    this.createButtons();
 
    this.createChoiceWindow();
    this._itemWindow.activate();
};

Scene_MapTypeDemo.prototype.createChoiceWindow = function() {
    this._itemWindow = new Window_ChoiceList();
    $gameMessage.setChoices(['Choice A', 'Choice B', 'Choice C', 'Cancel'], 0, 3);
    $gameMessage.setChoiceBackground(0);
    $gameMessage.setChoicePositionType(1);
    $gameMessage.setChoiceCallback(n => {
        $gameVariables.setValue(1, $gameMessage.choices()[n]);
        this.onItemCancel();
    }, this);
    this.addWindow(this._itemWindow);
};

Scene_MapTypeDemo.prototype.onItemCancel = function() {
    if (SceneManager._previousClass === Scene_Map){
        $gamePlayer.reserveTransfer($gameSystem._transferBackTo.mapId, $gameSystem._transferBackTo.x, $gameSystem._transferBackTo.y, 0, 2);
    }else{
        SceneManager.goto(SceneManager._previousClass)
    };
};

For this small demo example i've just called from an event on the map like this:
(of course same thing can be done from a command on the menu, just have to setup the menu command)

Looks like this in-game:
Spoiler: In-Game Demo Video


Attach this plugin for convenience 😉

Hope that helps and at least provides some insight for ya.


Thanks! By knowing that it doesn't store the information and pointing me to the SceneManager I was able to fix my issue.

Root Cause: I was not updating the $gameMap_mapId.

Detailed Answer:

I am combining multiple maps together for my plugin and when a player walks onto a different section of the map I am updating the $dataMap.mapId to be consistent with the current location (Just in case a player transfer is needed).

The SceneManager calls create() on the gameMap scene when it switches back to it. If the player's mapId doesn't match the mapId in gameMap then a player transfer is initiated.

In order to avoid the transfer I needed to update the $gameMap._mapId when I load new maps.

Note: Your solution would have worked as well, but I was lucky enough to find a solution that prevented the issue in the first place. If anyone else has this issue just know that you can't have any events on the map when you update the mapId. I am clearing mine every time I walk to a new map, so I was able to avoid this.
Thanks! By knowing that it doesn't store the information and pointing me to the SceneManager I was able to fix my issue.

Root Cause: I was not updating the $gameMap_mapId.

Detailed Answer:

I am combining multiple maps together for my plugin and when a player walks onto a different section of the map I am updating the $dataMap.mapId to be consistent with the current location (Just in case a player transfer is needed).

The SceneManager calls create() on the gameMap scene when it switches back to it. If the player's mapId doesn't match the mapId in gameMap then a player transfer is initiated.

In order to avoid the transfer I needed to update the $gameMap._mapId when I load new maps.

Note: Your solution would have worked as well, but I was lucky enough to find a solution that prevented the issue in the first place. If anyone else has this issue just know that you can't have any events on the map when you update the mapId. I am clearing mine every time I walk to a new map, so I was able to avoid this.
Awesome! Glad to help out & very glad to hear you got it working as needed.
Hope the rest of your project goes super smooth for ya 🙂 It's pretty awesome looking so far and cool idea too ☺️

Happy Plugin Making! :LZSgrin:

Original thread: https://forums.rpgmakerweb.com/threads/return-location-from-menu.147635/