Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Cycling links in Harlowe - variables not set

edited March 2016 in Help! with 2.0
I'm currently using furkle's cycling link macro for Harlowe in Twine 2.0.11 and Harlowe 1.2.2 (on desktop). I have the below code in a passage:
<tw-link class = 'cyclingLink' data-cycling-texts='["Dark", "Red", "Blonde", "Light"]' onclick='clickCyclingLink(this, "$haircolour");'>$haircolour</tw-link> hair frames your face.

and there are two problems - first, when I test play it, $haircolour is initially set to 0 instead of "Dark". How can I get the variable to be set to the first item in the list? Second, I realised that when I click through the cycling link, $haircolour isn't set to a new variable each time, it just remains as 0. Any ideas how I might go about fixing this?

Comments

  • First. It's not a macro. No, this isn't pedantry. It really is not a macro, so calling it one is unhelpful.

    Second. At the risk of sounding like a jerk, reading the examples and documentation on the page you linked, or reading them better, probably would have helped you here. The example from that page shows how to initially set the value of your $variable and the Passage Usage section even explains why you must do so.
    The TwineScript macro (set: $myVar to "One") is necessary because without it $myVar will not be set programmatically by the cycling link if the user leaves the page without having cycled the link at least once.

    In other words, your code should look something like the following:
    (set: $haircolour to "Dark")<tw-link class='cyclingLink' data-cycling-texts='["Dark", "Red", "Blonde", "Light"]' onclick='clickCyclingLink(this, "$haircolour");'>$haircolour</tw-link> hair frames your face.
    

    As to your $haircolour variable not being changed by the cycling link construct. How are you checking? If you're trying some static test on the same page, then that's never going to work as the construct is dynamic (i.e. it changes the value of $haircolour after the static page elements have been rendered). You'd need to either move to a new passage or use a dynamic check (e.g. using the (live:) macro).

    I'm attaching a Twine 2 archive of a working demo.
  • *digs hole*
    *hides in hole*

    I... probably should have realised that my checks weren't dynamic. (And so on.) Thanks so much!
  • Is there a way to adapt this so that the link that players see is different from the name of the variable that is set for it?
  • The link does not show the variable name, it shows its current value—having the link show the variable name itself would be somewhat pointless.

    If you don't want the cycling link to display the variable's current value, then simply don't use the cycling link code. You can cycle through a list easily enough with the built-ins.

    For example, assume that you've done something like the following someplace earlier:
    (set: $hairColorList to (a: "Dark", "Red", "Blonde", "Light"))
    (set: $hairColor to $hairColorList's 1st)
    
    A link which will cycle the array and assign its current 1st value to the given variable might look like the following:
    (link-repeat: "Cycle Hair Colors")[{(set: $hairColorList to (rotated: -1, ...$hairColorList))(set: $hairColor to $hairColorList's 1st)}]
    
Sign In or Register to comment.