You are viewing a potentially older version of this package. View all versions.
nfp007-SmartSave-1.3.0 icon

SmartSave

Don't lose progress when a game crashes. Saves your character after crafting, chests, tombstone and before teleports, never moves your respawn point in combat, and cuts the save freeze to ~20 ms, Steam Cloud included. Client-side.

Date uploaded 2 days ago
Version 1.3.0
Download link nfp007-SmartSave-1.3.0.zip
Downloads 62
Dependency string nfp007-SmartSave-1.3.0

This mod requires the following mods to function

denikson-BepInExPack_Valheim-5.4.2351 icon
denikson-BepInExPack_Valheim

BepInEx pack for Valheim. Preconfigured with the correct entry point for mods and preferred defaults for the community.

Preferred version: 5.4.2351

README

SmartSave

Don't lose your progress when the game crashes.

Valheim saves your character (inventory, equipment, skills, recipes, map) only every few minutes, or every 30 minutes without a server setting. When your game crashes, everything since that save is gone:

  • the armor you just crafted, and the materials it used up,
  • items you moved in or out of a chest: duplicated or lost for good, because the chest is saved on the server but your bag isn't,
  • your whole corpse run, if you had just emptied your tombstone,
  • skill levels, new recipes, a forsaken power.

SmartSave saves your character at exactly those moments: after crafting, after closing a chest, after taking your tombstone, right before every teleport, and more. A crash costs you seconds, not minutes.

It never moves your respawn point while you are fighting, falling or swimming, so after a crash you don't reconnect into the fight.

And it makes saving much faster, including the game's own autosave. The explored map, the slowest part of every save, is built in one go and reused when nothing changed: under 1 ms instead of ~200 ms. Steam Cloud characters are written in the background. Double saves are skipped. A save now freezes the game for about 20 ms, with local and Steam Cloud characters alike: barely noticeable.

Client-side only. The server doesn't need it, and it works on any server and in single player, with local or Steam Cloud characters.

Source code, issues and ideas: github.com/nfp007/smartsave

When it saves

Moment What happens
Crafting, upgrading, repairing Saves when you close the crafting window (one save, however many items you made)
Closing a chest Saves if your inventory changed while it was open. Chest to chest gives one save, not ten
Tombstone Saves right after you take everything back
Before a teleport Portals, Portaler, dungeon entrances, any mod that teleports. Loading screens are where games like to crash. By default this save also writes the map
After arriving Saves 5 seconds after a teleport
Boss / forsaken power Saves after a boss dies near you or when you take a new power
Skill level up, new recipe Saves at the next calm moment
After combat Anything that waited is saved when the fight is over
F6 Save right now, everything including the map
Fallback If nothing saved for 30 minutes, it saves at the next safe moment

Two kinds of save

  • Inventory save (items, skills, map): fine almost any time.
  • Logout point (where you appear after a crash or reconnect): only moved when you stand on solid ground and are not in combat, not swimming, not riding and not mid-air.

So a crash in the middle of a fight brings you back with your items, but not into the fight.

SmartSave also fixes this for the game's own server save: with -saveinterval, the server tells every client to save, and vanilla always moves your logout point, even mid-fight. SmartSave keeps the save but leaves the logout point where it was.

Combat is detected from the game's own combat-music state, damage you take, your own attacks and blocks, and alerted enemies within 30 m that target you.

How long a save takes

Every character save freezes the game for a moment. That's the game's own save routine, the same as its regular autosave. Two things decide how long it takes: whether the explored map is written, and where your character is stored.

Measured on a normal PC (normal frame 20-30 ms), how long one save freezes the game:

Character storage Without SmartSave With SmartSave, map reused With SmartSave, right after exploring With SmartSave, save without map
Local ~200-280 ms ~20 ms ~70 ms* ~20 ms
Steam Cloud ~300-400 ms ~17-21 ms ~72-88 ms ~17 ms

With SmartSave the map part of a save takes under 1 ms when the map is reused, and ~50 ms when it has to be built because you just explored. With Steam Cloud, the remaining ~13 ms is the game's own check before every cloud save; writing the file happens in the background (~90-150 ms, without freezing the game).

* Calculated from the measured parts (map ~50 ms plus the rest of a local save ~20 ms), not measured as a whole.

Faster map save. The game builds the map data for a save one point at a time, about 8.5 million steps. SmartSave builds the same data in one go: about 10 ms instead of 140-230 ms, plus ~40-60 ms for compressing it. This makes the game's own saves faster too.

Reusing the map. If your explored map and pins haven't changed since the last save, SmartSave reuses the last map data (under 1 ms). If they have changed, SmartSave prepares the map data in the background every minute (MapPrewarmSec), on another CPU core, so the next save can usually reuse it too.

Safeguard: once per game start, when you join a world (usually behind the loading screen), SmartSave builds the map data both ways, its own and the game's, and compares them byte by byte. Only if they are identical does it use its faster version for the rest of the session. If they differ (after a game update, or because another mod changes the map data), it switches back to the game's own map save and writes a warning to the log.

No double saves. The game's own regular save (the server's save interval, or the game's 30-minute timer) is skipped if a full save, with the map and your position, already ran within the last 30 seconds (SkipRepeatSaveSec). Saves when quitting, logging out or sleeping are never skipped.

The map is still the biggest part of a save. By default (MapSave = OnPortal), only the save right before a teleport writes it. The other SmartSave saves keep your items, skills and position safe and leave the stored map as it was. The map is also saved by the game's own saves (server save interval, logout) and by F6. After a crash you can lose map exploration and new pins since the last save that wrote the map. Never items. Because the map is reused when nothing changed, MapSave = Always now costs little: under 1 ms for the map most of the time, ~50 ms right after exploring.

Steam Cloud in the background (BackgroundWrite). Steam Cloud has no "rename", so the game copies the whole character file three times on every save, all inside the freeze (~100-300 ms). With SmartSave the save is still made at once (all data captured in that frame), but writing it to Steam Cloud happens in the background through Steam's own background write (~90-150 ms, the game keeps running). The game's safe order is kept: first .new, then the previous file as .old, then the new .fch, which is read back and compared before .new is removed. A new save always waits until the previous one is fully written. Quitting and logging out wait for it too. If Steam reports any error, the save is kept as a local backup (like the game does), the game's own save writes the newest data again, and SmartSave switches back to the game's own cloud save until the next start.

Local or Steam Cloud? With SmartSave both save about equally fast. Without SmartSave, a local character saves much faster than a Steam Cloud one. You can move a character between Steam Cloud and local storage in Valheim's Manage saves menu (main menu, character selection). A local character isn't synced to other PCs and isn't backed up by Steam, so keep your own backups.

Settings

All settings are in BepInEx/config/nfp007.SmartSave.cfg (also in ConfigurationManager, F1).

Section Setting Default
General Enabled true
General LogSaves true One line per save in the BepInEx log, with the reason
General SaveMessage Off Small "Saved" message top left: Off, Important (crafting, chests, tombstone, boss) or All. F6 always shows it
General MinSaveIntervalSec 15 Cooldown between two less important saves (arrival, boss, end of combat, skill up, recipe, fallback). Never triggers a save itself; important saves ignore it
General ManualSaveKey F6 None = off. Always saves everything, including the map
General FastMapSave true Builds the map data much faster and reuses it when nothing changed, for SmartSave's saves and the game's own. Same result; checked against the game once per start, and switched off by itself if they ever differ
General MapPrewarmSec 60 Every this many seconds, if the map changed, prepare the map data in the background. 0 = off
General SkipRepeatSaveSec 30 Skip the game's own regular save if a full save ran within this many seconds. 0 = off
General BackgroundWrite CloudOnly Steam Cloud characters: the save is made in the frame, the Steam Cloud file work happens in the background. Off = the game's own cloud save
General MapSave OnPortal When SmartSave's saves also write the explored map (see above): OnPortal, Always, Never
Triggers SaveAfterCrafting / SaveOnChestClose / SaveOnTombstone true
Triggers GuiCloseDelaySec 1.5 The window must stay closed this long before the save
Triggers GuiMaxDelaySec 20 Window open longer than this after a change: save anyway
Triggers SaveBeforeTeleport / SaveAfterTeleport true
Triggers ArrivalDelaySec 5
Triggers SaveOnSkillLevelUp / SaveOnNewRecipe / SaveOnBossKill / SaveAfterCombat true
Triggers FallbackIntervalMin 30 0 = off
Combat CombatCooldownSec 10 "In combat" for this long after damage or an attack
Combat CombatScanRadiusM 30
Combat AllowProfileSaveInCombat true Important saves keep your items in combat (logout point unchanged)
Combat SafeServerPushSave true The server's regular save doesn't move your logout point in combat
Server RequestWorldSaveAsAdmin false Admins only: also ask the server to save the world after important saves. Makes everybody stutter
Server WorldSaveMinIntervalMin 5
Debug Profiling false Measures every save (SmartSave's and the game's own) and writes the times to the BepInEx log and BepInEx/SmartSave_profile.csv. Needs a restart

Good to know

  • Each save takes a moment (see above). That's why there's a cooldown and saves are merged.
  • Chests, buildings and tombstones live on the server, so a crash of your game doesn't touch them. Your character file is on your PC, and that's what SmartSave protects.
  • It doesn't stop the game from crashing. It makes sure a crash costs you as little as possible.
  • If another map mod misbehaves (pins or map not saved as expected), set FastMapSave = false. Everything is then back to the game's own map save.
  • On a host (non-dedicated) SmartSave never delays the world save.

Installation

With a mod manager (Gale, r2modman, Thunderstore App): install SmartSave.

By hand: install BepInExPack Valheim, then copy SmartSave.dll into BepInEx/plugins/.

CHANGELOG

Changelog

1.3.0

  • New BackgroundWrite (CloudOnly, on by default): for Steam Cloud characters the save is still made in the frame, but writing it to Steam Cloud happens in the background through Steam's own background write. A Steam Cloud save now freezes the game for ~17-21 ms instead of ~100-300 ms (measured; ~72-88 ms right after exploring).
  • The game's safe order is kept (.new, .old, .fch), every save is read back and compared, a new save waits until the previous one is fully written, and quitting and logging out wait for it.
  • On any Steam error the save is kept as a local backup, the game's own save writes the newest data again, and the feature switches itself off until the next start.

1.2.0

  • The map data is reused when your explored map and pins haven't changed: the map part of a save now takes under 1 ms instead of ~50 ms (measured).
  • New MapPrewarmSec (60 s): when the map has changed, its data is prepared in the background, so the next save can reuse it.
  • The fast map save's safeguard check now runs when you join a world (usually behind the loading screen), not at the first save.
  • New SkipRepeatSaveSec (30 s): the game's own regular save is skipped right after a full SmartSave save (with the map and your position).
  • New icon: an iron-rimmed wooden shield with a glowing save symbol.

1.1.0

  • README: the save time table now compares the game's own map save, SmartSave's fast map save and a save without the map side by side.
  • Corrected the Steam Cloud time with the fast map save: ~170-310 ms (was ~170-270 ms).

1.0.0

  • Faster map save (FastMapSave, on by default): the explored map data is built in one go instead of one point at a time (~10 ms instead of ~140-230 ms, plus compression). Every save that writes the map gets faster, including the game's own saves. The result is the same as the game's; the first map save after each game start builds it both ways and compares them, and the faster version is switched off with a log warning if they ever differ.
  • SaveMessage setting (Off by default, Important or All) replaces ShowSaveMessage.

0.2.0

  • Much shorter freeze: SmartSave's saves no longer write the explored map every time. The map is about 85-90% of a save (with a local character a save takes ~20 ms instead of ~200-280 ms). New setting MapSave: OnPortal (default, only the save right before a teleport writes the map), Always or Never. The manual save key (F6) and the game's own saves always write the map.
  • New [Debug] Profiling setting (off by default): measures every character save, SmartSave's and the game's own, and writes the results to the BepInEx log and BepInEx/SmartSave_profile.csv.
  • Fixed: logging in triggered "new recipe" and "forsaken power" saves.
  • README: how long a save takes, and why a local character saves faster than a Steam Cloud one.

0.1.0

  • First version.
  • Saves after crafting, chest changes, tombstone recovery, before and after teleports, boss kills, skill level ups, new recipes, after combat, F6, and a 30-minute fallback.
  • The logout point only moves when you are safe (not in combat, on the ground, not swimming or riding).
  • The server's regular save (-saveinterval) doesn't move the logout point in combat.
  • Never delays the world save on a host.