DataManager.loadDataFile

Discussion in 'Javascript/Plugin Support' started by p0_boy, Aug 2, 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
    Howdy, folks-

    I have the following code:

    Code:
    let event_to_clone = (eventId_code) => {
    
        let result = {};
    
        switch (eventId_code === parseInt(eventId_code, 10)) {
            case false:
                let temporary = eventId_code.toString().split(`.`);
                let mapId = temporary[0];
                let eventId = Number(temporary[1]);
    
                let filename = `Map` + (mapId.length === 1 ? `00` : (mapId.length === 2 ? `0` : ``)) + mapId + `.json`;
    
                DataManager.loadDataFile(`$dataTemporary`, filename);
    
                result = Object.assign({}, $dataTemporary.events[eventId]);
                break;
            case true:
                result = Object.assign({}, $dataMap.events[eventId_code]);
                break;
        }
    
        return result;
    
    };
    
    However, whenever I execute it, the following error occurs:
    [​IMG]

    [​IMG]

    There seems to be no problem though if, after the error, I type $dataTemporary into the console window:
    [​IMG]
    I am deducing that:
    Code:
    result = Object.assign({}, $dataTemporary.events[eventId]);
    
    is executing before DataManager.loadDataFile is able to load said file into $dataTemporary?

    What am I doing wrong?

    As always, thank you in advance for any pointers / insights that you might be able to provide!
     
    Last edited: Aug 2, 2019
    #1
  2. gstv87

    gstv87 Veteran Veteran

    Messages:
    1,770
    Likes Received:
    801
    First Language:
    Spanish
    Primarily Uses:
    RMVXA
    *loaded* games won't always have the same data structure if they were saved by a program that didn't create that data structure before saving the file.

    you'll probably have to scrap that file and save a new one.
     
    #2
  3. Poryg

    Poryg Dark Lord of the Castle of Javascreeps Veteran

    Messages:
    3,931
    Likes Received:
    9,935
    Location:
    Czech Republic
    First Language:
    Czech
    Primarily Uses:
    RMMV
    Files need time before they load. The reason is, they are loaded asynchronously. So your assumption was correct.
     
    #3
    p0_boy likes this.
  4. p0_boy

    p0_boy anti-kumbaya Veteran

    Messages:
    64
    Likes Received:
    26
    Location:
    quezon city
    First Language:
    English
    Primarily Uses:
    RMMV
    @Poryg thanks for confirming my suspicion. Would you happen to have any suggestions on how to do this with DataManager?

    According to the RPGMakerMV Library, .loadDataFile doesn't return anything, so I don't think I can use .then or await.

    Edit:
    I was trying to avoid writing more functions by using the default library, but apparently using DataManager to load map data is bad practice because the $dataTemporary variable I am creating is global.

    Better practice is laid out in this older forum thread.
     
    Last edited: Aug 2, 2019
    #4
  5. Poryg

    Poryg Dark Lord of the Castle of Javascreeps Veteran

    Messages:
    3,931
    Likes Received:
    9,935
    Location:
    Czech Republic
    First Language:
    Czech
    Primarily Uses:
    RMMV
    Actually, await or then wouldn't work anyway, since the main thing happens asynchronously (during xhr.onload) and you cannot use async/await in combo with async calls.
    The only way to do it is to do the remaining stuff in next frame.
     
    #5
  6. p0_boy

    p0_boy anti-kumbaya Veteran

    Messages:
    64
    Likes Received:
    26
    Location:
    quezon city
    First Language:
    English
    Primarily Uses:
    RMMV
    This works:

    Code:
    function load_text_file (text_fn) {
     
        let return_value = ``;
    
        let rawFILE = new XMLHttpRequest();
        rawFILE.open(`GET`, text_fn, false);
        rawFILE.onreadystatechange = function () {
    
            if (rawFILE.status === 200 || rawFILE.status == 0) {
    
                let allText = rawFILE.responseText;
                return_value = allText;
    
            };
    
        };
    
        rawFILE.send(null);
    
        return return_value;
    
    };
    
    
    let event_to_clone = (eventId_code) => {
    
        let result = {};
    
        switch (eventId_code === parseInt(eventId_code, 10)) {
            case false:
                // Parse eventId_code.
                let temporary = eventId_code.toString().split(`.`);
                let mapId = temporary[0];
                let eventId = Number(temporary[1]);
    
                // Format map filename.
                let folder = document.URL.substring(0,(document.URL.length - (document.URL.substring(document.URL.lastIndexOf(`/`) + 1)).length)) + `/data/`;
                let filename = `Map` + (mapId.length === 1 ? `00` : (mapId.length === 2 ? `0` : ``)) + mapId + `.json`;
    
                // Load map into temporary object.
                raw_json = load_text_file(folder + filename);
                map_object = JSON.parse(raw_json);
    
                // Return requested event from that object.
                result = Object.assign({}, map_object.events[eventId]);
                break;
            case true:
                // Return requested event from current map.
                result = Object.assign({}, $dataMap.events[eventId_code]);
                break;
        }
    
        return result;
    
    };
    
     
    #6
  7. slimmmeiske2

    slimmmeiske2 Little Red Riding Hood Moderator

    Messages:
    5,556
    Likes Received:
    4,122
    Location:
    Belgium
    First Language:
    Dutch
    Primarily Uses:
    RMXP

    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.

     
    #7
Thread Status:
Not open for further replies.

Share This Page