Skip to content

export/import of savegame #103

@Pertex

Description

@Pertex

It would be nice to have the possibility to export savegames. Its saver than to save the game in the localstorage

I am doing it with this code

	squiffy.story.save= function() {
        const localStorageData = JSON.stringify(localStorage, null, 2); 
        const blob = new Blob([localStorageData], { type: "application/json" });

		const url = URL.createObjectURL(blob);
		const link = document.createElement("a");
		link.href = url;
		link.download = "savegame.json"; 
		document.body.appendChild(link);
		link.click();

		document.body.removeChild(link);
		URL.revokeObjectURL(url);
    };

	squiffy.story.load= function() {
		const input = document.getElementById('fileInput');
		if (!input.files.length) {
			squiffy.story.toggleInputBox();
			alert("Choose a file and click the button 'LOAD'");
			return;
		}
		const file = input.files[0];
		const reader = new FileReader();
		reader.onload = function (event) {
			try {
				const data = JSON.parse(event.target.result);
				// Optional: Vorher localStorage leeren
				 localStorage.clear();
				for (const key in data) {
					if (data.hasOwnProperty(key)) {
						localStorage.setItem(key, data[key]);
					}
				}

				alert("Spielstand erfolgreich wiederhergestellt.");
				squiffy.story.toggleInputBox();
				location.reload();
			} catch (error) {
				alert("Fehler beim Einlesen der Datei: " + error.message);
			}
		};
		reader.readAsText(file);
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions