How to load a savefile?

I'm creating my own title screen with the HIME_PreTitleEvents and I want to load a file after a choice. I tried to use this one:
Code:
DataManager.loadGame(1);
$gamePlayer.reserveTransfer($gameMap.mapId(), $gamePlayer.x, $gamePlayer.y);
$gamePlayer.requestMapReload();
SceneManager.goto(Scene_Map);
$gameSystem.onAfterLoad();
To load file 1 but it's crashing. Here is my bug:

Unbenannt.PNG
What happens when you do this in a project without plugins?
While it will be fine doing so in many cases, in general loading and saving the game is not supposed to be done in the middle of the games update cycle (when event commands would be run).

At this point, the events to update for the frame are already decided, and switching to a different savestate will continue updating those events, but many references to other events, switches, the player character etc. will use the freshly loaded data instead, which might trip the game up.
I'd recommend using a plugin for this one to move the execution of your loading script outside the main update routine:
Spoiler
Code:
// --------------------------------------------------------------------------
//  After Map Update Routine
//
// @plugindesc Allows to reserve a script to be run after the map update
//     cycle.
// @help Use
//     $gameTemp.afterUpdate = function() {
//        // ... your script ... //
//     };
//     to move the execution of your script outside the map update cycle.
//     Scene switches caused by events (Battle Processing etc.) and the
//     message box will take precedence over the reserved script.
//
//     It's recommended to put this as close to the bottom of your plugin
//     list as possible.
// --------------------------------------------------------------------------

(function() {
 
   var sceneMapUpdateMain = Scene_Map.prototype.updateMain;
   Scene_Map.prototype.updateMain = function() {
       sceneMapUpdateMain.call(this);
       if ($gameTemp.afterUpdate && this.isAfterUpdateOk()) {
           var callback = $gameTemp.afterUpdate;
           $gameTemp.afterUpdate = null;
           callback();
       }
   };
 
   Scene_Map.prototype.isAfterUpdateOk = function() {
       return this.isSceneChangeOk();
   };

})();
This should allow to alter your script call to
Code:
$gameTemp.afterUpdate = function() {
    DataManager.loadGame(1);
    $gamePlayer.reserveTransfer($gameMap.mapId(), $gamePlayer.x, $gamePlayer.y);
    $gamePlayer.requestMapReload();
    SceneManager.goto(Scene_Map);
    $gameSystem.onAfterLoad();
};
to move the execution of your script call and finish updating events and player first.
(Haven't tested the plugin yet though, but in general this should be a way to go)

As a sidenote, the
Code:
    $gamePlayer.reserveTransfer($gameMap.mapId(), $gamePlayer.x, $gamePlayer.y);
    $gamePlayer.requestMapReload();
part is usually only done to reset the current map if the game has since been updated. It resets all events and parallel processes, which you might not want when loading the game normally. If that is the case you can make this part conditional and only reset the map if you updated your project since (the default loading process):
Code:
if ($gameSystem.versionId() !== $dataSystem.versionId) {
    $gamePlayer.reserveTransfer($gameMap.mapId(), $gamePlayer.x, $gamePlayer.y);
    $gamePlayer.requestMapReload();
}
What happens when you do this in a project without plugins?
Without plugins its working fine. I guess its the way i tried to save the game. I used this
Code:
$gameSystem.onBeforeSave();
DataManager.saveGame(1);
to save the game. and if i use the normal save command from the menu its working with the plugins. i guess i have to put something in the code that saves the plugins?

$gameTemp.afterUpdate = function() { DataManager.loadGame(1); $gamePlayer.reserveTransfer($gameMap.mapId(), $gamePlayer.x, $gamePlayer.y); $gamePlayer.requestMapReload(); SceneManager.goto(Scene_Map); $gameSystem.onAfterLoad(); };
Tried this and still getting the same error
I solved it on my own. This thread can get closed.

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.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-load-a-savefile.114681/