Status
Not open for further replies.

p0_boy

anti-kumbaya
Veteran
Joined
Mar 26, 2019
Messages
64
Reaction score
29
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:
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!
 
Last edited:

gstv87

Veteran
Veteran
Joined
Oct 20, 2015
Messages
2,463
Reaction score
1,472
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.
 

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,147
Reaction score
10,708
First Language
Czech
Primarily Uses
RMMV
Files need time before they load. The reason is, they are loaded asynchronously. So your assumption was correct.
 

p0_boy

anti-kumbaya
Veteran
Joined
Mar 26, 2019
Messages
64
Reaction score
29
First Language
English
Primarily Uses
RMMV
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.
 
Last edited:

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,147
Reaction score
10,708
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.
 

p0_boy

anti-kumbaya
Veteran
Joined
Mar 26, 2019
Messages
64
Reaction score
29
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;

};
 

slimmmeiske2

Little Red Riding Hood
Global Mod
Joined
Sep 6, 2012
Messages
8,920
Reaction score
5,847
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.

 
Status
Not open for further replies.

Latest Threads

Latest Posts

Latest Profile Posts

finished the second chapter of deltarune today, I gotta say that the 3 years wait was worth it lol
Happy Friday, y'all! I just got home from work and am sitting down with a nice cider, and I'm ready to chill and enjoy myself. Not sure if I'm gonna dive into any of my game-related projects or not tonight.... we'll see how I feel after unwinding for a bit I guess!
Just found out that mint tea made from chocolate mint tastes a bit like hot cocoa with candy cane stirred in. As I'm allergic to chocolate this makes me very happy.
It's special day for me tomorrow so time to vanish for 24 hours lol.

Forum statistics

Threads
115,145
Messages
1,087,611
Members
149,668
Latest member
cp868323
Top