Save Update Issue

I’m not sure if anyone experience this issue. So what happens is that whenever I add something in the StoryInit passage, like a variable. It does not update with a previous save. So whenever I add that variable and save and then run the game. The variable does not appear, but all the other previous variable are there. Can any one help me with this. This has turned into a major issue for me.

It’s because the StoryInit passage is read only at the start of the story. So you have to restart your story for any change there to apply.

Is there anyway to create an automatic update of the variables?

The story JS gets executed each time the page is loaded IIRC, so you can put a statement there. You can use a nullish coalescing assignment to set a variable if it’s not already set. For a global variable I think you can use State.variables.varname, the $ notation doesn’t work in the story JS IIRC.

I haven’t done it myself but I know you will need:

This is an example taken from fg109’s post in the Twine Discord:

Save.onLoad.add(function (save) {
    /* exit the function early if the save is already up to date */
    if (save.version === Config.saves.version) return;
    /* assign 0 to save.version if it is not defined */
    save.version ??= 0;
    for (const moment of save.state.history) {
        const sv = moment.variables;
        if (save.version < 1) {
            /* update variables to align with version 1 */
            sv.someVarAddedInVersion1 = 'some value';
        }
        if (save.version < 2) {
            /* update variables to align with version 2 */
            sv.someVarAddedInVersion1 = 'new value';
        }
        if (save.version < 3) {
            /* update variables to align with version 3 */
            sv.someVarAddedInVersion3 = 15;
        }
        /* etc */
    }
    /* if you want to change save.version, do so here */
    save.version = Config.saves.version;
});
1 Like