Skip to content

State and settings

Inkweaver saves your plugin’s data for you. There are two kinds:

  • State follows the story. It’s saved in bookmarks, and it rewinds and replays along with the reader.
  • Settings belong to the reader, not to a point in the story. They’re the place for preferences like volume or text speed.

Your plugin’s state is a single object that holds any serializable data. It’s private to your plugin.

You can read your state with getState. To change it, use setState or updateState:

const { inventory } = getState();
updateState({ inventory: [...inventory, "brass-key"] });

The object getState returns is frozen, so you can’t edit it in place. Always use setState or updateState. That’s how Inkweaver records the change in the reader’s history.

Change state while Inkweaver is updating a beat. You can do that in any of these:

When the update finishes, Inkweaver takes a snapshot of every plugin’s state. The reader can rewind to that snapshot, or save it in a bookmark. You can read more in the beat loop.

onInitState sets your state’s starting value. Most plugins only need a default:

onInitState({
default: { journalEntries: [] },
});

If a new release of your plugin changes the shape of its data, or fixes how the data was stored, add a migration. The key is the version that introduced the change. The function takes the data from the previous version and returns it in the new shape:

// Adds an `isRead` flag to each journal entry, defaulting to unread.
function toV2(oldState) {
const journalEntries = oldState.journalEntries.map((entry) => ({
...entry,
isRead: false,
}));
return { ...oldState, journalEntries };
}
onInitState({
default: { journalEntries: [] },
"2.0.0": toV2,
});

Inkweaver records which version of your plugin saved the reader’s data, and runs only the migrations it still needs. The version comes from the version field in your plugin.json, or in project.json for your novel’s own scripts.

For every option, see onInitState.

Settings work like state, but they aren’t tied to the reader’s place in the story. You can read and change them using getSettings, setSettings, and updateSettings:

const { masterVolume } = getSettings();
updateSettings({ masterVolume: 0.8 });

onInitSettings sets your settings’ starting values. It takes the same versions and migrations as onInitState.

To let the reader change a setting, add a field for it to a menu, and connect the field’s value and setValue to your settings. Menus and input walks through an example.