Dynamic Event Creation causing TypeError

Discussion in 'Javascript/Plugin Support' started by p0_boy, Aug 6, 2019.

Thread Status:
Not open for further replies.
  1. p0_boy

    p0_boy anti-kumbaya Veteran

    Messages:
    64
    Likes Received:
    26
    Location:
    quezon city
    First Language:
    English
    Primarily Uses:
    RMMV
    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.

    [​IMG]
    [​IMG]

    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:
    [​IMG]
    [​IMG]
    [​IMG]
    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);
    
    [​IMG]

    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:



    [​IMG]

    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.

    [​IMG]

    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!
     
    Last edited: Aug 6, 2019
    #1
  2. Kes

    Kes Global Moderators Global Mod

    Messages:
    20,555
    Likes Received:
    10,495
    First Language:
    English
    Primarily Uses:
    RMVXA

    This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

     
    #2
Thread Status:
Not open for further replies.

Share This Page