How to make a cycle set a variable?

In my current project (using Sugarcube2/Twine v2.3.16), I have a cycle in a passage that determines whether the MC is the brother or sister of the character conversing with them. Obviously with this I’m wanting it to define whether the MC is a male or female, and set the variable accordingly at the same time, though I haven’t figured out how to do it in the same step. Here is what I have so far. First, the cycle itself (which is in the middle of some text):

<span id="cycle"><<cycle "$genderselect" autoselect>>\
<<option "sister">>
<<option "brother">>

Then, at the top of the next page:

<<if $genderselect is "sister">><<set $gender to "woman">>
<<else>><<set $gender to "man">>

As mentioned, what I’d like is to be able to combine both steps into one - making it that when you click on one it will set that variable with a value, and if you click it again it will replace that variable with its new value. OR, is it possible to have an external passage (like StoryInit) that I can put this code into and will actively track and set/change variables linked to the different passages (say, with the span id), so that the story passage itself isn’t cluttered with code?

1 Like


What you’re describing sounds like a live change, yeah? Basically changing the stuff in real time, without passage transitions? If so, you need this custom macro:

If you have further questions, ask!

Bye now!

1 Like

Welcome, Zedire !

It sounds like you’re only trying to detemine the gender of the main character. Thus you don’t need two story variables to do so. In every case you use a variable only to determine another variable you may wish to use a temporaray variable instead.

Anyway, in this case, you can probably do the trick with only one variable. According to documentation, you can give a label and a value inside each <<option>>.

Here’s what it could like:

<span id="cycle"><<cycle "$gender" autoselect>>\
<<option "sister" "woman">>
<<option "brother" "man">>

The text will show alternatively ‘sister’ and ‘brother’, but the variable will be filled by ‘woman’ or ‘man’

1 Like

While not exactly what I meant, I used something similar to this (live changing of text on the same page, if I understood correctly?) in another of my projects, but it required more coding in the passage, so this could be a better alternative. Thanks for the suggestion!

This was exactly what I was after! I didn’t know a way to set the variable within the cycle to a value that was different to the label in the passage itself, hence why I used two variables. lol. Anyway, thanks so much for the help!