How to return the player to start after clearing autosave? [Snowman 1.3]

0 votes
asked Jul 15 by cwcorella (240 points)

This is a follow up question to a previous post. How might I return the player to the starting passage after they've selected 'Clear Autosave'? It takes effect when the page is refreshed, but not every player will find this to be intuitive.

Autosave Code (TheMadExile)

/*
	Snowman Web Storage Autosave Module
*/
(function () {
	/*
		ATTENTION: You must configure the save ID properly.

		This is the key used to store the autosave in Web Storage,
		it should be both static and unique to your project.  I'd
		suggest using something like a pen name combined with your
		project's name.
			e.g. 'glitch:AGameOfGnomes'
	*/
	var saveId = 'glitch:AGameOfGnomes';

	/*
		Module Functions.

		You probably won't need to muck with these.
	*/
	function hasWebStorage() {
		try {
			var store = window.localStorage;
			var tid   = '_sm_' + String(Date.now());
			store.setItem(tid, tid);
			var result = store.getItem(tid) === tid;
			store.removeItem(tid);
			return result;
		}
		catch (ex) { /* no-op */ }

		return false;
	}

	function hasAutosave() {
		return localStorage.getItem(saveId) != null;
	}

	function createAutosave() {
		localStorage.setItem(saveId, story.saveHash());
	}

	function restoreAutosave() {
		var hash = localStorage.getItem(saveId);
		if (hash) {
			story.restore(hash);
		}
	}

	/*
		If Web Storage is available: attempt to load an existing
		autosave at startup, set up an event listener to save each
		turn, and export a method to remove the autosave.

		NOTE: The event handling may need to change for the next
		major version of Snowman, as it looks like Chris is making
		significant changes there.
	*/
	if (hasWebStorage()) {
		var eventName = 'showpassage:after';

		if (hasAutosave()) {
			jQuery(document)
				.one(eventName, function () {
					restoreAutosave();
					jQuery(document).on(eventName, createAutosave);
				});
		}
		else {
			jQuery(document).on(eventName, createAutosave);
		}

		window.SWSAutosave = {
			remove : function () {
				localStorage.removeItem(saveId);
			}
		};
	}
})();

Passage Button

<% if (SWSAutosave) { %><a href="javascript:SWSAutosave.remove()">Clear Autosave</a><% } %>

 

1 Answer

+1 vote
answered Jul 15 by TheMadExile (9,520 points)
selected Jul 15 by cwcorella
 
Best answer

This is one of the things you could have asked as a followup on the original. wink


Using the following link markup instead should do it:

<% if (SWSAutosave) { %><a href="javascript:SWSAutosave.remove();window.location.reload();">Clear Autosave</a><% } %>

 

...