Howdy, Stranger!

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

(Harlowe) If/else cycle links

I'm trying to create cycle links that are dependent on previously chosen variables.

Code right now is a scramble between if/else statements and cyclingLink code, but basically I want it to do the follow:

Page 1: set $food to cycle "fruit" or "vegetable" --> I can do this much

Page 2: if $food is "vegetable," set $favorite to cycle "carrots", "broccoli", "potatoes"
else set $favorite to cycle "apples", "oranges", "grapes"

Comments

  • You don't state which method you are using to implement a cycle link, so I am going to assume that you are using Furkle's Cycling Link addon.

    One possible solution is to use a String variable to store which favorite's link you want to use and then to use a (print:) macro to show the link.

    note: Because the cycle link code contains string values internally I am going to use both single and double quotes to delimit strings, I will also need to escape some of the single quotes using a backslash \ character.

    The following solution consists of three passages.

    1. First Passage:
    {(set: $food to "fruit")
    
    cycle link 1: <tw-link class='cyclingLink' data-cycling-texts='["fruit", "vegetable"]' onclick='clickCyclingLink(this, "$food");'>$food</tw-link>.}
    
    [[Second Passage]]
    
    2. Second Passage:
    note: I have used indents and line-breaks to make the following more readable, these can be safely removed.
    {(if: $food is "vegetable")[
    	(set: $favorite to "carrots")
    	(set: $link to '<tw-link class="cyclingLink" data-cycling-texts=\'["carrots", "broccoli", "potatoes"]\' onclick=\'clickCyclingLink(this, "$favorite");\'>$favorite</tw-link>')
    ]
    (else:)[
    	(set: $favorite to "apples")
    	(set: $link to '<tw-link class="cyclingLink" data-cycling-texts=\'["apples", "oranges", "grapes"]\' onclick=\'clickCyclingLink(this, "$favorite");\'>$favorite</tw-link>')
    ]
    
    cycle link 2: (print: $link)}
    
    [[Third Passage]]
    
    3. Third Passage:
    food: $food
    favorite: $favorite
    
  • I keep getting a 0 on the second passage, but after I click the 0, it cycles just fine.
  • I keep getting a 0 on the second passage, but after I click the 0, it cycles just fine.
    A zero generally means that the variable being shown has not been initialized, which I believe on the second page would be the $favorite variable.

    You did cut-n-pasted the contents of the each of the three passage examples into your story without editing the contents?

    I have attached a Twine archive which contains the story I used for creating and testing the solution. Use Twine's Import From File option to view it.
  • Hmm. When I used your file, everything works exactly as it should. No idea how I screwed up your initial code.

    Thanks so much for the help!
Sign In or Register to comment.