load map.json, assign to variable

In VX using Ruby, loading a map and assigning it to a variable was so simple and easy:

map_id = 1@map = load_data(sprintf("Data/Map%03d.rvdata2", map_id))I have some issues doing the same thing in MV. Like probably many of you, I'm inexperience with javascript and web programming, I'll surely learn the trick of the trade, but I'm dealing with a serious deadline right now, and I need to migrate to MV as soon as possible. It's probably one of the only challenge I've met thus far.

Here's some default code snippets from MV to put everyone on the right track.

DataManager.loadMapData = function() {    if (mapId > 0) {        var filename = 'Map%1.json'.format(mapId.padZero(3));        this.loadDataFile('$dataMap', filename);    } else {        this.makeEmptyMap();    }};This is probably the most overwhelming part for most people unfamiliar with web programming.

DataManager.loadDataFile = function(object, filename) {    var xhr = new XMLHttpRequest();    var url = 'data/' + filename;    xhr.open('GET', url);    xhr.overrideMimeType('application/json');    xhr.onload = function() {        if (xhr.status < 400) {            window[object] = JSON.parse(xhr.responseText);            DataManager.onLoad(window[object]);        }    };    xhr.onerror = function() {        DataManager._errorUrl = DataManager._errorUrl || url;    };    window[object] = null;    xhr.send();};Here again, $dataMap make this function inflexible. 

Code:
DataManager.onLoad = function(object) {    var array;    if (object === $dataMap) {        this.extractMetadata(object);        array = object.events;    } else {        array = object;    }    if (Array.isArray(array)) {        for (var i = 0; i < array.length; i++) {            var data = array[i];            if (data && data.note !== undefined) {                this.extractMetadata(data);            }        }    }};

So well, I'm wondering if anyone can put together a quick function to assign a map to a variable and then read its properties. Example: loaded_map.width, etc. My current tests didn't work. 

Thank you for reading and investigating this topic with me. Anyone helping can be credited as special thanks in my game, if interested. (find more about it here: www.blossomsoft.com/ )
Here's what I tried to do thus far as a test:

Code:
DataManager.loadDataFile_2 = function(object, filename) {    var xhr = new XMLHttpRequest();    var url = 'data/' + filename;    xhr.open('GET', url);    xhr.overrideMimeType('application/json');    xhr.onload = function() {        if (xhr.status < 400) {            window[object] = JSON.parse(xhr.responseText);            DataManager.onLoad_zzz(window[object]);        }    };    xhr.onerror = function() {        DataManager._errorUrl = DataManager._errorUrl || url;    };    window[object] = null;    xhr.send();};
Code:
DataManager.onLoad_2 = function(object) {    var array;        this.extractMetadata(object);    array = object.events;            if (Array.isArray(array)) {        for (var i = 0; i < array.length; i++) {            var data = array[i];            if (data && data.note !== undefined) {                this.extractMetadata(data);            }        }    }    };
Code:
	var mapId = 1;	var my_map = {};	var filename = 'Map%1.json'.format(mapId.padZero(3));		DataManager.loadDataFile_2('my_map', filename);		var test = my_map.width; // map doesn't seem assigned.
Please, if you need to load a file, dont use the default loading functions

They are terrible, and only create global variables. You will need, at the very least, a plugin that allows you to load data into the game.

At this time, the only plugin I can think of off the top of my head is the MV Commons plugin. However, as I'm on my phone, I cannot link it to you. Please search for, and download that plugin.

Part of that plugin is some code that lets us download data, which you can to get the map itself. Simply use MVC.ajaxLoadFile("data/Map001.json"), and you will have the map data for map 1.

PLEASE NOTE: This will not give you a Game_Map object. Doing that is considerably more complicated. But this will give you enough to get the raw map data, including the map width and height. You'll be able to access it similarly as in Ace. Whatever variable name you use to store the map data, just use ".width" and ".height" to get the width and bright, respectively.
This is the basic function i'm using for loading files:

Code:
var loadFile = function(url, callback) {    var xhr = new XMLHttpRequest();    xhr.open('GET', url);    xhr.overrideMimeType('application/json');    xhr.onload = function() { callback(JSON.parse(xhr.responseText)); };    xhr.onerror = function() { throw new Error("There was an error loading the file '" + url + "'."); };    xhr.send();};
You can call it like this:
Code:
var map;loadFile('data/Map001.json', function(data) { map = data; });
and it will store the loaded map data in "map", once it finishes.
Zalerinian, wonderful, I will definitely check out this plugin.

Iavra, wow you just saved my life tonight!

Thanks a bunch for your replies!
Actually, Iavra, there's a small issue. 

based on your example, 

var map;loadFile('data/Map001.json', function(data) { map = data; });if I try to get map.width, it return me this when I store the value in a string to check the value, rather than returning a number

function() { return $gameMap.width; }So how do I get the properties from your example? 
I just tried it and it works. Was something else saved in map beforehand? The data is loaded asynchronously, so you'll need to wait until it finishes or write all logic that deals with it in the callback. If you know beforehand, what data you need, i suggest loading it in Scene_Boot.prototype.create and return false in Scene_Boot.prototype.isReady untile the data is loaded.
Thanks for the support, Iavra. I didn't know the data is loaded asynchronously. 

Zalerinian, I'm currently investigating your document and it looks very interesting.
I just tried it and it works. Was something else saved in map beforehand? The data is loaded asynchronously, so you'll need to wait until it finishes or write all logic that deals with it in the callback. If you know beforehand, what data you need, i suggest loading it in Scene_Boot.prototype.create and return false in Scene_Boot.prototype.isReady untile the data is loaded.
this is interesting... any example on how to know that data is finish loaded?

i notice that when using asynchronous load and data not finish loading when we changing map... sometimes it throw $dataMap null error.
In my example, as long as map === undefined, it's not loaded. I never got the $dataMap error, but i never change maps, because my "game" only has a single test map 😀

Original thread: https://forums.rpgmakerweb.com/threads/load-map-json-assign-to-variable.51288/