Need Some Help Using Scripts to Save/Load Files

Hey everyone!

While I was working in MV, I had two script snippets I could use to load/save game data via a picture based menu just fine:

Save:
JavaScript:
if(AudioManager._bgmBuffer){
    savbgm=AudioManager._currentBgm.name
}else{
    savbgm=false;
}$gameSystem.onBeforeSave();
if (DataManager.saveGame(6)) {
  StorageManager.cleanBackup(6);
}
Load:
JavaScript:
if (DataManager.loadGame(6)) {
    $gamePlayer.reserveTransfer($gameMap.mapId(), $gamePlayer.x, $gamePlayer.y);
    $gamePlayer.requestMapReload();
    SceneManager.goto(Scene_Map);
    $gameSystem.onAfterLoad();
}

The snippets were mainly taken from the MV/MZ Script Call List here.

Since then however... I'm porting my project to MZ- these scripts still work well, except audio doesn't resume upon loading in.

The audio however does successfully load if I run the load script twice, in separate executions. (It seems like the code can load the audio if the map is already loaded?)

Was there something about loading files that changed between versions that I should fix? I am now running Visustella's Save Core as well, but I don't think that should interfere with this.

Thanks in advance for the help!
So...the weird part to me is that some of it seems to work, based on your description. To me, this:
JavaScript:
if (DataManager.loadGame(6)) {
should be giving you false. loadGame() returns:
Code:
return StorageManager.loadObject(saveName).then(contents => {
        this.createGameObjects();
        this.extractSaveContents(contents);
        this.correctDataErrors();
        return 0;
    });
so I'd be expecting to be receiving the value 0, which will evaluate to false in an if statement.

Going by how MZ's default code is written, I'd suggest you try:
Code:
DataManager.loadGame(6).then(() => {
    $gamePlayer.reserveTransfer($gameMap.mapId(), $gamePlayer.x, $gamePlayer.y);
    $gamePlayer.requestMapReload();
    SceneManager.goto(Scene_Map);
    $gameSystem.onAfterLoad();};
but it would probably help if you posted the entirety of the code you're working with.
So...the weird part to me is that some of it seems to work, based on your description. To me, this:

should be giving you false. loadGame() returns:
Code:
return StorageManager.loadObject(saveName).then(contents => {
        this.createGameObjects();
        this.extractSaveContents(contents);
        this.correctDataErrors();
        return 0;
    });
so I'd be expecting to be receiving the value 0, which will evaluate to false in an if statement.

Going by how MZ's default code is written, I'd suggest you try:
Code:
DataManager.loadGame(6).then(() => {
    $gamePlayer.reserveTransfer($gameMap.mapId(), $gamePlayer.x, $gamePlayer.y);
    $gamePlayer.requestMapReload();
    SceneManager.goto(Scene_Map);
    $gameSystem.onAfterLoad();};
but it would probably help if you posted the entirety of the code you're working with.
Unfortunately the suggested code doesn't seem to load anything at all. I think I noticed an extra "(" after DataManager.loadGame(6).then? I tried it with, and without the "(" and still no difference. Sorry I am a javascript amateur. 😳

I'm just working in the event screen, so really that's all the code that I got.

1685481057614.png

I have the picture menu working, but I haven't inserted this in yet- it will basically just be under some conditional branches, and of course instead of sticking to file 6, I'll replace that with a variable.
Unlike MV, MZ does not wait for the "load file" operation to complete before continuing to the next line of code. The MV script call is not working for you because it's calling the "after load" part without waiting for the file load to complete (or fail). You need a different script call here.

Fortunately the Script Call List documents a separate version for MZ. Notice the two small columns on the left-hand side of the sheet, filled with green checkboxes? Those indicate whether the script call applies to MV, MZ, or both. Here's the MZ Continue/Load script call example from that sheet:

// Continue from save slot 4; play success/failure sounds DataManager.loadGame(4) .then(() => { SoundManager.playLoad(); SceneManager._scene.fadeOutAll(); if ($gameSystem.versionId() !== $dataSystem.versionId) { const p = $gamePlayer; p.reserveTransfer($gameMap.mapId(), p.x, p.y, p.direction(), 0); p.requestMapReload(); } SceneManager.goto(Scene_Map); $gameSystem.onAfterLoad(); }).catch(() => SoundManager.playBuzzer());
(then and catch are part of the Promise API: they're a bit like an asynchronous if...else.)
Unlike MV, MZ does not wait for the "load file" operation to complete before continuing to the next line of code. The MV script call is not working for you because it's calling the "after load" part without waiting for the file load to complete (or fail). You need a different script call here.

Fortunately the Script Call List documents a separate version for MZ. Notice the two small columns on the left-hand side of the sheet, filled with green checkboxes? Those indicate whether the script call applies to MV, MZ, or both. Here's the MZ Continue/Load script call example from that sheet:

// Continue from save slot 4; play success/failure sounds DataManager.loadGame(4) .then(() => { SoundManager.playLoad(); SceneManager._scene.fadeOutAll(); if ($gameSystem.versionId() !== $dataSystem.versionId) { const p = $gamePlayer; p.reserveTransfer($gameMap.mapId(), p.x, p.y, p.direction(), 0); p.requestMapReload(); } SceneManager.goto(Scene_Map); $gameSystem.onAfterLoad(); }).catch(() => SoundManager.playBuzzer());

(then and catch are part of the Promise API: they're a bit like an asynchronous if...else.)
Ah you know what- I was accidentally looking at the old "back up" sheet instead of the current one 😳sorry about that. Thanks for the pointer!! I'll be giving those new calls a try when I get to work on my project again 🙂
Easy mistake to make! (Maybe it'd be worth adding notices to the top of each of the older sheets...)

A similar thing applies for saving, so you should replace that script call too. Example:

// Save to slot 4; on success/failure, play suitable sounds $gameSystem.setSavefileId(4); $gameSystem.onBeforeSave(); DataManager.saveGame(4) .then(() => SoundManager.playSave()) .catch(() => SoundManager.playBuzzer());
(MZ automatically cleans the backup.)
Easy mistake to make! (Maybe it'd be worth adding notices to the top of each of the older sheets...)

A similar thing applies for saving, so you should replace that script call too. Example:

// Save to slot 4; on success/failure, play suitable sounds $gameSystem.setSavefileId(4); $gameSystem.onBeforeSave(); DataManager.saveGame(4) .then(() => SoundManager.playSave()) .catch(() => SoundManager.playBuzzer());

(MZ automatically cleans the backup.)
Yes, just tested both save and load scripts- they work perfectly. Thanks again for the help! 🙂

Original thread: https://forums.rpgmakerweb.com/threads/need-some-help-using-scripts-to-save-load-files.157933/