Howdy, Stranger!

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

Complicated if-else statement

Hey there, I'm pretty new to twine and I had a question on whether or not it's possible to create an if-else statement that takes into account some rather complicated variables or if I should find a workaround.

From what I can tell it's not possible to include multiple if's or else's within the same if-else statement, so I thought I'd try making two different ones. At it's most basic, it looks like this:

<<if $seen_2 eq "yes">>Text description if you've seen passage 2
<<else>>Text description if you haven't seen passage 2

However, I'd like to make it so that if you haven't seen passage two but have seen at least one of two other passages that it will display different text. So I tried inserting another if-else statement, but I can't seem to get any sort of 'unless' statements to work. It looks like this:

<<if $seen_4 or $seen_5 eq "yes" unless $seen_2 eq "yes">>Text description if you've read 4 or 5 but not passage 2
<<else>>Text description if you haven't read passage 2 but have seen 4 or 5

If anyone knows the correct syntax to get this all to work in one passage then I would be very grateful. Otherwise I might see about getting it to lead to one other passage so I can do what I want.


  • edited May 2017
    We need to know which story format you're using and it's version. I can tell by the syntax it's SugarCube, and I'll assume you're using 2.18.0.

    The statement
    <<if $seen_4 or $seen_5 eq "yes" unless $seen_2 eq "yes">>

    has a few problems.

    1. It's not checking if $seen_4 equals "yes", it's checking to see if $seen_4 is Boolean true.

    2. 'Unless' isn't a valid TwineScript operator, though it is a Harlowe macro.

    3. You could/should use Booleans to check for binary values instead of the string "yes". It's just generally safer and easier to understand.

    So for your code, no work around is required, but it'd probably be best to change a few things.

    Something like this should work if you have to use the strings:
    <<if $seen_2 neq 'yes' and $seen_4 eq 'yes' or $seen_5 eq 'yes'>>

    Something like this is better:
    <<if !$seen_2 and $seen_4 or $seen_5>>
    /% ! is the negation operator, it flips true to false and false to true %/
  • Also when combining the or and and joins in a single complex expression it is generally a good idea to use parenthesis () to control the order the and/or sets are processed in, even though they are generally processed from left to right.

    eg. The following two complex expressions are not equal.
    <if $seen_2 neq 'yes' and $seen_4 eq 'yes' or $seen_5 eq 'yes'>>
    <if $seen_4 eq 'yes' or $seen_5 eq 'yes' and $seen_2 neq 'yes'>>
    ... because in the first example the $seen_2 neq 'yes' and $seen_4 eq 'yes' part is (generally) evaluated first, then the Boolean result of it is or'ed with the $seen_5 eq 'yes' part. And in the second example the or part is evaluated before the and

    Where as the following two complex expressions are equal.
    <if $seen_2 neq 'yes' and ($seen_4 eq 'yes' or $seen_5 eq 'yes')>>
    <if ($seen_4 eq 'yes' or $seen_5 eq 'yes') and $seen_2 neq 'yes'>>
    ... because the part within the parenthesis is always evaluated before the part that is not.
Sign In or Register to comment.