(Solved) [RMMV] Saving and loading gradually take longer

Edit / TL;DR: Solved! The problem was MrTS_MonsterBook registering unnecessary information. @Mac15001900 provided a fix and I suggested how to deal with existing saves, Mac15001900 optimized my approach.



Hello, everyone!
Thanks in advance for any help provided.

I have had this project for some years now. During playtime on Steam, though, I've noticed that as I progress in the game, it takes longer to save and load my files, and sometimes even load a new scene. This usually starts getting noticeable midgame+, after the game has been saved over a hundred times, lots of switches and variables have been registered, and playtime is over 20 hours. During the late-game phase, the save/load time increases, and there have been times when it took over 30 seconds to load or save the game, which can be annoying for players. I've tested that on multiple computers and a friend has reported the same issue, so it probably affects even more players.

I had larger save files in the past, but the one I'm currently using to test the issue has over 40 hours of gameplay. I've saved the game 243x and the file is a little smaller than 7MB.

Other than that, the gameplay looks fluid and players haven't reported lag or other delays.

I use YEP_SaveCore 1.06 and YEP_Autosave. I've just tried deactivating them, but it still takes the same amount of time to load and save. I'd hoped OrangeAntiLag could solve the issue, but it didn't. Other threads on the same topic haven't helped me either.

I know this issue might only affect a minority of players who save hundreds of times and progress a lot into the game, but since I'm not the only one affected anymore, I'd like to find a fix for it. Is there a way to reduce save/load time or avoid its increase throughout gameplay?

Extra information:
- audio and image files have been compressed
- most unnecessary files have been removed
- offering 20 or 200 maximum save files, it's the same
- the game has 3300 switches and 1500 variables
- this problem also happens when playtesting, so it's probably not Steam's fault
- the game is available on Windows only

I can provide any other information and Steam keys if necessary.

Here is a list of the plugins I use in the game:

plugins-aurora.png
Just to hazard a guess, it probably is due to the amount of switches and variables you have. That's a pretty large amount. I do wonder how you've managed to get such a large amount. I've got 300 switches and 140 variables for my pretty large open world game, 10% of what you do. It might just be something you need to accept at this point if it is impossible to optimize that more (which I can imagine would be quite a pain indeed).

You could try finding out a way to flip off switches that the player doesn't need to access anymore. If there are a bunch of switches all the way back from the beginning of the game that never get called again, maybe just flip them back off when they become irrelevant and see if that helps. You might even be able to test that by going into the debug menu and flipping off a lot of switches then saving and seeing how much it helps. I honestly don't know how much that factors in, or if a switch has been turned on it always sort of lingers in the save file even if it has been turned off again, but hey it's worth a shot.


The problem could be worse. You could have the issue that Skyrim did on PS3 where the longer you played the worse the frame rates would get. If the FPS is still stable I wouldn't worry about it too much and instead perhaps find a way to pull up a loading/saving icon to let the player know that their game hasn't crashed.

I do hope that I'm wrong though and there is some way to get this loading faster with a simple fix.
7MB per save file is definitely not due to any amount of variable or switches.
My guess is probably structured data, such as a large amount of unique equipments that the player can collect or upgrade, that is accumulated over time.

Is the save file size also increasing with time?
it probably is due to the amount of switches and variables you have.
Those barely affect anything here. As a quick test, I've made 10,000 variables and assigned a random value between 1 and 1000 to each - the save file is still only 39KB (and that's together with everything else my game saves), nowhere near 7MB.

I'm assuming this is due to some plugin constantly adding more and more stuff to save data when it definitely shouldn't. Though since it takes hours of playtime to replicate and the cause is unknown, I also assume that you can't just try turning off different plugins and see what happens.

Can you share one these bloated save files? It might be possible to identify the problem from looking at one.
@ZoeZero
This has nothing to do with your problem directly, but it should be mentioned for the many possible issues it might cause:
The order of your yanfly plugins is way off.
Yanfly's plugins are known to cause random strange effects or errors if they are not in the exact order they are in the main list of yanfly's website, top to bottom.
you can place other plugins between them, but the yanfly plugins themselve should be in that order.

And there is even a small chance that this might have caused your problem since you're using yanfly's savecore.

That said, it really sounds as if one of your plugins places more and more data into your saves the longer you play.
Unfortunately you have a rather large and mixed list of plugins, and I don't know most of them enough to guess which one increases the save size.
And the one plugin that is guaranteed to do this (yanfly's itemcore with settings for independent items/equipment) is not in your list (or I didn't see it)
For save file bloat, you'd generally be looking for plugins that add save data. From that plugin list, I'd guess YEP_SaveCore and/or YEP_SaveEventLocations. Like others have said, though, it could be all sorts of things.

You might find this helpful:
Spoiler: plugin code
JavaScript:
/*:
 * @target MV MZ
 * @plugindesc Simple save file size reporter.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/167816/
 * @help Use this script call in-game to show info on current save data size:
 *     debugSaveSize();
 *
 * Free to use and/or modify for any project, no credit required.
 */
debugSaveSize = function(toConsole) {
'use strict';
  const l = function(v) {
    return LZString.compressToBase64(JsonEx.stringify(v)).length;
  };
  const f = toConsole ?
    function(o) {
      for (const k in o)
        o[k] = { size: l(o[k]) };
      return o;
    }
    :
    function(o) {
      let s = "";
      for (const k in o)
        s += k + " = " + l(o[k]) + "\n";
      return s;
    };
  const c = f(DataManager.makeSaveContents());
  const i = f(DataManager.makeSavefileInfo());
  const h0 = "Save contents (playthrough data)";
  const h1 = "Save info (selected data for save menu)";
  if (toConsole) {
    console.log(h0);
    console.table(c);
    console.log("---");
    console.log(h1);
    console.table(i);
  } else {
    alert(h0 + "\n\n" + c);
    alert(h1 + "\n\n" + i);
  }
};
The displayed size estimates (bytes) will be a bit higher than they are in-file, since this compresses each component individually rather than all components together. Should still be a good indicator of relative sizes, though! 👋
If the FPS is still stable I wouldn't worry about it too much and instead perhaps find a way to pull up a loading/saving icon to let the player know that their game hasn't crashed.
Thanks, I'll look for a way to add that.

I do hope that I'm wrong though and there is some way to get this loading faster with a simple fix.
I hope for the same! >.< Thank you very much!

such as a large amount of unique equipments that the player can collect or upgrade, that is accumulated over time.
There are 84 weapons and 178 pieces of equipment. However, there are 200 maximum of each, being the remaining ones unused. I'll reduce the maximum amount to the actual number being used, just in case.

Is the save file size also increasing with time?
Yes, the save file size increases with time. Also, I've just found a larger file, 7.05MB. I didn't progress as much in this one, but it has 71 hours of playtime (most probably being idling). New save files, on the other hand, have 90kb, 128kb, 368kb, etc. Here is a screenshot of my project's save folder.
1713373503468.png


Those barely affect anything here. As a quick test, I've made 10,000 variables and assigned a random value between 1 and 1000 to each - the save file is still only 39KB (and that's together with everything else my game saves), nowhere near 7MB.
That's a relief. Touching thousands of switches and variables now would take a long time. Furthermore, not all of them are used in Story Mode, which is the one I'm dealing with.

I also assume that you can't just try turning off different plugins and see what happens.
Unfortunately, at the current point, nothing changed. I've just tried removing all the unused plugins from the list and turning off all but the ones that won't let me play if turned off. So only OrangeGreenworks and SRD_TranslationEngine are turned on. Saving time is still the same, unsteady, varying from 18 to 25 seconds when overwriting and around 8-12 seconds when creating a new file. Maybe something different could happen if I played for 40+ hours without any script on, which I don't think will be even possible since I haven't tested battling, for example, without any scripts. I'll have to test that out.

Can you share one these bloated save files? It might be possible to identify the problem from looking at one.
Sure, here are two of my largest save files. If I open them with Notepad, only encrypted text shows up, lots of random letters.

The order of your yanfly plugins is way off.
Thanks. I have to go to work now, but when I return, I'll definitely try to rearrange them and test the game.

it really sounds as if one of your plugins places more and more data into your saves the longer you play.
Hmm... I'll have to create a new save file with fewer scripts turned on then to test it out. Thank you for the help.

yanfly's itemcore with settings for independent items/equipment
Maybe I faced some sort of incompatibility problem because I remember actively choosing not to use this one.

you'd generally be looking for plugins that add save data. From that plugin list, I'd guess YEP_SaveCore and/or YEP_SaveEventLocations.
I didn't even think of SaveEventLocations. Thank you. I'll take a look at it, too, though I only actively use it in a specific part of the game.

You might find this helpful
As I said, I'm going to work now. I'll check it when I come home. But sorry, should I add it to the game as a script and run the game? Though I have these many scripts in my game, my JavaScript knowledge is very limited.

Thank you everyone for your help so far. I'm still looking forward to other opinions, but I'll definitely take yours into consideration while investigating the issue. <3
Sure, here are two of my biggest save files. If I open them with Notepad, just encrypted text shows up, lots of random letters.
It's not actually encrypted, just compressed (like a text file equivalent of a zip file).

So, 99.56% of that file is taken up by system._monsterBookItems
1713380450057.png


I'm guessing MrTS_MonsterBook might be the culprit (I don't know anything about that plugin, but the name does sounds suspiciously like something that would create that variable).
Note that removing it won't change anything on save files that are already bloated (the data is already there, part of system, whether it's used or not), but should stop new files from getting bloated.

EDIT: I've looked at it, and I can confidently say that this plugin is indeed the cause. What I don't know is what on Earth was its developer thinking 😛 Every time a monster is defeated, its drops are added to the list of discovered drops. Without checking if they're already there. So with every monster you defeat, the list just keeps growing with no limit.

I've made a potential fix (untested).
Add this as a plugin below MonsterBook
JavaScript:
void ((alias) => {
    Game_System.prototype.monsterBookDiscoverItem = function (_id, item) {
        if (!this._monsterBookItems[_id] || !this.isEnemyItemRevealed(item, _id)) alias.call(this, _id, item);
    }
})(Game_System.prototype.monsterBookDiscoverItem);

Game_System.prototype.addMonsterItemProperly = function (id, item) {
    let newItem = { id: item.dataId, kind: item.kind };
    if (!this._monsterBookItems[id]) this._monsterBookItems[id] = [];
    if (this._monsterBookItems[id].some(i => i.id === newItem.id && i.kind === newItem.kind)) return;
    this._monsterBookItems[id].push(newItem);
}

And inside MonsterBook, replace line 851 (this._monsterBookItems[_id].push({id: item.dataId, kind: item.kind});) with
this.addMonsterItemProperly(_id, item);
So, 99.56% of that file is taken up by system._monsterBookItems
Oh, gosh. I would never have thought that was the one causing it. I thought it was a regular RMMV issue.

Thanks for checking it for me. And thanks everyone for all the help provided.

Every time a monster is defeated, its drops are added to the list of discovered drops. Without checking if they're already there. So with every monster you defeat, the list just keeps growing with no limit.
Omg, thank you very much for looking into it. That explains the issue, I use battling a lot, especially with multiple-enemy troops/parties/groups, so, after some number of enemies are defeated (and consequently more data is registered into the save file), the player starts facing huge delays when saving. It would've taken me weeks or months to identify what the problem was within the script by myself, and I would probably have had to save some money to pay someone else to fix it for me.

I've made a potential fix (untested).
Add this as a plugin below MonsterBook
You're an angel! I'll test it out. Can I contact you for requesting and paying for a new fix in case this one doesn't work?

Also, I'll send you all Steam keys for the game through dm after I fix it. Thank you very much, everyone.

(Please, don't close the thread yet, I'll still test it and report back when I get home)
Can I contact you for requesting and paying for a new fix in case this one doesn't work?
Sure, feel free to. Though hopefully this will only require small tweaks if it doesn't work right away, and those I'd be happy to do for free.
I've made a potential fix (untested).
Add this as a plugin below MonsterBook
JavaScript:
void ((alias) => {
Game_System.prototype.monsterBookDiscoverItem = function (_id, item) {
if (!this._monsterBookItems[_id] || !this.isEnemyItemRevealed(item, _id)) alias.call(this, _id, item);
}
})(Game_System.prototype.monsterBookDiscoverItem);

Game_System.prototype.addMonterItemProperly = function (id, item) {
let newItem = { id: item.dataId, kind: item.kind };
if (this._monsterBookItems[_id].some(i => i.id === newItem.id && i.kind === newItem.kind)) return;
this._monsterBookItems[_id].push(newItem);
}
And inside MonsterBook, replace line 851 (this._monsterBookItems[_id].push({id: item.dataId, kind: item.kind});) with
this.addMonterItemProperly(_id, item);

When I try to load a save, an error message shows up:
ReferenceError
_id is not defined

This is the console error message:
Code:
rpg_managers.js:1949 ReferenceError: _id is not defined
    at Game_System.addMonterItemProperly (Mac_MonsterBookFix.js:9)
    at Game_System.monsterBookDiscover (MrTS_MonsterBook.js:851)
    at Game_Interpreter.pluginCommand (MrTS_MonsterBook.js:266)
    at Game_Interpreter.pluginCommand (YEP_SaveEventLocations.js:265)
    at Game_Interpreter.pluginCommand (YEP_PartySystem.js:731)
    at Game_Interpreter.pluginCommand (YEP_X_InBattleStatus.js:489)
    at Game_Interpreter.pluginCommand (YEP_X_TurnOrderDisplay.js:603)
    at Game_Interpreter.pluginCommand (ALOE_VirtualButtons.js:1452)
    at Game_Interpreter.pluginCommand (YEP_MusicMenu.js:864)
    at Game_Interpreter.command356 (rpg_objects.js:10508)

I've added your code as a plugin below MrTS_MonsterBook:
1713408764294.png


Sure, feel free to. Though hopefully this will only require small tweaks if it doesn't work right away, and those I'd be happy to do for free.
Again, thank you very much! How should I refer to you on the game's credit page? And which website/link should I add for people who want to see your work?

Also, should this conversation continue in DM then I can come back here and report the outcome? Or should we keep it here so that everyone can see the process and details that led to the final fix?

I've restored the plugin to the original version and disabled your plugin for now, to do some testing.

Moreover, in my game, the monster book information isn't taken through encounters or victory in battle, but rather from an NPC that analyzes (and takes) certain drops from the monsters. I was wondering if it would be too complicated to add a feature like "detect if a monster has been discovered" (through conditional branches) to that plugin. That way, I could use a Common Event to detect which monsters have already been discovered, save that information into regular RMMV variables, then clear the Monster Book and fill it up again by using conditional branches and the information from the variables. That could be a very quick way to fix the existing bloated saves. I've just tried clearing up the Monster Book, and it's worked! It's incredibly reduced the file size to 123KB! Saving again, even when overwriting, is now instantaneous!

1713409915752.png


Now, I'll only have to work on avoiding making players completely lose their current Monster Book information. Please, tell me whether we should continue in DM or here, and if that would require commissioning.

Thanks a lot for everything so far!
_id is not defined
... I've really jinxed it when naming that function, haven't I xD
I've edited the code in that post, so at the very least this particular error should be fixed now.

add a feature like "detect if a monster has been discovered"
There's no need, it seems to already exist 😉 Use a conditional branch with $gameSystem.isEnemyRevealed(id), replacing "id" with the id of that monster.

How should I refer to you on the game's credit page? And which website/link should I add for people who want to see your work?
Mac15001900 is fine. I don't have much of an online presence, but I guess my itch page would do if you are adding links https://mac15001900.itch.io/.

Please, tell me whether we should continue in DM or here
It should be fine to just continue here. It also has the added benefit of other people potentially spotting another solution that we're missing and chiming in.
I've edited the code in that post, so at the very least this particular error should be fixed now.
Thanks, it does work now! <3
Just one thing, I noticed it says "Monter" instead of "Monster" in some parts of the code. I'm pointing that out in case someone wants to do something else with it and doesn't notice it.

There's no need, it seems to already exist 😉 Use a conditional branch with $gameSystem.isEnemyRevealed(id), replacing "id" with the id of that monster.
That's amazing! I'm using it now.

Mac15001900 is fine. I don't have much of an online presence, but I guess my itch page would do if you are adding links https://mac15001900.itch.io/.
Okiedokie! You should consider at least a WordPress for yourself. haha

It should be fine to just continue here. It also has the added benefit of other people potentially spotting another solution that we're missing and chiming in.
Good point. However, from my end, I think we're done now. I'll explain why:



With y'all's help, I've managed to solve the issue.

Instead of creating a huge amount of conditional branches to check each enemy and then the same amount again to reactivate them, what I ultimately did was check the MrTS_MonsterBook script for another function:

JavaScript:
$gameSystem.monsterBookDiscover(Number(args[id]), true);

With that and a little help from ChatGPT, the final part of the code was created.

I first had to separate some subsequent switches for each monster within my Monster Book (52 total, starting from Switch 3278). When using the code below, remember to replace "3277" with the first monster switch number minus 1. Also, my switch 1 ("teste") is activated when starting a new game. If you don't have one like that, you might have to use another switch that detects that the game isn't new. I've now activated the "bloatedsave-fix" switch before the "teste" switch when starting a new game so that new games don't have to go through this code.

This is what my common event page looks like:
1713462570015.png


I used the plugin command MonsterBook clear in the middle because it's easier and I was too lazy to look for the proper script call within MrTS_MonsterBook.
I also used the plugin command Autosave from YEP_X_Autosave, but you can save the game (or not) any way you want.

Here are the pieces of code:

Part 1 (before clearing up Monster Book):
JavaScript:
// Loop through IDs from 1 to 52

for (let id = 1; id <= 52; id++) {

    // Check if enemy with the specified ID is revealed

    if ($gameSystem.isEnemyRevealed(id)) {

        // Turn on switch 3277 + id

        $gameSwitches.setValue(3277 + id, true);

      }

}

Part 2 (after clearing up Monster Book):
JavaScript:
// Loop through IDs from 1 to 52

for (let id = 1; id <= 52; id++) {

    // Check if the switch corresponding to 3277 + id is on

    if ($gameSwitches.value(3277 + id)) {

        // Run $gameSystem.monsterBookDiscover with the corresponding ID

        $gameSystem.monsterBookDiscover(id, true);

    }

}

I've also added a 60s wait at the end of the Common Event just in case.

I've already updated the game and tested it out. My friend has also tested it. I am immensely thankful for all the help provided on this thread. I've used the "START CONVERSATION!" button to send Steam keys to everyone who has contributed!

I'd appreciate it if someone could just take a look at the code I've created with ChatGPT to check if it may cause any harm in the future. After that, I guess the thread could be closed due to being resolved.

Again, thank you very much, everyone!
I'm so happy! <3 <3 <3
That's awesome, I'm glad that worked! Hopefully this can help a lot of other people too, since it seems like there's a fair amount of them using this plugin.

That code looks fine, though you could also do it with just one variable (42 in this example, feel free to change it). Might be especially useful if someone has a lot of monsters in their game.

Pre-clear:
JavaScript:
let monstersFound = [];
// Loop through IDs from 1 to 52
for (let id = 1; id <= 52; id++) {
    // Check if enemy with the specified ID is revealed
    if ($gameSystem.isEnemyRevealed(id)) {
        // Add its id to the list
        monstersFound.push(id);
      }
}
$gameVariables.setValue(42, monstersFound);

Post-clear:
JavaScript:
let ids = $gameVariables.value(42);
// Loop through found monster IDs
for (let i = 0; i < ids.length; i++) {
    //Add each monster id to the book
    $gameSystem.monsterBookDiscover(ids[i], true);
}

I noticed it says "Monter" instead of "Monster" in some parts of the code
The problem with code auto-completion: make a typo once, and it will helpfully make it for you everywhere else 😛 I've changed it to correct spelling to not confuse anyone else using it.
you could also do it with just one variable
Oh, that's neat! I wasn't sure how to use arrays within javascript/RMMV. Thanks again!

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/solved-rmmv-saving-and-loading-gradually-take-longer.167816/