Help with showing chosen value of variables throughout whole story

Hi I am a beginner, I’ve watched a few videos and referenced to forums but my variable value which the reader chooses at the beginning passage will revert to zero in other passages. I wonder why is that the case?

Twine Version: 2.5.1 (website)
Story Format: Harlowe

To give some context, I’m making an rpg with selectable character settings which I would like to appear on other passages as well.

The beginning passage, titled Welcome contains this code:

Character Creation: (text-colour: blue)[$name]
Gender: (text-colour: blue)[(set: $gender to (array: "male", "female","non-binary"))(set: $g to 1)|gender>[(print: $gender's ($g))] (display: "gender")]
Hair length: (text-colour: blue)[(set: $hair_length to (array: "long", "short", "medium"))(set: $hl to 1)|hair_length>[(print: $hair_length's ($hl))] (display: "hair length")] Hair colour: (text-colour: blue)[(set: $hair_colour to (array: "black", "brown", "blonde","platinum","auburn","pink","red","lilac","indigo","white","grey","silver","green","midnight blue","pale blue"))(set: $hc to 1)|hair_colour>[(print: $hair_colour's ($hc))] (display: "hair colour")]
Eye colour: (text-colour: blue)[(set: $eye_colour to (array: "black", "brown", "gold","red","purple"," blue","silver","grey","green","amber","pink"))(set: $eye to 1)|eye_colour>[(print: $eye_colour's ($eye))] (display: "eye colour")]
Skin tone: (text-colour: blue)[(set: $skin_tone to (array: "fair", "beige", "tanned","golden","olive","sepia"))(set: $skin to 1)|skin_tone>[(print: $skin_tone's ($skin))] (display: "skin")]
Height: (text-colour: blue)[(set: $height to (array: "average","tall","short"))(set: $h to 1)|height>[(print: $height's ($h))] (display: "height")]

Select where you'd like  to be transported to:

[[School of Magic->School Prologue]]
[[Medieval World->Medieval Prologue]]
[[Survival Novel->Survival Prologue]]

The unlinked passage contains this code, because I wanted readers to choose which gender, hair colour, eye colour, etc, by toggling between the options:

(link-repeat: "> ")[{
	(if: $g is $gender's length)[(set: $g to 1)]
	(else:)[(set: $g to $g + 1)]
	(replace: ?gender)[(print: $gender's ($g))]
}]

Then when I (print: $gender) on another passage, it just shows zero.
I hope a kind soul can advise me on how to write a code that fixes the value that the reader chose throughout entire story’s variables. Thank you!

As you initialised the $gender variable to be an Array containing three String literals (eg. “male”, “female”,“non-binary”) then that’s what should be displayed when you call (print: $gender), the fact that it outputs a zero indicates that the $gender variable has not been initialised before that (print:) macro call.

Are you sure that that (print: $gender) macro call occurs after the Welcome Passage was visited, or that you aren’t using the Test From Here option to view the Passage that contains the (print: $gender) macro call

Regarding variable initialisation, ideally that should be done within your project’s startup tagged Passage.

If your project doesn’t have such a Passage yet then you should create a new Passage, named whatever you like (eg. Startup), and assign a startup Passage Tag to it. Then you would move the variable initialisation being done in your Welcome Passage to this new tagged Passage, it would end up looking something like the following…

(set: $gender to (array: "male", "female","non-binary"))
(set: $g to 1)
(set: $hair_length to (array: "long", "short", "medium"))
(set: $hl to 1)
(set: $hair_colour to (array: "black", "brown", "blonde","platinum","auburn","pink","red","lilac","indigo","white","grey","silver","green","midnight blue","pale blue"))
(set: $hc to 1)
(set: $eye_colour to (array: "black", "brown", "gold","red","purple"," blue","silver","grey","green","amber","pink"))
(set: $eye to 1)
(set: $skin_tone to (array: "fair", "beige", "tanned","golden","olive","sepia"))
(set: $skin to 1)
(set: $height to (array: "average","tall","short"))
(set: $h to 1)

The above code would then be automatically executed before the initial Passage of your project is shown to the end-user, and also before any Passage targeted by the Test From Here option is shown.

Doing the above would allow you to simplify the code for each choice to something like the following…

Gender: (text-colour: blue)[{
	|gender>[(print: $gender's ($g))]
	(display: "gender")
}]

note: I have added Collapsing whitespace markup to your own example so I could reformat it a little to help improve the readability it.

You could also simplify the link code within the child Passages (eg. “gender”, “hair length”, etc…) by using the (rerun:) macro instead of the (replace:) macro…

(link-repeat: "> ")[{
	(set: $g to $g + 1)
	(if: $g > $gender's length)[
		(set: $g to 1)
	]
	(rerun: ?gender)
}]

And while the technique of using child Passages as a means to store code that you want to execute repeatably (using the (display:) macro) is a good way to add code modularity to a project, I don’t see such a use-case occurring here. So you may want to move those (link-repeat:) macros into the Welcome Passage itself…

Gender: (text-colour: blue)[{
	|gender>[(print: $gender's ($g))]
	(link-repeat: "> ")[
		(set: $g to $g + 1)
		(if: $g > $gender's length)[
			(set: $g to 1)
		]
		(rerun: ?gender)
	]
}]

note:
I’m not sure if you are aware that Harlowe includes a (cycling-link:) macro that works somewhat similarly to your own (link-repeat:) base code. The following example shows a repeating link that can be selected, and the currently displayed link text is stored within the $gender variable.

Gender: (cycling-link: bind $gender, "male", "female", "non-binary")

There is also the (dropdown:) macro that allows selection from a list, however the default styling the list of items can result in them being unreadable.

1 Like