[MZ] Forcing save before battle processing

Alright, so I am using the Visustella plugin suite, and a recent update just rendered another plugin I really liked (Dragon Cave's battle retry plugin) rather useless (retrying a battle now pops up a message saying that VS Core does not allow you to switch in the middle of combat)... no biggie. I realized that I could do something to circumvent that by having every manual combat event processing have the following script call before the combat processing command:

JavaScript:
$gameSystem.onBeforeSave();
if(DataManager.saveGame(99)) {
   StorageManager.cleanBackup(1);
}

(I chose slot 99 so the player wouldn't see the savefile, as it is past the scope of how many save slots I'm allowing them to have.)
Then all I need is to the enable the 'can lose?' option, and upon losing giving the player the option to retry the combat, load a game or exit, and upon retrying simply execute

Code:
$gameSystem.onAfterLoad();
DataManager.loadGame(99);
StorageManager.cleanBackup(99);
$gamePlayer.reserveTransfer($gameMap.mapId(), $gamePlayer.x, $gamePlayer.y);
$gamePlayer.requestMapReload();


Right now I've got them running as common events to save me some time, but I wanted to know if there was any way to modify one of the core files in RMZ in order to force the system to save to that file right before battle processing? I realized that my workaround works for any combat that the player may be able to initiate on the map... but it doesn't really work for map random encounters.

Any help would be greatly appreciated!
Could you do that via a Turn 0, Span Battle troop event? VisuStella's Battle Core has a Base Troops feature that could help with that~

Otherwise you could try importing this as a plugin:
Spoiler: plugin code (untested)
JavaScript:
/*:
 * @target MZ
 */
(alias => {
  Scene_Battle.prototype.create = function() {
    $gameSystem.onBeforeSave();
    DataManager.saveGame(99);
    alias.apply(this, arguments);
  };
})(Scene_Battle.prototype.create);

Also, note that by default RMMZ uses a different save system from RMMV, so the script calls for save/load are different, namely they're asynchronous and backups are handled automatically. Here's some MZ examples adapted from the RPG Maker MV / MZ Script Call List:
Spoiler: example MZ save/load script calls
JavaScript:
// Silently save to slot 99
$gameSystem.onBeforeSave();
DataManager.saveGame(99);
JavaScript:
// Continue from save slot 99
DataManager.loadGame(99)
  .then(() => {
    $gameSystem.onAfterLoad();
    SceneManager._scene.fadeOutAll();
    if ($gameSystem.versionId() !== $dataSystem.versionId) {
      const p = $gamePlayer;
      p.reserveTransfer($gameMap.mapId(), p.x, p.y);
      p.requestMapReload();
    }
    SceneManager.goto(Scene_Map);
  });
(Plugins could change any aspect of what I just mentioned.)
Could you do that via a Turn 0, Span Battle troop event? VisuStella's Battle Core has a Base Troops feature that could help with that~

Otherwise you could try importing this as a plugin:
Spoiler: plugin code (untested)
JavaScript:
/*:
 * @target MZ
 */
(alias => {
  Scene_Battle.prototype.create = function() {
    $gameSystem.onBeforeSave();
    DataManager.saveGame(99);
    alias.apply(this, arguments);
  };
})(Scene_Battle.prototype.create);

Also, note that by default RMMZ uses a different save system from RMMV, so the script calls for save/load are different, namely they're asynchronous and backups are handled automatically. Here's some MZ examples adapted from the RPG Maker MV / MZ Script Call List:
Spoiler: example MZ save/load script calls
JavaScript:
// Silently save to slot 99
$gameSystem.onBeforeSave();
DataManager.saveGame(99);
JavaScript:
// Continue from save slot 99
DataManager.loadGame(99)
  .then(() => {
    $gameSystem.onAfterLoad();
    SceneManager._scene.fadeOutAll();
    if ($gameSystem.versionId() !== $dataSystem.versionId) {
      const p = $gamePlayer;
      p.reserveTransfer($gameMap.mapId(), p.x, p.y);
      p.requestMapReload();
    }
    SceneManager.goto(Scene_Map);
  });
(Plugins could change any aspect of what I just mentioned.)
Oh, that worked beautifully,

caethyril. Thank you!​

Original thread: https://forums.rpgmakerweb.com/threads/mz-forcing-save-before-battle-processing.141727/