I need help with RPGMaker MZ Load/Continue Game Script

Hey,
I'm working on a game with RPGMaker MZ and have a custom title screen in the form of a converted Map1. There I have 2 choices: New Game and Continue. If i go to new game i get to map2, the first playable map. if i save on map2 and go back to the custom titlescreen, i can click on continue and it works as it should. but if i don't stay on map2 but go to map3 and save there and then go back to my titlescreen to click on continue, i get "TypeError cannot read property 'pages' of undefined". The same happens if I saved in map 1 but then quit the game completely and restart it and then go to continue playing.

To save I use either the normal save function per event of the RMMZ or a save script per event:

$gameSystem.onBeforeSave();
DataManager.saveGame(1);

To load:
if (DataManager.loadGame(1)) {
SoundManager.playLoad();
$gamePlayer.reserveTransfer($gameMap.mapId(), $gamePlayer.x, $gamePlayer.y);
SceneManager.goto(Scene_Map);
$gameSystem.onAfterLoad();
}

strangely, loading with a try catch block around it works better, but still buggy, like the music of the title screen plays instead of the bgm of the map that was loaded.

Basically I want to have only one savegame in my game, which the player always overwrites when saving and can continue in the title screen, unless he wants to start a new game.

I hope someone can help me, thank you very much!
👋
Those are for MV, but MZ is different: it saves/loads asynchronously.

Try these script calls instead:
  • Save:
    JavaScript:
    $gameSystem.setSavefileId(1);
    $gameSystem.onBeforeSave();
    DataManager.saveGame(1);
  • Continue/Load:
    JavaScript:
    DataManager.loadGame(1)
      .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());
Source: MV / MZ Script Call List. Note that there are checkboxes down the left-hand side of the main sheet to indicate which script calls apply to MV, MZ, or both.
hey, i have already seen this post, that's why i registered here. The code unfortunately has the same problem for me. But thanks anyway! In addition, I have the problem that when I test my game and I press F8 or F12 that the game quits. F5 or the others work. But so I also unfortunately can not look into the console. I have the RPGmaker via steam and use a mac with M2 chip, if that is important.
Spoiler: technical explanation for my initial response
if i save on map2 and go back to the custom titlescreen, i can click on continue and it works as it should. but if i don't stay on map2 but go to map3 and save there and then go back to my titlescreen to click on continue, i get "TypeError cannot read property 'pages' of undefined". The same happens if I saved in map 1 but then quit the game completely and restart it and then go to continue playing.
This suggests it's trying to update an event that doesn't exist. This might happen if the new map has fewer events than the current one and you tell it to go to the map scene before the save data has finished loading. This is likely if you used a synchronous if-else ("MV style") load check instead of an asynchronous then-catch promise chain ("MZ style").

loading with a try catch block around it works better, but still buggy, like the music of the title screen plays instead of the bgm of the map that was loaded.
This suggests it has processed onAfterLoad (which replays the map BGM/BGS and restores the saved frame count) before the new map data has loaded. Again, this can happen if using an MV call in MZ, since for MZ DataManager.loadGame immediately returns a Promise, which is always truthy.
I would recommend this approach for each test:
  1. Delete your project's save subfolder (removes old save data),
  2. Launch playtest,
  3. Start a new game,
  4. Do some things! 😂
  5. Make a new save in the correct slot,
  6. Close the game (to ensure the load is "clean" of lingering session data),
  7. Launch playtest again,
  8. Start a new game,
  9. Trigger the "load game" script call.
Loading directly from the map is not a core engine feature, so plugins may not account for it. In that case you may have to manually refresh the data of any applicable plugin(s) in your "load game" script call. That will have to be considered on a per-plugin basis. (I am not offering to do that for you.)

If you are using any plugins: try turning them off, save your project to apply Plugin Manager changes, then test. This may help to identify which of them, if any, are causing problems.

The F8/F12 crash might be a NWJS-on-Mac issue. I don't really know much about Mac, though.

Perhaps someone else will have some advice. Good luck! 👋

Original thread: https://forums.rpgmakerweb.com/threads/i-need-help-with-rpgmaker-mz-load-continue-game-script.159902/