Preserving Variable State Across Restart / Restore

Greetings all! I’m having trouble figuring out how to do something. I’m trying to have a case where the player can be sent back in time. But this will mean essentially a “restart” of the game. Some dialogue, however, will indicate that they did, in fact, have a “world reset.” So I need to preserve a state that says such a reset happened.

Here’s what I’m trying. (Incredibly minimal experiment.)

!% -G
!% +include_path=../../inform6lib

Constant Story "Learning";
Constant Headline "^An Interactive Learning Example^";

Include "Parser";
Include "VerbLib";

Constant RESET 1;

Array time_travel_state -> 2;

[ Initialise;
  print time_travel_state -> 1;
  TimeTravelOccurred();

  if (time_travel_state -> RESET == false) {
    print "Time travel has not occurred.";
  } else {
    print "Time travel has occurred.";
  }
];

[ TimeTravelOccurred;
  @protect time_travel_state 1;
];

Include "Grammar";

[ MyResetSub;
  time_travel_state -> RESET = true;
  TimeTravelOccurred();
  print time_travel_state -> 1;
];

Extend "version" replace
* -> MyReset;

end;

For this simple example, if someone types “VERSION”, I’ve set up a replace function that will set the variable in question to true. Then I have a TimeTravelOccurred state that will protect that variable.

What I’m doing definitely doesn’t work. Meaning that even if I just do a restart, nothing actually gets protected. I’m also going to clearly need this to persist across save/restore, but if my small experiment on restart isn’t workable, there’s probably not much point in complicating the example.

Does what I’m doing here seem logical at all?

Note: This is for Glux. I didn’t know, and had to google @protect.

You ask to protect one byte of memory, and then you only access the second byte from the start of the array. So no, it’s not protected.

AH! So simple and you are correct indeed. For some reason, I was reading @protect as protecting the one part I wanted, not the whole thing. Which was a silly oversight on my part. So I changed my statement in TimeTravelOccurred to:

@protect time_travel_state 2;

That did the trick! Much thanks. So that works if there’s a restart in game. Obviously if a game starts up totally new, the default is retained, which makes sense, and is desired. So now I just have to make sure to handle a restore action.

1 Like