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,419
Reaction score
1,434
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,136
Reaction score
10,674
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,136
Reaction score
10,674
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,725
Reaction score
5,760
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

For anyone interested: there's another huge humble bundle with JRPG assets + a game jam starting next month - it's for a good cause, so check it out <3
Turning a specific vibe into reality is incredibly difficult. For instance, this song is one of the ones I listen to when working on my post apocalyptic project, and getting it's "feeling" into the game has been a real challenge.

I don't just wanna regurgitate the numbers I see, I wanna actually understand the calculations I'm making so I'm probably gonna make a post later with some calculations and what I THINK they mean to get some advice and corrections.

Edit: All (Most) of these will be tested in game prior. I just wanna make sure I understand what's going on under the hood if you will.
my girlfriend calls all my little chibi art cute lol... funny considering that wasn't the original art style of the game.
100 problems in RMMZ 100 more problems take one down pass it around 101 Problems in RMMZ

Forum statistics

Threads
113,778
Messages
1,077,362
Members
147,808
Latest member
traviscot
Top