Dynamic Event Creation causing TypeError

Aloha, folks-

I have a plug-in with the following function (MapCrowd()):

Code:
// Reads current map's [Note] field and assigns events to specified regions.
    function MapCrowd() {

        // Check that <clone> tag is present in [Note] field.
        if ($dataMap.meta.clone != undefined) {

            let clone_event = 0;

            // Separates multiple RegionIds/Events and places them in an array.
            let crowd = ($dataMap.meta.clone.replace(/\s/g, ``)).split(`;`);

            // Perform placement of events on regions.
            for (let clones = 0; clones < crowd.length; clones++) {
                let temporary = crowd[clones].split(`|`);

                let regionId = Number(temporary[0]);
            
                // Checks if event source is internal or external source (CloneBank.json)
                if (isNaN(temporary[1]) === true) {
                    if (temporary[1].charAt(0) === `$`) {
                        let clone_bank = RootFolder() + `/data/` + `CloneBank.json`;

                        if (FileExists(clone_bank) === true) {
                            let raw_json = load_text_file(clone_bank);
                            let clone_objects = JSON.parse(raw_json);

                            clone_event = Object.assign({}, clone_objects.events[temporary[1].substr(1)]);

                        }
                    }
                } else {

                    clone_event = Number(temporary[1]);

                }

                // Place events on specified regions.
                if (clone_event != 0) $.PlaceEvent(clone_event, regionId);

            }

        }

    };

As the comments in the code remark, it basically reads the <clone> tag from the [Notes] field of maps, and places events on specified regions.

map-note.png

region-map.png


After placing MapCrowd() in aliases of Game_Map.prototype.setup, Game_Map.prototype.setupEvents, Scene_Map.prototype.initialize, and getting errors (to do with trying to read meta of null, I assume because $dataMap had not yet been loaded), I finally got it working by placing it in
Scene_Map.prototype.onMapLoaded:
Code:
    // Executes upon map load.
    var alias_Scene_Map_onMapLoaded = Scene_Map.prototype.onMapLoaded;
    Scene_Map.prototype.onMapLoaded = function() {

        // Established map load routines.
        alias_Scene_Map_onMapLoaded.call(this);
    
        // Plug-in addendum.
        MapCrowd();

    }
This was only partially successful though (please excuse the rudimentary graphics):



Everything loads okay, however a TypeError occurs after I exit the game menu:
typeerror.png

error.png

error2.png

I am hoping that somebody can point me to a better way that I can go about running MapCrowd() upon loading of a map?

Thank you in advance!


EDIT (08/06/2019 7:26 AM [GMT +8]):

The problem doesn't have to do with Scene_Map.prototype.onMapLoaded as I originally assumed, it seems to be a problem with dynamically creating events.

As a comparison, I removed the plug-in that I wrote and tried to use another, similar plug-in that I found (while scouring Google for a solution)- Alphaxaon's EventWrapper.js.

I typed the following commands in the console (as per the plug-in's instructions):

Code:
var event = new MapEvent();

event.copyActionsFromEventOnMap(1,1);

event.spawn(2,4);

eventwrapper.png


And it seemed to be all gravy, until I tried entering and exiting the game menu again and encountered the same "Cannot read property 'pages' of null" TypeError:



scenemanager-error.png


Does anybody know what I am doing wrong?

I have disabled all other plug-ins except for Community_Basic, so I know there aren't any external conflicts.

plugin-window.png


Any help would be much appreciated. Thanks!


EDIT 2 (08/06/2019 12:15 [GMT +8])

Since nobody seems to have a clue about this, I just re-wrote my plug-in to use Galv's Event Spawner as the method for creating the events.

It's kind of a pain because the Galv's plug-in requires the use of a "Spawn Map" to draw events from, but at least it isn't giving me the TypeError any more.

Below is the less exciting, MapCrowd() revision.

Code:
    function MapCrowd() {

        if ($dataMap.meta.clone != undefined) {
          
            let crowd = ($dataMap.meta.clone.replace(/\s/g, ``)).split(`;`);

            for (let clones = 0; clones < crowd.length; clones++) {
                let temporary = crowd[clones].split(`|`);

                let regionId = Number(temporary[0]);

                let clone_event = Number(temporary[1]);

                PlaceEvent(clone_event, regionId);

            }

        }

    };

PlaceEvent() calls Galv.SPAWN.event to create the events.

Subsequently I also had to rewrite a few other things to get everything working again- Galv's plug-in uses a custom global $dataSpawnMap, which I needed to reference (instead of $dataMap) to read the [Note] field of the newly spawned events.

That's all folks!
Thread closed IgnoreMe

Original thread: https://forums.rpgmakerweb.com/threads/dynamic-event-creation-causing-typeerror.111834/