Saving Data in the SKSE Co-Save
A plugin stores data in the SKSE co-save through IPerPluginSavedState. The co-save is the .skse file beside each .ess save,
so the data follows its save when the save is copied, renamed, or loaded on another character.
This plugin counts how many times a save has been loaded, and recalls the player's race:
using SkyrimForge.Events;
using SkyrimForge.Game;
namespace SkyrimForge.ExamplePlugins;
/// <summary>Counts how many times this save has been loaded, and remembers the player's race.</summary>
/// <remarks>
/// The count is stored in the co-save, so each save has its own count. It is written to the file when the game next saves.
/// </remarks>
public sealed class SavedStatePlugin(GameTarget gameTarget, IPerPluginSavedState savedState, IGameEvents events, ILog log) : IPlugin
{
private readonly ISavedStateFormIdPair<Race> racePair = savedState.GetFormIdPair<Race>("race");
/// <inheritdoc/>
public void Start()
{
events.OnSaveLoaded.Subscribe(Count);
}
private void Count(SaveLoadedEventArgs args)
{
ISavedStatePair<int> timesLoadedPair = savedState.GetPair<int>("timesLoaded");
int timesLoaded = (timesLoadedPair.Value ?? 0) + 1;
timesLoadedPair.Value = timesLoaded;
string recalledText = DescribeStoredRace();
StoreRace();
log.Write($"This save has been loaded {timesLoaded} times. {recalledText}");
}
// The framework translates a stored form ID into the current load order.
private string DescribeStoredRace()
{
if (racePair.Value is not { } raceId)
{
return "No stored race.";
}
return $"Stored race 0x{raceId}" +
(raceId.FormOrNull is { } race ? $": \"{race.GetName()}.\"" : ", which is not loaded.");
}
private void StoreRace()
{
if (gameTarget.Player.GetRace() is { } race)
{
racePair.Value = race.FormId;
}
}
}
GetPair<T>returns the entry with a given key, read and written asT, a type such asintorbool.GetFormIdPair<TForm>returns one that holds a form ID. Nothing is read or written until the pair'sValueis.- The store holds the loaded save's entries from OnSaveLoaded onward, and is emptied as a new game starts or a save starts loading. A value written reaches the file when the game next saves.
- A stored form ID is mapped through the load order the save recorded, so it identifies the same form after the load order changes.
- Entries belong to the plugin, so two plugins using one key do not collide. Keys that start with
SkyrimForge.belong to the framework. - Members can be called on any thread, and each is atomic. A read and a write are two calls, though, so a plugin that writes a value derived from one it read, from handlers on more than one thread, acquires a lock around both.
Data for a Specific Form
GetFormEntries gives the entries stored under one form. This plugin counts how often the player opens each container:
using SkyrimForge.Events;
using SkyrimForge.Game;
namespace SkyrimForge.ExamplePlugins;
/// <summary>Counts each time the player opens a container and stores the count per container, with no Papyrus script and no record edit.</summary>
/// <remarks>
/// Attaching a script to every container means overriding every container record, and only the last override in the load order takes effect.
/// Testing the base object's form type overrides no record and also matches containers added by other .esp files.
/// </remarks>
public sealed class ObjectScriptPlugin(IGameEvents events, IPerPluginSavedState savedState, IDebug debug, ILog log) : IPlugin
{
/// <inheritdoc/>
public void Start()
{
log.Write("Counting each container the player opens.");
events.OnPlayerActivate.Subscribe(args =>
{
if (args.ObjectActivated is { } activatedReference && activatedReference.GetBaseObject()?.GetFormType() == FormType.Container)
{
ISavedStatePair<int> openCountPair = savedState.GetFormEntries(activatedReference).GetPair<int>("opened");
int openCount = (openCountPair.Value ?? 0) + 1;
openCountPair.Value = openCount;
debug.Notification($"Opened {openCount} times");
}
});
}
}
The framework maps each form through the save's load order, as it does a stored form ID. Entries under a form whose .esm, .esp, or .esl file is no longer loaded are dropped as the save is read.
GetFormEntriesKeys lists the forms that have entries. RemoveFormEntries removes a form's entries, for example from an OnFormDelete handler.