If I start in map 1, and I put this line in an event in a script call:
DataManager.loadMapData(2);The whole thing crashes.
Probably because it overwrites the data for the current map I guess. I can't find where this happens though. It does not seem to happen in the DataManager (unless an empty map is created).
I somehow need to load the map-data for another map without causing any problems for the current map AND wait for it to load with DataManager.isMapLoaded().
In Ace we would just write:
mapData = load_data("Data/Map" + map_id + ".rvdata2")and all is well and the mapData instantly contains all the data without having to wait for some other 'thread'.
Do I need to make a new DataManager.loadDataFile()? I'm probably overlooking something really simple somewhere.
Take a look at how DataManager.loadMapData works and copy that function. Also extend DataManager.isMapLoaded to wait for your own function to finish (have an internal variable for the map data you are loading, set it to null when your function starts and have DataManager.isMapLoaded check for data !== null).
Thanks I got it working.
Okay so they used a public variable called "window", that's why I couldn't find it. So I didn't miss anything at all then. This is messy as hell... So the RPG Maker devs made a function that calls an asynchronous function to load the map using a public variable. BUT only support loading one map at a time AND do not support loading another map not even after it finished loading. Then continuously check a value that is set through a callback to test if it's done... That's just... Extremely bad design or ?
So basically I just rewrote it so that it actually support all that and then in some update-method I check if all the maps are fully loaded which are set through a callback... Once again I'm severely disappointed in (both) RPG Maker and Javascript... This design is beyond bad, I must have understood something wrong, not even the RPG Maker devs can make somethings this ugly.
But Javascript doesn't even seem to support waiting for asynchronous methods. It only has callbacks so we all end up using dirty hacks around this, as usual.
I had to rewrite:
loadDataFile()
isMapLoaded()
onLoad()
checkError()
and switch out two public variables.
Just to load another map.
Won't work. Some of the other methods have their variables 'hardcoded', like checkError(). You gotta rewrite or create new methods for the all of those I posted. No way around it (that I know of) to make it truly asynchronous to allow the loading of 1 or more maps at the same time.
We are forced to rewrite or create new (better) methods.
Found another weird piece of RM-code while attempting to locate where the data is converted to actual map-tiles:
Game_Map.prototype.layeredTiles = function(x, y) { var tiles = []; for (var i = 0; i < 4; i++) { tiles.push(this.tileId(x, y, 3 - i)); } return tiles;};Why not just write:
Game_Map.prototype.layeredTiles = function(x, y) { var tiles = []; for (var i = 3; i >= 0; i--) { tiles.push(this.tileId(x, y, i)); } return tiles;};Saves a subtract calculation and makes more sense when reading.
What exactly do you want to accomplish with that? "checkError" doesn't matter, because you can throw the error directly, once the map load fails.
I already got it working anyway. It's just that I don't understand why the RM-code is so limited or oddly designed sometimes combined with Javascript's limitations, it drives me nuts. MV is a big step up from Ace, but it still has these 'argh-moments'.
I want to load multiple maps into memory simultaneously without overwriting each other or any problems whatsoever. And that I got working thanks to your first reply.
Loading a file is simple, there's no need to extend all those functions. In my Achievements plugin, for example, i want to load a file at startup and have Scene_Boot.isReady return false, as long as it hasn't been loaded. I do it like this:
(function($) { var alias_create = $.prototype.create; $.prototype.create = function() { alias_create.call(this); _loadFile(_param_configuration, _create); }; var alias_isReady = $.prototype.isReady; $.prototype.isReady = function() { return !!_achievements && alias_isReady.call(this); }; })(Scene_Boot); var _loadFile = function(url, callback) { var request = new XMLHttpRequest(); request.open('GET', _param_configuration); request.overrideMimeType('application/json'); request.onload = function() { callback(JSON.parse(request.responseText)); } request.onerror = function() { throw new Error('There was an error loading the file ' + url); } request.send();};
The callback function itself isn't listed here, but it takes the parsed JSON, walks over the data and stores everything in "_achievements". At that moment, "isReady" returns true, because my function has finished loading data.
Depends on what you want to do with this particular logic. Like why do you want to load map info?