DataManager.loadDataFile

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:
uncaught-typeerror.png


object-assign.png


There seems to be no problem though if, after the error, I type $dataTemporary into the console window:
datatemporary.png

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!
*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.
Files need time before they load. The reason is, they are loaded asynchronously. So your assumption was correct.
Files need time before they load. The reason is, they are loaded asynchronously. So your assumption was correct.

@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.
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.
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;

};

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.

Original thread: https://forums.rpgmakerweb.com/threads/datamanager-loaddatafile.111731/