Howdy, Stranger!

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

Trouble using variables inside Furkle's cycling links add-on

Hi there,

I've been using Furkle's cycling links add-on in Harlowe 1.2.2 and it was working great until I tried using a $variable within the cycling text. The main character's name is set at the start of the game ($firstname) and I wanted to be able to refer to what they chose within the options which you can cycle through. However, when I attempt this it just displays '$firstname', rather than 'Jimmy' or whatever, and also won't cycle to the third option; it only displays the first two options.

I'm not great with code so it's probably completely obvious why javascript(?) can't access harlowe variables, but it would be great if someone could help me make it work!

Here's the actual code I'm using currently:
(set: $robots to "They were each in their own little padded slot. There was something about the way they fitted so snugly which really appealed to $firstname; it was probably why they bought this particular box of robots.")<tw-link class='cyclingLink' data-cycling-texts='["They were each in their own little padded slot. There was something about the way they fitted so snugly which really appealed to $firstname it was probably why they bought this particular box of robots.", "&#39;Good afternoon, boys and girls,&#39; $firstname said absent-mindedly as they waited for the little robots to power up. They said it quietly, to themselves, in the way you might say hello to a magpie.", "$firstname tapped their fingertips on the metal casing as they waited for the robots to boot. They wondered whether they were actually loading up, or if it was just a ploy to make you think there was more in there to load."]' onclick='clickCyclingLink(this, "$robots");'>$robots</tw-link>

Comments

  • As you have found out the story formats do not substitute a variable name with its value if that variable is within a String literal. eg. "$variable*

    You will need to create that String via concatenation as follows:
    (set: $robots to "They were each in their own little padded slot. There was something about the way they fitted so snugly which really appealed to " + $firstname + "; it was probably why they bought this particular box of robots.")
    
  • Hi greyelf!

    Thanks for taking the time to help out.

    Within the first (set:) passage the variable works just fine, so the first link shown before you click it has the character's name displayed, rather than just '$firstname'. It's after you click & it uses the text within the
    <tw-link class='cyclingLink' data-cycling-texts='[ ... ]' onclick='clickCyclingLink(this, "$robots");'>$robots</tw-link>
    

    section that it doesn't recognise the variable as anything but plain text. I tried using your concatenation there & it give me an alert box error ('Sorry to interrupt, but this page's code has got itself in a mess. SyntaxError: Unexpected token +'), which I guess was to be expected.

    Like I say, I don't know much about coding in any language, so I might be barking up the wrong tree here, but is there a way to make the javascript(?) '<tw-link>' section recognise, or use the info stored in, the twinescript(?) variables?

    Again, thanks for your time & any help would be greatly appreciated!
  • That one is a little more complex:

    1. Convert the markup for the tw-link into a String literal by wrapping it in quotes, I will be using double quotes.

    2. Use the same concatenation technique to append the value of $firstname in each location it is needed.

    3. Carefully escape (using as backslash) out all instances of the quotes used in step one (double quotes in the example) that were in the original tw-link markup, you need to do this because you need quotes to represent three different things in the String literal and you only have two type of quotes.
    eg. The delimiter of the overall String literal; the delimiter of the tw-link element's property values; the delimiter of each element in the data-cycling-texts property array.

    4. Use a (print:) macro to dynamically convert the String literal into markup again.

    The end result will look something like the following:
    (print: "<tw-link class='cyclingLink' data-cycling-texts='[\"They were each in their own little padded slot. There was something about the way they fitted so snugly which really appealed to " + $firstname + " it was probably why they bought this particular box of robots.\", \"&#39;Good afternoon, boys and girls,&#39; " + $firstname + " said absent-mindedly as they waited for the little robots to power up. They said it quietly, to themselves, in the way you might say hello to a magpie.\", \"" + $firstname + " tapped their fingertips on the metal casing as they waited for the robots to boot. They wondered whether they were actually loading up, or if it was just a ploy to make you think there was more in there to load.\"]' onclick='clickCyclingLink(this, \"$robots\");'>$robots</tw-link>")
    
  • Ahh amazing it works perfectly!

    Thank you so much for your time & for sharing your expertise! :smiley:
Sign In or Register to comment.