[Snowman 2.0.2] Some user-defined variables and functions are not working afte Save/Load

Hi,

I created a save/load system like this:

JS:

let s = window.story.state;
window.setup = window.setup || {};
let se = window.setup;

function setMiscFunctions() {
    se.loadSaveFile = (input) => {
	 let file = input.files[0];
	 let fileReader = new FileReader(); 
	 fileReader.readAsText(file); 
	 fileReader.onload = function() {
	    window.story.restore(fileReader.result);
	 }; 
	 fileReader.onerror = function() {
	    alert(fileReader.error);
	 }; 
    }

    se.saveToFile = () => {
	 let fileContent = window.story.save();
	 let bb = new Blob([fileContent], { type: 'text/plain' });
	 let a = document.createElement('a');
	 a.download = 'varangian.save';
	 a.href = window.URL.createObjectURL(bb);
	 a.click();
    }
}

$(window).on('sm.story.started', function(event, eventObject) {
    setMiscFunctions();
    s.passageIncrement = 0;
});

$(window).on('sm.passage.showing', function(event, eventObject) {
    if (eventObject.passage.name != "Start") {
    	s.passageIncrement++;
    }
});

Story Save/Load:

<a href="javascript:void(0)" onclick="window.setup.saveToFile()">Save</a>

<input type="file" onchange="window.setup.loadSaveFile(this)" id="file-selector">

The issue is after Save/Load s.passageIncrement++ is not working. Save/Load functions also didn’t work before I exported them from window.story.state to window.setup(), but windows.setup() doesn’t help s.passageIncrement++.

I don’t get it, don’t saved/loaded games work with sm.passage.showing triggered functions?

Got it.

Seems like userscript on save/load doesn’t define let s = window.story.state; at the beginning of the JS file.

This works:


$(window).on('sm.passage.showing', function(event, eventObject) {
    if (eventObject.passage.name != "Start") {
    	window.story.state.passageIncrement++;
    }
});