'save code' in Harlowe 3.1.0

Twine Version: 2.3.2
Story Format: Harlowe 3.1.0

I am writing a second chapter to a story (each chapter is a separate Twine game), and my idea is to have the events that happened in the first chapter influence the second.

There are about 6 boolean variables that I want to track. My idea is to generate a ‘save code’ keyword representing the state of these variables that is provided to the player after the end of the first chapter and which can be entered at the start of the second chapter to initialise the game state.

My first idea was to code a whole set of nested if statements, but 6 boolean variables leads to 64 possible combinations, and I’m note sure how to handle these as they look very messy when nested.

Is there a better way?

The following is one method you could use. It assumes the Boolean variables are named $boolean1 to $boolean6 but obviously you could name the variables whatever you like.

  1. Generate the “code” to display to the Reader.
{
(set: $code to "")
(if: $boolean1)[(set: $code to it + "1")](else:)[(set: $code to it + "0")]
(if: $boolean2)[(set: $code to it + "1")](else:)[(set: $code to it + "0")]
(if: $boolean3)[(set: $code to it + "1")](else:)[(set: $code to it + "0")]
(if: $boolean4)[(set: $code to it + "1")](else:)[(set: $code to it + "0")]
(if: $boolean5)[(set: $code to it + "1")](else:)[(set: $code to it + "0")]
(if: $boolean6)[(set: $code to it + "1")](else:)[(set: $code to it + "0")]
(print: '<input type="text" id="code" name="code" size="6" value="' + $code + '" readonly>')
}
  1. Obtain a code from the Reader then determine what each Boolean variable’s value is…
{
(set: $boolean1 to false, $boolean2 to false, $boolean3 to false)
(set: $boolean4 to false, $boolean5 to false, $boolean6 to false)
(set: $code to "000000")
(set: $code to (prompt: "Enter Code", $code))
(if: $code's length is 6)[
	(if: $code's 1st is "1")[(set: $boolean1 to true)]
	(if: $code's 2nd is "1")[(set: $boolean2 to true)]
	(if: $code's 3rd is "1")[(set: $boolean3 to true)]
	(if: $code's 4th is "1")[(set: $boolean4 to true)]
	(if: $code's 5th is "1")[(set: $boolean5 to true)]
	(if: $code's 6th is "1")[(set: $boolean6 to true)]
]
(else:)[Code is wrong length]
}

Note: When generating the “code” you don’t need to use String values as simple as “0” and “1”, you could make the Strings harder to guess like so…

{
(set: $code to "")
(if: $boolean1)[(set: $code to it + "A")](else:)[(set: $code to it + "M")]
(if: $boolean2)[(set: $code to it + "D")](else:)[(set: $code to it + "F")]
(if: $boolean3)[(set: $code to it + "Z")](else:)[(set: $code to it + "P")]
(if: $boolean4)[(set: $code to it + "E")](else:)[(set: $code to it + "R")]
(if: $boolean5)[(set: $code to it + "L")](else:)[(set: $code to it + "G")]
(if: $boolean6)[(set: $code to it + "W")](else:)[(set: $code to it + "I")]
(print: '<input type="text" id="code" name="code" size="6" value="' + $code + '" readonly>')
}

…as long as you change the (if:) macros in the 2nd code example to check for the same String values to used to build the “code”…

1 Like