Question about map loading mechanism

Greetings.
I am new at RMMV, and aims to develop a (simple) rougelike game. To achieve this, first I have to understand the map loading mechanism... (since rougelike style indicates highly random map)

I know how to program with javascript, and had read this post about understanding the map structure.
https://forums.rpgmakerweb.com/index.php?threads/understanding-map-data.83140/

Also, I know how to store map data into game variables and restore it when in need.
But my problem is, I don't know how the map loading mechanism works. For example, I have a map looks like this:
rmmv.png

And I want the map turn into something like this (just for exercise) after triggered some events:
rmmv.png

Actually, I can already achieve that by modifying $dataMap.data by running some javascript codes. However, the map changes back immediately after I opened & closed menu.

So my question is, how can I replace a map data when entering area, also keep it the same when I open/close menu, or after other possible actions? (map data can be generated by algorithm)
I presume you're talking about changing the tileset of the map. Rather than use $dataMap you might consider using $gameMap. eg.

Code:
$gameMap._tilesetId = 2;
$dataMap (like other globals starting with $data) is loaded directly from the relevant .json file in the project's data directory; it's intended to be static, used for setting up the map, referencing notetags, etc. From what you've said, I guess the game must reload the map data from file when you go from Scene_Menu back to Scene_Map.

$gameMap (like other globals starting with $game) stores the "dynamic" data for the current map, primarily event stuff. This is saved, along with the other $game variables, to save files (cf. rpg_managers.js, StorageManager handles save file access).

I think the typical approach for procedural map generation is to write to file, then transfer to the corresponding map ID (or refresh if already there). I don't think RMMV has any routines for writing to data files, though, so you'd need to write your own and probably wouldn't be able to deploy to web format due to sandboxing.

As an aside: if you just want to change the tileset, there's the Change Tileset event command, which handles all the image reservation and map refresh stuff. As with other event commands, the code behind it can be found in rpg_objects.js, just search for Change Tileset. 🙏
To Silva:
I don't need to change tilesetId, instead I need to change $dataMap.data or $gameMap.data values, so I can implement some map changing events after action (e.g. after perform digging action, a wall may disappear)

To caethyril:
Thanks for $dataMap & $gameMap concept explanation! When talking about $dataMap.data v.s. $gameMap.data(), the code looks like this when I explored them using google chrome debugging tool:
Code:
$gameMap.data
ƒ () {
    return $dataMap.data;
}

Looks like $gameMap.data() just returns $dataMap.data, so I guess they are just the same?

As you mentioned, If I generate new map files and store them using default json format, the game would not work through web service, which I don't want that to happen. I will try to store them in $gameVariables first...

Indeed, when the screen goes back from Scene_Menu to Scene_Map, game engine just reloaded the entire map (through Scene_Map.initialize/create in rpg_scenes.js). Based on this, I found a function DataManager.loadMapData() in rpg_manager.js file:
Code:
DataManager.loadMapData = function(mapId) {
    if (mapId > 0) {
        var filename = 'Map%1.json'.format(mapId.padZero(3));
        this._mapLoader = ResourceHandler.createLoader('data/' + filename, this.loadDataFile.bind(this, '$dataMap', filename));
        this.loadDataFile('$dataMap', filename);
    } else {
        this.makeEmptyMap();
    }
};

Originally I thought "this.loadDataFile" will fill the variable $dataMap, so I added a log under this.loadDataFile line:
Code:
console.log("$dataMap: " + $dataMap);
However, the result shows that $dataMap is still null at this point. Any idea about where this variable being assigned?
I think its because the data files for the $dataX variables are actually loaded asynchronously, so at the time the log was called, the data might actually not yet be fully loaded into the variable.
Code:
$gameMap.data
ƒ () {
    return $dataMap.data;
}

Looks like $gameMap.data() just returns $dataMap.data, so I guess they are just the same?
Yes and no. The values are identical, since the former is a getter for the latter. As you can see, though, the actual values are defined on $dataMap. The values are not saved as part of $gameMap. 🙏
As you mentioned, If I generate new map files and store them using default json format, the game would not work through web service, which I don't want that to happen. I will try to store them in $gameVariables first...
Sounds like a good idea! Then you can simply alias the loadMapData method to grab generated maps from $gameVariables rather than load from external files. One possible issue: could cause save bloat. I imagine it'll be OK unless you store a lot of generated maps simultaneously and/or make huge maps. May need to experiment a little, there. 😴
...I found a function DataManager.loadMapData() in rpg_manager.js file:
Code:
DataManager.loadMapData = function(mapId) {
    if (mapId > 0) {
        var filename = 'Map%1.json'.format(mapId.padZero(3));
        this._mapLoader = ResourceHandler.createLoader('data/' + filename, this.loadDataFile.bind(this, '$dataMap', filename));
        this.loadDataFile('$dataMap', filename);
    } else {
        this.makeEmptyMap();
    }
};

Originally I thought "this.loadDataFile" will fill the variable $dataMap, so I added a log under this.loadDataFile line:
Code:
console.log("$dataMap: " + $dataMap);
However, the result shows that $dataMap is still null at this point. Any idea about where this variable being assigned?
Like @Engr. Adiktuzmiko says, RMMV loads resources asynchronously via XMLHttpRequest. The request's onload method, defined under DataManager.loadDataFile (rpg_managers.js), is responsible for actual assignment.

Edit: if you want to check when the map is loaded, I suggest investigating the onMapLoaded method of Scene_Map (rpg_scenes.js). 😤
To Engr. Adiktuzmiko and caethyril:
Thanks a lot! Surely the problem is that resources were loaded asynchronously, so I can't find them immediately. I added some logs in function DataManager.isMapLoaded(), and found that it might be called 2~3 times upon a map reload, so I tried to add some check & replace code inside:
Code:
DataManager.isMapLoaded = function() {
    if ($dataMap && $gameVariables[1]) {
        $dataMap.data = $gameVariables[1];
    }
    this.checkError();
    return !!$dataMap;
};

After filled $gameVariables[1] with map data, I can make the map change after closing menu.
Furthermore, I did some research on how to redraw the map immediately after I updated some map data, and so far I found a safe way to do this: (Others will cause exception and so the game crashed)
Code:
SceneManager.goto(Scene_Map);

Combined those techniques, I can make the map redraw immediately after an event updated! Thanks for your help~~ = )
(To caethyril: Thank you for those data structures explanation, I really need and appreciate that 😀)

Original thread: https://forums.rpgmakerweb.com/threads/question-about-map-loading-mechanism.110094/