0 votes
by (350 points)
Hello, I am trying to implement game saving and loading and I used these scripts:

To load a game:

{(if: $Saves contains "Slot A")[
  (link: "Load game")[(load-game:"Slot A")]

[[Start new game->story]]


To save a game:

{(link:"Save game")[
  (if:(save-game:"Slot A"))[
    Game saved!
  ](else: )[
    Sorry, I couldn't save your game.


The thing is, that this works - the games save and load when I click on the buttons. But despite working, I get this error message near the "Load game"  link: the number 0 cannot contain any values, let alone the string "Slot A"►

How can I fix that? Thanks.

1 Answer

+1 vote
by (63.1k points)
selected by
Best answer

You should be using the (saved-games:) macro to test if a saved game exists. You're testing against a variable called $Saves. If you didn't previously set this variable to (saved-games:) then that would cause the error. 

You almost certainly didn't declare the $Saves variable anywhere, since undeclared variables in harlowe are treated as the number 0, hence the error message. 

Example using the (saved-games:) macro: 
(if: (saved-games:) contains "Slot A")[..,

Example using a variable: 
(set: $Saves to (saved-games:). 
(if: $Saves contains "Slot A")[...

Note. If you use the latter method, you'll need to include the (set:) just before the check as shown; since harlowe doesn't pass objects by reference, but rather by copying/cloning them, setting the variable once won't work because changes to the save data won't be available to the variable. 

All in all, you probably should go with the first method. 

by (350 points)
edited by
I tried both options, and it seems to work, but instead of loading the game on clicking the "load game" button it loads it as soon as the passage is opened


Edit: It works now! Thanks!
by (159k points)

If you plan on 'using the variable' example then I strongly suggest you change the variable from a story variable ( $Saves ) to a temporary variable ( _Saves ), otherwise the value of the $Saves variable will be included within the next Save and that would be a meaningless waste of storage space.

Example using a temporary variable so it's value doesn't get included within the next Save:
(set: _Saves to (saved-games:). 
(if: _Saves contains "Slot A")[...