Howdy, Stranger!

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

Automatically Saving the Game in Harlowe

Because my current story is long, I added an auto-save system to it. That way, if you stop playing part-way through and then come back to it, it'll let you continue where you left off. In case others might find it useful, I'm posting it here.

These instructions work as of Harlowe 1.2.1.


1. Set Variables in a Startup Passage

In a startup passage, put the following code:
{
<!-- Set autosave variables -->
(set: $_version to "VERSION NUMBER")
(set: $_autosave_slot to "autosave")
(set: $_autosave_filename to "save v"+$_version)
(set: $_start_passage to "THE REAL START OF THE GAME")
}

Change "VERSION NUMBER" to something like "1.0". Every time you re-release your story, change the version number so the story doesn't try to restore an autosave from a different version.

Change "THE REAL START OF THE GAME" to the name of the passage where your game actually starts. We'll need this because, here in a second, we're going to add a new startup passage that will let the player reload an autosave.


2. Save the Game in a Header Passage

In a header passage, put the following code:
{
<!-- Auto-save our progress (unless a passage forbids it) -->
(unless: (passage:)'s tags contains "nosave")[
	(save-game: $_autosave_slot, $_autosave_filename)
]
}

Now, every time a new passage loads, the game is saved. If you don't want the game saved on a certain passage, add the "nosave" tag to it.


3. Offer to Reload the Autosave in a New Passage

Finally, create a new passage and set it to be your starting passage. In it, put the following:
{
<!-- Set initial variables that change here instead of in the Startup passage -->
<!-- YOUR VARIABLES GO HERE -->

(unless: (saved-games:) contains $_autosave_slot and (datavalues: (saved-games:)) contains $_autosave_filename)[(goto: $_start_passage)]
}''[[NEW GAME->$_start_passage]]

(link: "CONTINUE")[(load-game: $_autosave_slot)]&nbsp;&nbsp;&nbsp;&nbsp;(link: "No")[(goto: $_start_passage)]''

If you've got any variables that you initialize in a startup passage and then change during the story, move 'em into this new passage. Otherwise they'll always be reset after you load your game.


Restarting the Story or Loading a Different Saved Game

If you ever want to re-start the story in the middle of it, you'll have to get rid of the autosave. Harlowe currently doesn't have a way to delete a saved game, but you can cheat by giving the autosave a new filename.
(save-game: $_autosave_slot, "not-a-real-save")
(reload:)

If you add other save slots, you'll need to include the "(save-game: $_autosave_slot, "not-a-real-save")" bit before you restore the game, too.

Comments

  • Thanks so much for this information, sgranade! I've been working with your model, trying to get it to work and it won't. I'm sure there's something I'm doing wrong or missing. I'm attaching a test story with only your script in it. I noticed that when I created the passage in step three, it automatically created a new passage titled "$_start_passage". Would you mind having a look and see what I did wrong?
  • The "Automatically Create Missing Passage" feature of Twine 2 only understands basic markup links.

    When it comes across advance link types (like the NEW GAME->$_start_passage link in the example) it will generally do one of three things:

    1. Ignore the link and not create a missing passage.
    In this case you will need to create the missing passage yourself.

    2. Create an incorrectly named passage.
    In this case you will need to rename the passage yourself.

    3. Create a passage that is not needed. (your situation)
    In this case you will need to delete the passage.
  • I should clarify. I've deleted the auto-created unneeded passage as prescribed and while testing things, it gives the appearance of working, but when I exit the game at say room3, and then reload the game and select "continue', nothing is happening. I suspect progress was never actually saved.
  • I figured out what's going on! There are two problems. The first is that $_start_passage should point to the first passage where your game begins, not the passage where you offer to reload the game.

    In your test game, make the following change:
    (set: $_start_passage to "room1")
    

    The second thing is that the passage where you offer to reload the game needs a "nosave" tag. I completely left that step out of my original instructions.

    So 1. change the value of $_start_passage, and 2. in your "start" room, add the "nosave" tag and see if it works.
  • Awesome, it works now! Thanks for your help and your auto save script. This is indeed most helpful as I'm building a really large game where no saves is just not an option.
    I may try eventually to hack your script to save only at certain passages when a link is clicked IE: sleeping in a bed to set a save point.
  • Amazing. Thank you for this great tutorial!
Sign In or Register to comment.