Run common event on game quit.

It's just that... Make it so the game automatically runs a common event even the game is shuts down. Even 1 if that changes anything.
it is possible in 1 way (depends what the event does).
but it shut after it without saving.

if you can save, this isn't recommended as it keeps shut down
when you continue it or alter the load game part too so it can be continued.

the code is simple, but its best to know what the common event does first.
it is possible in 1 way (depends what the event does).
but it shut after it without saving.

if you can save, this isn't recommended as it keeps shut down
when you continue it or alter the load game part too so it can be continued.

the code is simple, but its best to know what the common event does first.
It saves the game.


$gameSystem.onBeforeSave();

if(DataManager.saveGame(1)) {

StorageManager.cleanBackup(1);

}
Are you planning some kind of perma-death game that attempts to forbid any way of exploiting save-stats? Then maybe flip the problem and look for a plugin that save-locks the player.
if it saves the game on quite or the X, loading the game
when using "autosave" (which there are plugins for), this lead into
exit right after loading the game.

if they can save the game, they can simply exit it after or redo the part
upon closing, is there a specific reason for it to save upon exit the game?
Are you planning some kind of perma-death game that attempts to forbid any way of exploiting save-stats? Then maybe flip the problem and look for a plugin that save-locks the player.
That was our first thought, didn't find anything.

if it saves the game on quite or the X, loading the game
when using "autosave" (which there are plugins for), this lead into
exit right after loading the game.

if they can save the game, they can simply exit it after or redo the part
upon closing, is there a specific reason for it to save upon exit the game?
Ya because it's a roguelike.

"The player will find away to do the thing." Doesn't always mean you should provide a means of doing it. Some things are do in particular ways for particular reasons.
Maybe you like my perma-death plugin! ^^

Maybe you like my perma-death plugin! ^^

We're sure you did a fine job on it, but we needs something that saves when the player selects the quit game option...

That and this game has permanent progress. Death doesn't show a game over, it saves and starts a new loop, so the switches and variables can be preserved.
You can try this:
Make a plugin with any name, and insert:

Code:
(function() {

const _SceneManager_terminate = SceneManager.terminate;
SceneManager.terminate = function() {
    
  // your custom code below
  StorageManager.cleanBackup(1);
  
  // your custom code ends here
  _SceneManager_terminate.call(this);
}

})();

I don't know what happens when the player Alt+F4's, though.
You can try this:
Make a plugin with any name, and insert:

Code:
(function() {

const _SceneManager_terminate = SceneManager.terminate;
SceneManager.terminate = function() {
  
  // your custom code below
  StorageManager.cleanBackup(1);
 
  // your custom code ends here
  _SceneManager_terminate.call(this);
}

})();

I don't know what happens when the player Alt+F4's, though.
Doesn't seem to do anything at all, and don't worry too much about the Alt+4, if we can have it working when they open the menu and select quit game that should be fine.

Edit: Oh we think we see what you meant to do now... No, we do not want to delete a save, we want to save to the 1st file.
so in short, you want a common event to load a new game when game over?
if exit, save the game?

if so, you need a loading point defined in variables when saving the game.
otherwise, it cannot load the game correctly if they continue.

TSR_Save can start from a save slot and not go to the save scene again,
so you can use autosave feature to save over it.

if you want to delete the file on game over to start a new game, or keep
progress when dieing, it probably need minor modifications, so it can do
what you need.
so in short, you want a common event to load a new game when game over?
if exit, save the game?

if so, you need a loading point defined in variables when saving the game.
otherwise, it cannot load the game correctly if they continue.

TSR_Save can start from a save slot and not go to the save scene again,
so you can use autosave feature to save over it.

if you want to delete the file on game over to start a new game, or keep
progress when dieing, it probably need minor modifications, so it can do
what you need.
No, what we need is for the game to call a common event if the player opens the game menu and selects the "End game" option then selects "To title".

It's one of those things that shorts out your brain because it's at once so simple and so counter to everything you normally think about. Like a fish asking how to peal a boiled egg.
Change Id in commonEvent, and save it as a plugin (any name) and
see if it works what you want.

after the common event it fades it out and to title scene.
JavaScript:
Scene_GameEnd.prototype.commandToTitle = function() {
    $gameTemp.reserveCommonEvent(id)
    this.fadeOutAll();
    SceneManager.goto(Scene_Title);
};
Change Id in commonEvent, and save it as a plugin (any name) and
see if it works what you want.

after the common event it fades it out and to title scene.
JavaScript:
Scene_GameEnd.prototype.commandToTitle = function() {
    $gameTemp.reserveCommonEvent(id)
    this.fadeOutAll();
    SceneManager.goto(Scene_Title);
};
... Doesn't seem to do anything at all...
we do not want to delete a save, we want to save to the 1st file.
what we need is for the game to call a common event if the player opens the game menu and selects the "End game" option then selects "To title".
The title scene has no interpreter to run events. Setting that up would probably be a lot more work than writing a plugin to save the game when you want. 😅

Here's one you can try~
Spoiler: plugin code
JavaScript:
/*:
 * @target MV
 * @plugindesc Auto-save to slot 1 when quitting to title or closing the game.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/176462/
 * @help May fail to save on quit in games deployed for web.
 *
 * Free to use and/or modify for any project, no credit required.
 */
;void (function() {
"use strict";
  /** Save slot ID. @type {number} */
  const SLOT_ID = 1;

  /** If `true`, show debug messages. @type {boolean} */
  const SHOW_DEBUG = true;

  /** If `true`, save when closing the game. @type {boolean} */
  const SAVE_ON_QUIT = true;

  /** Scenes from which the title should save. @type {Scene_Base[]} */
  const TITLE_SAVE_FROM = Object.freeze([
    Scene_GameEnd,
    Scene_Gameover,
    Scene_Map,
    Scene_Battle
  ]);

  /** @returns {boolean} `true` iff title should save on start. */
  const shouldSaveOnTitle = function() {
    return TITLE_SAVE_FROM.some(function(s) {
      return SceneManager.isPreviousScene(s);
    });
  };

  /** 
   * Performs a save to {@linkcode SLOT_ID}.
   * @returns {boolean} `true` iff save was successful.
   */
  const doSave = function() {
    $gameSystem.onBeforeSave();
    if (DataManager.saveGame(SLOT_ID)) {
      StorageManager.cleanBackup(SLOT_ID);
      return true;
    } else {
      return false;
    }
  };

  /**
   * As {@linkcode} but with debug messages if appropriate.
   * @returns {boolean} `true` iff save was successful.
   */
  const doSaveDebug = SHOW_DEBUG ? function() {
    const r = doSave();
    alert(r ? "Save successful." : "Save failed, oh no!");
    return r;
  } : doSave;
  
  // Patch - save when the title scene starts, if appropriate.
  const alias = Scene_Title.prototype.start;
  Scene_Title.prototype.start = function() {
    if (shouldSaveOnTitle()) {
      doSaveDebug();
    } else if (SHOW_DEBUG)
      alert("Not saving, based on previous scene.");
    alias.apply(this, arguments);
  };

  if (SAVE_ON_QUIT) {

    /** @returns {boolean} `true` iff game app/page should save on close. */
    const shouldSaveOnQuit = function() {
      const c = SceneManager._scene.constructor;
      if (c === Scene_Boot || c === Scene_Title)
        return false;
      if (
        (c === Scene_Options || c === Scene_Load) &&
        SceneManager.isPreviousScene(Scene_Title)
      )
        return false;
      return true;
    };

    /** Handler for game close event. Saves if appropriate. */
    const onClose = function() {
      if (shouldSaveOnQuit())
        doSaveDebug();
      else if (SHOW_DEBUG)
        alert("Not saving, based on current scene.");
      if (Utils.isNwjs())
        nw.App.quit();
    };

    // Patch - hook a save call into a relevant quit event.
    if (Utils.isNwjs())  // local/standalone
      nw.Window.get().on("close", onClose);
    else  // web deployment
      document.addEventListener("beforeunload", onClose);  // may fail?

  }

})();
Currently it is configured to show debug alerts every time a save succeeds or fails. If it looks like it's working for you then you can edit the code to define SHOW_DEBUG = false. 👋

As noted in the plugin's help section, I'm not sure about web support. It looks like the beforeunload event and its brethren all have caveats.
The title scene has no interpreter to run events. Setting that up would probably be a lot more work than writing a plugin to save the game when you want. 😅

Here's one you can try~
Spoiler: plugin code
JavaScript:
/*:
 * @target MV
 * @plugindesc Auto-save to slot 1 when quitting to title or closing the game.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/176462/
 * @help May fail to save on quit in games deployed for web.
 *
 * Free to use and/or modify for any project, no credit required.
 */
;void (function() {
"use strict";
  /** Save slot ID. @type {number} */
  const SLOT_ID = 1;

  /** If `true`, show debug messages. @type {boolean} */
  const SHOW_DEBUG = true;

  /** If `true`, save when closing the game. @type {boolean} */
  const SAVE_ON_QUIT = true;

  /** Scenes from which the title should save. @type {Scene_Base[]} */
  const TITLE_SAVE_FROM = Object.freeze([
    Scene_GameEnd,
    Scene_Gameover,
    Scene_Map,
    Scene_Battle
  ]);

  /** @returns {boolean} `true` iff title should save on start. */
  const shouldSaveOnTitle = function() {
    return TITLE_SAVE_FROM.some(function(s) {
      return SceneManager.isPreviousScene(s);
    });
  };

  /**
   * Performs a save to {@linkcode SLOT_ID}.
   * @returns {boolean} `true` iff save was successful.
   */
  const doSave = function() {
    $gameSystem.onBeforeSave();
    if (DataManager.saveGame(SLOT_ID)) {
      StorageManager.cleanBackup(SLOT_ID);
      return true;
    } else {
      return false;
    }
  };

  /**
   * As {@linkcode} but with debug messages if appropriate.
   * @returns {boolean} `true` iff save was successful.
   */
  const doSaveDebug = SHOW_DEBUG ? function() {
    const r = doSave();
    alert(r ? "Save successful." : "Save failed, oh no!");
    return r;
  } : doSave;
 
  // Patch - save when the title scene starts, if appropriate.
  const alias = Scene_Title.prototype.start;
  Scene_Title.prototype.start = function() {
    if (shouldSaveOnTitle()) {
      doSaveDebug();
    } else if (SHOW_DEBUG)
      alert("Not saving, based on previous scene.");
    alias.apply(this, arguments);
  };

  if (SAVE_ON_QUIT) {

    /** @returns {boolean} `true` iff game app/page should save on close. */
    const shouldSaveOnQuit = function() {
      const c = SceneManager._scene.constructor;
      if (c === Scene_Boot || c === Scene_Title)
        return false;
      if (
        (c === Scene_Options || c === Scene_Load) &&
        SceneManager.isPreviousScene(Scene_Title)
      )
        return false;
      return true;
    };

    /** Handler for game close event. Saves if appropriate. */
    const onClose = function() {
      if (shouldSaveOnQuit())
        doSaveDebug();
      else if (SHOW_DEBUG)
        alert("Not saving, based on current scene.");
      if (Utils.isNwjs())
        nw.App.quit();
    };

    // Patch - hook a save call into a relevant quit event.
    if (Utils.isNwjs())  // local/standalone
      nw.Window.get().on("close", onClose);
    else  // web deployment
      document.addEventListener("beforeunload", onClose);  // may fail?

  }

})();
Currently it is configured to show debug alerts every time a save succeeds or fails. If it looks like it's working for you then you can edit the code to define SHOW_DEBUG = false. 👋

As noted in the plugin's help section, I'm not sure about web support. It looks like the beforeunload event and its brethren all have caveats.
Oh this is perfect! Thank you! Be sure to put it somewhere folks can find it later, it's very helpful for those of us who want to make a proper roguelike.

Original thread: https://forums.rpgmakerweb.com/threads/run-common-event-on-game-quit.176462/