MZ Odd Crash in Ritter Ultimate Event Spawner

Hi folks,

I've reached out to the developer of a plugin I'm using for support, but haven't received a response in a few days, so I figured I'd make a post here to see if this is maybe something I could fix myself (and hopefully learn a bit more about what is happening).

I'm using the plugin "Ritter Ultimate Event Spawner", which is more or less working completely fine in my project. The only issue I have is that the plugin will crash the moment I launch the game from the editor, but it will only do this once. After I close and re-launch the game, it works, and will continue to work until I stop launching the game for a while (change windows, do something else, or work in the editor for a bit). After waiting, launching the game will cause the crash again, but only the first time.

This is the error report:
Crash.png


The plugin is a paid one, so I'll just post the one function that is causing the crash at line 1160:
Spoiler: Map Pre-Loader Function
JavaScript:
/**
 * --------------------------------------------------------------------------------
 *   Spawn Map Data Preloading
 * --------------------------------------------------------------------------------
 */
   
DataManager.preloadMaps = function() {
    if (!Ritter.Params.preloadMaps) return;
    var mapIds = Ritter.Params.preloadMaps;
    for (var i = 0; i < Ritter.Params.preloadMaps.length; i++) {
        DataManager.loadSpawnMap(mapIds[i]);
    }
};
/**
 * Loads Map Data for Spawner.
 * Can also spawn events on map load.
 * This allows for you to spawn an event from any map
 * without preloading.
 * Just enter mapId and eventId into spawnEvent to spawn
 * any event from any map without preloading.
 *
 * @param {number} mapId - The Map Id # or "Map Name".
 * @param {number} eventId - The Event Id # or "Event Name".
 * @param {number} x - The X Location to Spawn an Event.
 * @param {number} y - The Y Location to Spawn an Event.
 * @param {boolean} save - Spawn as Saved Event?
 */
DataManager.loadSpawnMap = function(mapId, eventId, x, y, save) {
    var map = new XMLHttpRequest();
    var url = 'data/Map%1.json'.format(mapId.padZero(3));
    map.open("GET", url);
    map.overrideMimeType("application/json");
    map.onload = function() {
        Ritter._SpawnMaps.push(mapId);
        const mapData = JSON.parse(map.responseText);
        $MapData[mapId].events = mapData.events;
        $MapData[mapId].events.forEach(event => {
            if (event !== null) DataManager.extractMetadata(event);
        })
        if (Imported.Ritter_BoundarySystem) Ritter.Boundary.extractBoundaryData(mapData);
        if (typeof eventId === "string") eventId = Ritter._getEventIdFromString(mapId, eventId);
        if (eventId > 0) $gameMap.spawnEvent(mapId, eventId, x, y, save);
    };
    map.onerror = function() { throw new Error('Failed to load: ' + url); };
    map.send();
};

The specific line at 1160 in the crash report is:
JavaScript:
$MapData[mapId].events = mapData.events;

While I can simply not set any spawn map in the parameters to pre-load, this causes other issues with event spawning, so having a pre-loaded spawn map is a necessity. But, having any set in the parameters causes the crash (whether it is one map ID, multiple, and regardless of how many events are on the spawn map).

A link to the plugin can be found here, if anyone wants to look at it (or already has it): https://notritter.itch.io/ritter-ultimate-event-spawner-rpg-maker-mz

Some things I have already tried:
  • Disabling all other plugins
  • Changing the location of my project files from my NAS to local storage (incase it was a loading speed/performance issue)
  • Making a different spawn map and trying it instead
  • Replacing all of my core files
So far, the only thing to have an impact is removing all other plugins and resetting plugins.js, but this just makes the crash less common/reliable, rather than completely fixing it.

Any advice on how I might fix this crash is much appreciated!
The error is very likely a timing problem.

correctly read the error message means that either $MapData[mapId] or mapData ist undefined, which means it does not exist. And since it cannot set the property it's likely the first ($MapData) that does not exist.

Or in other words, the plugin tries to run before the game has initialised its map data.
And that likely happens because of a delay, because running the project from inside the editor activates several bughunting and playtesting functions that are skipped when using the external application.

However solving that problem is not really easy - basically someone who knows the code needs to add in a load check if the data loading is complete or wait if not. And I can't help you with that, it goes too deep into the details.
The error is very likely a timing problem.

correctly read the error message means that either $MapData[mapId] or mapData ist undefined, which means it does not exist. And since it cannot set the property it's likely the first ($MapData) that does not exist.

Or in other words, the plugin tries to run before the game has initialised its map data.
And that likely happens because of a delay, because running the project from inside the editor activates several bughunting and playtesting functions that are skipped when using the external application.

However solving that problem is not really easy - basically someone who knows the code needs to add in a load check if the data loading is complete or wait if not. And I can't help you with that, it goes too deep into the details.
Thanks for the insight!

I had figured it might be a timing thing (hence taking files off my NAS to see if it was maybe a file loading issue), and it sort of explains why adding more plugins seems to cause it to happen more often.

I hadn't tested exporting the game and playing it, though. It never occurred to me that it could be extra processes in the playtest mode. I'll do some more testing on an exported game and see if it continues to happen there. If it is just an editor issue, that would be kind of annoying for playtesting, but not as big of a deal as if it were just a regular crash at least.
The error is very likely a timing problem.

correctly read the error message means that either $MapData[mapId] or mapData ist undefined, which means it does not exist. And since it cannot set the property it's likely the first ($MapData) that does not exist.

Or in other words, the plugin tries to run before the game has initialised its map data.
And that likely happens because of a delay, because running the project from inside the editor activates several bughunting and playtesting functions that are skipped when using the external application.

However solving that problem is not really easy - basically someone who knows the code needs to add in a load check if the data loading is complete or wait if not. And I can't help you with that, it goes too deep into the details.
This was spot on. I was loading map data asynchronously without waiting for it to load fully before trying to access it, which in most cases it would load fast enough but there was room for failure, a simple callback to wait for the data to load fully fixed the issue in our early testing. Thanks for pointing me in the right direction!

Original thread: https://forums.rpgmakerweb.com/threads/odd-crash-in-ritter-ultimate-event-spawner.180829/