Howdy, Stranger!

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

Creating If Else Statement

Hey!

I'm trying to use If Else statements to display particular passage text depending on the player's previous choices.

Passage C is reached by the player through Choice A or Choice B (this has a longer path).

I want to display certain text is the player reaches Passage C through Choice A and different text if reached through Choice B.

I've looked at tutorials but can't seem to figure out how to phrase my statement to make it work. I'm using Harlowe on Twine 2.

Comments

  • You need to state the name and full version number of the story format you are using, as answers can be different for each one. I will assume you are using v1.2.3

    Two of the ways you can do what you want are:

    1. Use a variable to track what option the reader selected.

    1a. Initialise the variable in your story's startup tag passage using a (set:) macro.
    (set: $path to "")
    


    1b. Update the variable to indicate with option/path was chosen.

    eg. In the Choice A passage:
    (set: $path to "A")
    
    ... and in the Choice B passage:
    (set: $path to "B")
    


    1c. Use an (If:) macro in your Passage C passage to test the current value stored in the variable.
    (if: $path is "A")[The text to show if Choice A was selected]\
    (else:)[The text to show if Choice B was selected]
    
    ... if there was a third (or more choices) you could also use an (else-if: ) macro.
    (if: $path is "A")[The text to show if Choice A was selected]\
    (else-if: $path is "B")[The text to show if Choice B was selected]\
    (else:)[The text some other choice was selected]
    


    2. Use the (history: ) macro to determine if a particular passage has been visited by the reader.
    eg. Assuming the two passages are named: Choice A and Choice B then you could use code like the following in Passage C
    (if: (history:) contains "Choice A")[The text to show if Choice A was selected]\
    (else:)[The text to show if Choice B was selected]
    

    warning: All of the above code was written without using the Twine 2 application and has not been tested, so it may include syntax errors.
  • Hi. If I read you right, you want to set a variable that shows whether the player has chosen Choice A or Choice B. Something like this:

    ::start
    [[Choice A]]
    [[Choice B]]
    

    ::Choice A
    You have chosen passage A! (set: $choiceA to true)
    
    [[Passage C]]
    

    ::Choice B
    You have chosen passage B! (set: $choiceB to true)
    
    [[Passage C]]
    

    ::Passage C
    (if: $choiceA is true)[You have been to passage A, I see!](elseif: $choiceB is true)[You have been to passage B, I see!]
    
  • edited April 2017
    Thanks guys for both your suggestions. I'm using Harlowe 1.1.1 with Twine 2.0.8.

    I'm still finding these variables a little confusing. I've tried implementing both your suggestions but it's still not performing the way I like - I don't think I've got my head around it.

    In Passage 1:
    Player gets a choice between [[Choice A | Passage where they proceed]]
    and [[Choice B | Passage with dire consequence]]
    

    If the Player chooses Choice A, they later get another Passage:
    Player gets a choice between [[Choice A | A different path]]
    and [[Choice B | Passage with dire consequence]]
    

    The player can get to this passage through two different branches so I'd like different text to appear in the "Passage with dire consequence" dependent if the Player got there through the original Choice B or the later choice.

    I don't really understand what code I'm supposed to put and where - am I using the passage tags or putting it all in the body of the passage? Thanks for your patience!
  • dontpanic wrote: »
    Player gets a choice between [[Choice A | Passage where they proceed]]
    and [[Choice B | Passage with dire consequence]]
    
    One quick note:
    Do not add white-space (eg. space characters) to either the start or end of the Target Passage Name of a markup based link, doing so causes the automatically created Passage to also have the same white-space in its name.
    eg.
    [[Link Text|Target Passage Name]]
    
    <!-- Bad markup based link -->
    [[Choice A | Passage where they proceed]]
    
    <!-- Good markup based link, note there is no space between | and P -->
    [[Choice A |Passage where they proceed]]
    
  • Thanks! I checked and I don't have white-space in the passage names, I must have accidentally added them in the post to make it it look less cramped.
  • edited June 2017
    Hi,

    I am trying to do something similar, but when I test the code it always goes to my "else" command, even if the variable is set to the "if". This is what my code looks like.
    (link: "away")[(if: $direction is "clockwise")(goto: "away.")][(else:)[(goto: "off.")]]
    

    Putting a slash between "if" and "else" caused that to show up with the story text. Thank you for any help.

    EDIT: putting "if" and "else" into the same hook causes it to always go to my "if"
  • You should space and indent your code to make it more readable, and use back slashes to get rid of the unwanted white space. If you did so, you'd probably notice you're missing some hooks, and other hooks are in weird places. Here's how your code should probably look:
    (link: 'away')[\
        (if: $direction is 'clockwise')[\
            (goto: 'away')\
        ](else:)[\
            (goto: 'off')\
        ]\
    ]
    

    Some notes.

    1. I changed the second (goto:)'s passage to 'off' because it's usually a terrible idea to include periods and certain other punctuation in passage names.

    2. You can place this entire code chunk inside braces ( {} ) to eliminate the new lines as well, instead of using all those back slashes.
Sign In or Register to comment.