About "Save" System

@Mert, I’ve tried a few different things regarding saving the game so that I can close the browser and come back to the game later. It’s not an exact science. I can implement a save system, but the restoration of the game typically only restores the game to the beginning of a scene. Restoring to the middle of a scene should be possible, but I can never get it to work the way I really want it to. For that, I usually have to resort to the built-in “password” system.

For your purposes, the “password” system built into ChoiceScript should work decently well if you set it up correctly, as I’ll describe forthwith. To use the password system, you first have to add “restore_password” as a valid ChoiceScript command. There are several ways to do this, but one of the easiest ways that doesn’t involve editing any of the JavaScript files is to modify your index.html file itself. To do this, open your index.html file (the one in the “web/mygame” folder) in your favorite text editor. Look for the line that reads <script src="../navigator.js"></script> and under that line, enter the following code:

<script>
Scene.validCommands["restore_password"] = 1;
</script>

This now allows you to use the \*show_password and \*restore_password commands (the \*show_password command is already enabled by default). To make use of these commands effectively, you have to call \*show_password in a scene at the place where you want to save your game. Where you put this call is up to you, but I would suggest that when you use it, you use the following block of code for it:

*show_password
<em>Some text here that reminds you where you are in the game.</em>
*page_break

Wherever you put the above block of code, when you get to that part of the scene, you’ll see a huge text area with a bunch of gibberish inside. The gibberish is the “password” for your saved game. Copy the password and save it to a file, because you will use that same password later to get back to that place in the game. You can put the above block of code in several places, which allows you to save off any number of passwords, each representing a different place in the game. So, figure out where you want to be able to save and restore, and that’s where you place the above block of code in your scene files.

Now, to be able to restore your game using one of these passwords, you will need to make use of the \*restore_password command. You can put this command wherever you like, but the one place that makes the most sense is right at the beginning of your first scene, so that when you start up your game the next day, you can jump directly to where you left off. Here is the ChoiceScript code I’d recommend that you place at the beginning of your first scene for this:

Do you want to restore a game?
*choice
  #No
    *goto regular_start
  #Yes
    *restore_password
    *goto regular_start
*label regular_start

Following that, you have your regular game code.

Now, when you start up the game and you have a saved password you want to use to restore your game, you’ll select Yes as your first choice, after which you’ll be presented with another huge text area into which you will paste one of your saved passwords. You’ll then click Next, and your saved game will be restored. Or you can click Cancel and the game will start up regularly.

A couple of warnings: When you edit a scene file, you could be invalidating any existing passwords that apply to that scene file. Also, if you modify mygame.js, you could be invalidating all of your existing saved passwords.

Basically, your standard mode of operation should be to delete all of your old passwords and start your game over from the beginning after you make any changes to your game. You might be okay to use an old password after certain changes, but it’s just safer to start over with a clean slate after you make any changes.

Hope this helps.

1 Like