Need Some Help Using Scripts to Save/Load Files

jjraymonds

Regular
Regular
Joined
Feb 1, 2020
Messages
70
Reaction score
9
First Language
English
Primarily Uses
RMMZ
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!
 

ATT_Turan

Forewarner of the Black Wind
Regular
Joined
Jul 2, 2014
Messages
11,227
Reaction score
9,188
First Language
English
Primarily Uses
RMMV
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.
 

jjraymonds

Regular
Regular
Joined
Feb 1, 2020
Messages
70
Reaction score
9
First Language
English
Primarily Uses
RMMZ
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. o_O

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.
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
5,041
Reaction score
4,393
First Language
EN
Primarily Uses
RMMZ
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.)
 

jjraymonds

Regular
Regular
Joined
Feb 1, 2020
Messages
70
Reaction score
9
First Language
English
Primarily Uses
RMMZ
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 o_Osorry about that. Thanks for the pointer!! I'll be giving those new calls a try when I get to work on my project again :smile:
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
5,041
Reaction score
4,393
First Language
EN
Primarily Uses
RMMZ
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.)
 

jjraymonds

Regular
Regular
Joined
Feb 1, 2020
Messages
70
Reaction score
9
First Language
English
Primarily Uses
RMMZ
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! :smile:
 

Latest Threads

Latest Posts

Latest Profile Posts

I’m so lucky! Simone got referred to a feline cardiologist and had I not called this morning the moment there was a cancellation the next opening would have been SEVEN months from now! The other heart hospital would have been on the other side of Michigan. Who knew that animal specialty appointments were also terrible to get!?
Scalemail project is one step closer to completion. Ordered scales from a local metalworking company, ordered some split rings... now all I need is to wait. :>
And pray that the split rings will be flexible enough to handle that.
A spooky banner and a spooky pfp for a spooky season.
Spooky-Season.png
Broke: Actually making the stuff you need to make before the game can progress.
Woke: Wasting time instead by making a sidequest where you can recruit an imaginary friend to the party.
1696264391516.png
Day 1 I don't know where to start... I enjoy designing icons and brainstorming the abilities my video game will have

Forum statistics

Threads
134,993
Messages
1,252,690
Members
177,900
Latest member
LinkIncGames
Top