MV Can I save data in a file?

Instead, I might generate all actions before the first report is shown, save the outcome of the match, then show the reports one by one.
Good idea! That sounds like a much neater solution~ 😮‍💨

But I fail to understand where I can decide what to store there and how to read it out. 😛
That's my fault: there are various problems with what I posted. 👋
Spoiler: more info
For that approach, you would need something like @TG22 suggested. However, there are a couple of differences:
  1. MV's StorageManager.save and load methods (different names than in MZ) expect a save ID rather than a file name, so you'd still need the conversion stuff I suggested.

  2. As noted, MV's load operations block execution until complete, so the then clause (for an async "Promise") should simply be removed.
So maybe something like this (untested again, fingers crossed):
Spoiler: plugin code
JavaScript:
;void (function() {
'use strict';

  /** Matched against custom per-slot save ID, see {@linkcode extractId}. */
  const RX = /JDN_(\d+)/;

  /** New local save file name. `%1` = slot ID. */
  const FILENAME = "jdn_data%1.rpgsave";

  /** New web storage data key. `%1` = slot ID. */
  const WEB_KEY = "RPG JDN%1";

  /**
   * @param {*} value input internal file ID.
   * @returns {?number} corresponding slot ID, or `null` if not applicable.
   */
  const extractId = function(value) {
    if (typeof value === "string") {
      const match = RX.exec(value);
      if (match)
        return parseInt(match[1], 10);
    }
    return null;
  };

  // Patch - map custom ID (`"JDN_%1"`) to custom file name.
  void (function() {
    const alias = StorageManager.localFilePath;
    StorageManager.localFilePath = function(savefileId) {
      const id = extractId(savefileId);
      if (id)
        return SAVE_NAME.format(id);
      return alias.apply(this, arguments);
    };
  })();

  // Patch - map custom ID (`"JDN_%1"`) to custom web storage key.
  void (function() {
    const alias = StorageManager.webStorageKey;
    StorageManager.webStorageKey = function(savefileId) {
      const id = extractId(savefileId);
      if (id)
        return SAVE_NAME.format(id);
      return alias.apply(this, arguments);
    };
  })();

})();
Then:
  • Save (modelled on DataManager.saveGame, rpg_managers.js):
    JavaScript:
    const data = $gameVariables.value(1), id = "JDN_1";
    StorageManager.backup(id);
    try {
      StorageManager.save(id, data)
      StorageManager.cleanBackup(id);
    } catch (ex) {
      console.error(ex);
      StorageManager.remove(id);
      StorageManager.restoreBackup(id);
    }
  • Load (modelled on DataManager.loadGame, rpg_managers.js):
    JavaScript:
    const id = "JDN_1";
    try {
      $gameVariables.setValue(1, StorageManager.load(id));
    } catch (ex) {
      console.error(ex);
    }

Uhm... Did you just say save files in MZ auto zipped before written? That's annoying. 😛
It reduces the file size, and gets unzipped automatically on load. This page claims the limit on local storage is currently 5 MiB per origin:
So file compression may be particularly valuable for web games.
Good idea! That sounds like a much neater solution~ 😮‍💨


That's my fault: there are various problems with what I posted. 👋
Spoiler: more info
For that approach, you would need something like @TG22 suggested. However, there are a couple of differences:
  1. MV's StorageManager.save and load methods (different names than in MZ) expect a save ID rather than a file name, so you'd still need the conversion stuff I suggested.

  2. As noted, MV's load operations block execution until complete, so the then clause (for an async "Promise") should simply be removed.
So maybe something like this (untested again, fingers crossed):
Spoiler: plugin code
JavaScript:
;void (function() {
'use strict';

  /** Matched against custom per-slot save ID, see {@linkcode extractId}. */
  const RX = /JDN_(\d+)/;

  /** New local save file name. `%1` = slot ID. */
  const FILENAME = "jdn_data%1.rpgsave";

  /** New web storage data key. `%1` = slot ID. */
  const WEB_KEY = "RPG JDN%1";

  /**
   * @param {*} value input internal file ID.
   * @returns {?number} corresponding slot ID, or `null` if not applicable.
   */
  const extractId = function(value) {
    if (typeof value === "string") {
      const match = RX.exec(value);
      if (match)
        return parseInt(match[1], 10);
    }
    return null;
  };

  // Patch - map custom ID (`"JDN_%1"`) to custom file name.
  void (function() {
    const alias = StorageManager.localFilePath;
    StorageManager.localFilePath = function(savefileId) {
      const id = extractId(savefileId);
      if (id)
        return SAVE_NAME.format(id);
      return alias.apply(this, arguments);
    };
  })();

  // Patch - map custom ID (`"JDN_%1"`) to custom web storage key.
  void (function() {
    const alias = StorageManager.webStorageKey;
    StorageManager.webStorageKey = function(savefileId) {
      const id = extractId(savefileId);
      if (id)
        return SAVE_NAME.format(id);
      return alias.apply(this, arguments);
    };
  })();

})();
Then:
  • Save (modelled on DataManager.saveGame, rpg_managers.js):
    JavaScript:
    const data = $gameVariables.value(1), id = "JDN_1";
    StorageManager.backup(id);
    try {
      StorageManager.save(id, data)
      StorageManager.cleanBackup(id);
    } catch (ex) {
      console.error(ex);
      StorageManager.remove(id);
      StorageManager.restoreBackup(id);
    }
  • Load (modelled on DataManager.loadGame, rpg_managers.js):
    JavaScript:
    const id = "JDN_1";
    try {
      $gameVariables.setValue(1, StorageManager.load(id));
    } catch (ex) {
      console.error(ex);
    }
Awesome.
I think I understand how to use this. I can use those save and load calls in the plugin as wel, right? But then it would be
var playerArray = StorageManager.load(id)

If I use an external save file, I don't need tu use a variable (or else it still takes up all that space)

Thank, I'll play around with this in the weekend. 🙂

It reduces the file size, and gets unzipped automatically on load. This page claims the limit on local storage is currently 5 MiB per origin:
So file compression may be particularly valuable for web games.

Okay, but wouldn't that slow down the loading and saving process even more? Specially with web games?
I can use those save and load calls in the plugin as wel, right?
Assuming I've got them correct this time, yep! 😅

Okay, but wouldn't that slow down the loading and saving process even more? Specially with web games?
I haven't done any related testing or investigation, but I don't imagine it would be noticeable. Also, save/load in MZ is asynchronous, so it shouldn't even cause a frame of wait time in-game (unless you deliberately choose to make it wait). If anything, I'd expect save operations to be slightly slower for local/standalone deployments than for web, due to security checks.

The library MZ uses is pako, which claims to be very fast. Zipping a data chunk (e.g. save data) is a CPU-bound operation, so if it zips as a single chunk then I imagine the compression stage would be faster than writing to disk, even on an SSD.
Assuming I've got them correct this time, yep! 😅
I'll let you know if it works, also if it doesn't. 😛 but I trust you skills.

I haven't done any related testing or investigation, but I don't imagine it would be noticeable. Also, save/load in MZ is asynchronous, so it shouldn't even cause a frame of wait time in-game (unless you deliberately choose to make it wait). If anything, I'd expect save operations to be slightly slower for local/standalone deployments than for web, due to security checks.

The library MZ uses is pako, which claims to be very fast. Zipping a data chunk (e.g. save data) is a CPU-bound operation, so if it zips as a single chunk then I imagine the compression stage would be faster than writing to disk, even on an SSD.
Oh wow, that surprises me. I always thought working in a webbrowser was giving me more limits, but maybe that was just a placebo effect: it might just have felt like it, because I expected it to be true.

Original thread: https://forums.rpgmakerweb.com/threads/can-i-save-data-in-a-file.171276/