Howdy, Stranger!

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

More (if) headaches (sugarcube 2.11.0)

edited December 2016 in Help! with 2.0
I'm going to apologise beforehand because I know my lack of understanding in the area of <<ifs>> must be very frustrating, but they are the BANE of my life!!

And yes, I'm repeating myself yet AGAIN - for which I also apologise.

I'm trying to get a response in this passage where if a certain documentation has already been read the player is sent to a passage which says "You've already read this." and then sends them back to where they were. But, I also need a <<if visited() is 1>> in there too.

But when I test this by revisiting the passage WITHOUT have read the documentation, I still get sent to the 'You've already read this" message.

Here's my code:
<<if visited() is 1>>You open the drawers until you find one stacked with documentation. There's a pamphlet titled [[Deep Sleep and Its Affects on the Human Mind|deepsleep]], another called <<if visited ("luckdrug")>>[[Partialiterfortuna|luckdrug2]]<<else>> [[Partialiterfortuna|luckdrug]]<</if>>, and one on [[Handling Isolation in Deep Space.|deepspace]]

Return to [[room.|lab]]
<<else>>There's a pamphlet titled [[Deep Sleep and Its Affects on the Human Mind|deepsleep]], another called [[Partialiterfortuna|luckdrug2]], and one on [[Handling Isolation in Deep Space.|deepspace]]

Return to [[room.|lab]]<</if>>

Comments

  • Except for the invalid space character between the second visited function name and its related () brackets the code looks fine.

    What code are you using to send the player to the "You've already read this." passage and more importantly what code are you using to return the player back to the passage containing the above example?

    eg. If your above example is in a passage named "The Room" are you using code like either of the following to return from the passage containing the "You've already read this." message or are you doing something else?
    [[Return|The Room]]
    
    <<return>>
    
  • edited December 2016
    The room which give the 'Already read this' message is called luckdrug2 - which is where I send them if they've already been to luckdrug and read the documents.

    I return the player on a timer.
  • If I understand correctly then the following three passages should do what you ask:

    1. The passage that contains a modified version of your example, you did not state what this passage was called so I will name it search.
    <<if visited() is 1>>You open the drawers until you find one stacked with documentation. <</if>>\
    \
    There's a pamphlet titled [[Deep Sleep and Its Affects on the Human Mind|deepsleep]], another called \
    \
    <<if visited("luckdrug")>>[[Partialiterfortuna|luckdrug2]]\
    <<else>>[[Partialiterfortuna|luckdrug]]\
    <</if>>\
    \
    , and one on [[Handling Isolation in Deep Space.|deepspace]]
    
    Return to [[room.|lab]]
    

    note: I formatted the above example with extra line-breaks to make it easier to see the different conditional sections, then used the backslash character to remove the extra line-breaks in the output.

    2. The luckdrug passage, which contains some text about reading and a time based goto.
    You read the document titled Partialiterfortuna.\
    \
    <<timed 3s>><<goto "search">><</timed>>
    

    3. The luckdrug2 passage, which contains your "You've already read this." message and a time based goto.
    You've already read this.\
    \
    <<timed 3s>><<goto "search">><</timed>>
    
  • edited December 2016
    Works perfectly! Thank you very much.

    So that I can at least try and get my head around <<if>> macros, can I ask a few questions.

    In the main block of code (the first) you put a closing <</if>> at the end of the first sentence, and then the rest of it afterwards. Is this instead of using an <<else>> ?

    I always thought an <<if visited() is 1>> always had to be followed by an <<else>>, because in English what we're saying is If the player is visiting for the first time say [this], but if he's re-visiting say [this]

    I know there's an <<else>> in there, but that one's handling which passage they should be sent to.

    What I don't understand is how the game knows to read beyond the closing <</if>> for a first visit??
  • If Thens are just conditions the program is evaluating. if it's true like visited("PassageName") is 1>> (and it's true, then it will output THAT), otherwise, it will look at the other ones and see if they're even true. If not, the whole If/THen gets ignored except for the last condition which doesn't meet the other conditions.

    I dunno if that makes sense?
  • edited December 2016
    Not really, Gryphbear, but that's probably just me. Thank you all the same.
  • Jud_Casper wrote: »
    What I don't understand is how the game knows to read beyond the closing <</if>> for a first visit??
    By default, all the content within a passage is processed to determine what output is displayed to the reader.

    note: the first part of the following explanation ignores the fact you have a markup link that needs to conditional change which passage it targets. I will cover that issue in the second part.

    Part A. The output of your example can be basically broken down into two sections:

    1. The Static section:
    This section of text is displayed to the reader every time they visit this passage and it does not change. This means that you don't need to use an <<if>>, <<else-if>> or <<else>> macro to cause it to appear in the output.

    2. The Conditional section:
    This section of text is only displayed to the reader if the related condition is true, because there is no alternative text to display instead you don't need an <<else-if>> or <<else>> macro.

    A simplified example would look something like the following:
    <<if visited()>>This text is only included in the output if the condition is true<</if>>
    
    This text appears in the output every time this passage is visited, and it does not change.
    


    Part B. A link that changes it's target passage:

    In your example the Static section also include a markup link which conditionally targeted one of two different passages, there are two ways this could be handled.

    1. Conditionally change which markup link was added to the output:
    You used an <<if>> macro to output one of the possible markup links if the related condition was true, and you used an <<else>> macro to output the second if the related condition was false.

    Using this method required changing the Static section so that a part of it is now conditional.

    2. Use a (temporary) variable as the conditional markup link's target:
    Using this method does not require the Static section to change but it does add extra complexity between the Conditional section and the Static section.
    <<if visited() is 1>>You open the drawers until you find one stacked with documentation. <</if>>\
    \
    <<if visited("luckdrug")>><<set _target to "luckdrug2">><<else>><<set _target to "luckdrug">><</if>>\
    \
    There's a pamphlet titled [[Deep Sleep and Its Affects on the Human Mind|deepsleep]], another called [[Partialiterfortuna|_target]], and one on [[Handling Isolation in Deep Space.|deepspace]]
    
    Return to [[room.|lab]]
    
    warning: Due to a limitation of Twine 2.0.11 the above example will cause an invalid passage named _target to be automatically created, you will need to delete the _target passage.
  • I'll have to read that a few times, greyelf, but thank you.
Sign In or Register to comment.