Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Auto-save and auto-load

Hi all, are there any samples (.tws files) available for auto-save and auto-load? I'm using Twine 1.4.2 in combination with the Sugarcube 2 story format. It is possible for me to store / load local variables to/from localStorage (see below) but it's more efficiënt (for my game) to save the entire state after every passage (I use a lot of variables, it will be a rpg style gamebook)...

I know this has been asked several times (after googling) but I can't seem to get a clear and workable sample (which I segment out / copy-paste ;) into my game).

::script
Macro.add("SaveState",{
handler : function() {
localStorage.setItem("LOCATIE", this.args[0]);
}
});

Macro.add("LoadState",{
handler : function() {
state.active.variables["LOCATIE"]=localStorage.getItem("LOCATIE");
}
});

::passage save
passage load
<<set $LOCATION to "loc001">>
<<SaveState $LOCATION>>

:: passage load
<<LoadState>><<print $LOCATIE>>


I hope someone can point me to the right direction. The sugarcube 1 samples (http://www.motoslave.net/sugarcube/1/) for saving/loading state looked promising but i can't use them in sugarcube 2.
I learned something from this url: https://twinery.org/forum/discussion/4913/any-way-to-pull-data-variables-from-a-previous-story ... it's almost the same what i was looking for, but I want an auto-save on every passage and at startup (via StoryInit or Start) I want an auto-load, if possible ;)

Hope someone can help me out...

With kind regards, Jeroen Wolf

Comments

  • edited July 2016
    Can't you just use the config object?

    Put
    config.saves.autosave = "autosave";
    

    in your javascript passage.

    Then on any passage where you want the autosave to take effect from tag it with "autosave".

    To load the autosave put the loa api inside a click macro or button eg

    <<click "Load Autosave">><<script>>SaveSystem.loadAuto()<</script>><</click>>


    You can use the config object
    config.saves.autosave = true;
    

    To autosave it on every single passage without the need for tags but you have to be careful with that if your game has a title or splash screen.


    If you want an autoload on startup just write
    config.saves.autoload = true;
    

    in javascript passage.



    Your current save and load macros look a bit overly complex as well. SugarCube has a built in save system which you can use. http://www.motoslave.net/sugarcube/2/
  • One thing to remember when implementing auto-save/auto-load is to also include a way for the Reader to reset it or they may notable to read your story a second time from the start without clearing their web-browser cache. (which they may not want to do!)
  • Hi Claretta, thx for your answer, also thank you greyelf for your comment. I'm still struggeling to implement my wishes into my script. I've deleted the localstorage syntax and added your advice (lines of code) and a short syntax from themadexile into the config script:

    config.saves.isAllowed = function () {
    if (tags().contains("nosave")) {
    return false;
    }
    return true;
    };

    After your recommendation (Claretta) my short dungeon sample (I've included the .tws file) remembers the story. But I can't seem to get the save api working (http://www.motoslave.net/sugarcube/2/docs/api-save.html) the way I want to.

    What I want (also you recommendation greyelf ;))

    I want my adventure to start at the ::Start passage. There should be an logic to check if an autosave is present. I want it to auto load (no prompt). If so, the reader should have to option to reset the game (thus deleting the autosave (read: all variables) and going to location 1 of my script) or to continue the game.

    I know I do someting wrong here, but after inserting this command in the ::Start, just to see if I can get the save api commands to work (in this case delete the autosave):
    <<click "reset">>
    <<script>>
    Save.clear()
    <</script>>
    <</click>>

    Nothing seems to happen. I know it should be something minor ;) I hope, but if anyone can help me out with this.. Again I'm using Twine 1.4.2. in combination with the Sugarcube 2 story format.

    With kind regards, Jeroen Wolf
  • edited July 2016
    I want my adventure to start at the ::Start passage. There should be an logic to check if an autosave is present. I want it to auto load (no prompt). If so, the reader should have to option to reset the game (thus deleting the autosave (read: all variables) and going to location 1 of my script) or to continue the game.
    You've virtually described exactly what the autoload "prompt" setting does, so why don't you want to use it again?

    Regardless, if you want to handle loading of the autosave manually, which you apparently do, then you cannot use the autoloader, since the whole point of it is to automatically load the autosave.

    To manually replicate what the autoloader does, remove the Config.saves.autoload setting from your config passage and replace your Start passage with something like the following:
    <<if Save.autosave.ok() and Save.autosave.has()>>\
    An autosave exists. Load it now or go to the start?
    
    <<button "Load">>
    <<script>>Save.autosave.load()<</script>>
    <</button>> &nbsp; \
    <<button [[Restart|location 2]]>>
    <<script>>Save.autosave.delete()<</script>>
    <</button>>
    <<else>>
    <<goto "location 2">>
    <</if>>
    
    Also, SugarCube v2's Config object uses a capital C. For compatibility reasons a lowercase c works, but you should spell it correctly.

    I know I do someting wrong here, but after inserting this command in the ::Start, just to see if I can get the save api commands to work (in this case delete the autosave):
    <<click "reset">>
    <<script>>
    Save.clear()
    <</script>>
    <</click>>

    Nothing seems to happen.
    All that does is delete all of the players saves (incl. the autosave), not simply the autosave—no fanfare is involved. I'm unsure what you were expecting it to do.
  • edited July 2016
    Hi Thomas, thx, again ;) that worked spot on :)

    For reverence (if someone is interested), I've updated and uploaded this mini dungeon with a start menu (continue, restart or begin). This .zip file contains the .tws file, .html output file and a exported .apk (android) file.

    Thx again ... also thank you Claretta (hope your game is progressing nicely...) and greyelf for your comments earlier.
    Jeroen
  • The .zip file ;)
Sign In or Register to comment.