Can someone explain what's going on here? Specifically, why does loadMapData call loadDataFile (same function used to load the database) but also calls ResourceHandler.createLoader? What does that do, and why is it an addition for maps when it wasn't used for any of the database files?
Also, what does !!$dataMap do in the isMapLoaded method? $dataMap could either be a valid map or an empty map after loading, but I'm not sure what it is while the loading is happening. I don't see anywhere that it's reset to null when changing maps, so why doesn't it still contain its previous contents while the loading is happening?
It looks like $dataMap is populated before the metadata is extracted, so isMapLoaded could return true before onLoad has finished running - is that correct?
My goal is to load some more stuff after the metadata is extracted, and have isMapLoaded only return true after that has been loaded as well.
Suggestions welcome.
Also, what does !!$dataMap do in the isMapLoaded method? $dataMap could either be a valid map or an empty map after loading, but I'm not sure what it is while the loading is happening. I don't see anywhere that it's reset to null when changing maps, so why doesn't it still contain its previous contents while the loading is happening?
It looks like $dataMap is populated before the metadata is extracted, so isMapLoaded could return true before onLoad has finished running - is that correct?
My goal is to load some more stuff after the metadata is extracted, and have isMapLoaded only return true after that has been loaded as well.
Suggestions welcome.
Code:
DataManager.loadDataFile = function(name, src) {
var xhr = new XMLHttpRequest();
var url = 'data/' + src;
xhr.open('GET', url);
xhr.overrideMimeType('application/json');
xhr.onload = function() {
if (xhr.status < 400) {
window[name] = JSON.parse(xhr.responseText);
DataManager.onLoad(window[name]);
}
};
xhr.onerror = this._mapLoader || function() {
DataManager._errorUrl = DataManager._errorUrl || url;
};
window[name] = null;
xhr.send();
};
DataManager.isDatabaseLoaded = function() {
this.checkError();
for (var i = 0; i < this._databaseFiles.length; i++) {
if (!window[this._databaseFiles[i].name]) {
return false;
}
}
return true;
};
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();
}
};
DataManager.makeEmptyMap = function() {
$dataMap = {};
$dataMap.data = [];
$dataMap.events = [];
$dataMap.width = 100;
$dataMap.height = 100;
$dataMap.scrollType = 3;
};
DataManager.isMapLoaded = function() {
this.checkError();
return !!$dataMap;
};
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);
}
}
}
if (object === $dataSystem) {
Decrypter.hasEncryptedImages = !!object.hasEncryptedImages;
Decrypter.hasEncryptedAudio = !!object.hasEncryptedAudio;
Scene_Boot.loadSystemImages();
}
};