Howdy, Stranger!

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

Newby: help with variables and 'else' command

Hello,
I'm just starting my first story: a translation and adaptation from an old gamebook.
I'm using Twine 2.0.5.

Really easy question (sorry): I'd like to know how to use the 'else' command.

Example:

In paragraph X I set a variable:
(set: $lightspell to "yes")
Since in that paragraph the player acquires the Light spell.

In a later paragraph I need to check whether the player has that spell or not, in order to give him/her the choice of using it.
So I would write:
(if: $lightspell is "yes")[Cast the Light spell-> to peer into this darkness.]

Here to my question:
I'd like to know if it's possible to display an alternative text whatever else the case is, so that I don't have to set all the variables to "no" at the beginning of the story.

I hope I've put it down clearly!

Thanks a lot.

Comments

  • I use sugarcube, so the syntax might be slightly different than if you're using Harlowe or Snowman

    But what I would write is

    <<set $lightspell = "yes">>

    then later

    <<if $lightspell = "yes">>
    PassageNameHere
    <<else>>
    You do not have a light spell.
    <<endif>>

    You're assigning the variable a value, and then checking to see if it has said value. It will display one thing if the conditions are met, and another AKA <<else>> if the conditions are not met. What's also important is to make sure you use <<endif>> otherwise it won't work. You gotta have an open and a close to make an if sandwich.

    As I said before, if you're using Harlowe, then the syntax will be a bit different than what I've provided, so make sure it's formatted properly as well.
  • You gotta have an open and a close to make an if sandwich.
    +50 points for using the phrase 'if sandwich'. Love it.
  • warning: There is a bug in the <<if>> part of @ryanindustries example, they incorrectly use a single equals sign = to compare the $lightspell variable with the "yes" value.

    A single equals sign is use for assignment, where as a double equals sign == should be used when doing a comparison. What @ryanindustries <<if>> example is actually doing is assigning the "yes" value to the $lightspell variable and then check if result of that assignment was true.

    note: This is a common mistake and one of the reasons using the to and is keywords is suggested.

    A corrected version of @ryanindustries SugarCube <<if>> example:
    <<if $lightspell == "yes">>
    [[Cast the light spell|PassageNameHere]]
    <<else>>
    You do not have a light spell.
    <<endif>>
    

    The Harlowe equivalent of @ryanindustries original example would be:
    (set: $lightspell to "yes")
    
    then later
    
    (if: $lightspell is "yes")[
    [[Cast the light spell|PassageNameHere]]
    ]
    (else:)[
    You do not have a light spell.
    ]
    
  • Thanks a million, made my day.
    I'm using harlowe, where can I find a 'dictionary' of its syntax, variables and so on? I've read a bit on twine2.neocities.org, but it felt a bit advanced and incomplete, meaning the basics weren't there.
    Thanks, really grateful.
  • That website is the documentation for Harlowe.

    Did you have a close look at the image in the "Syntax comparison to Twine 1" section at the start?
  • Yes, unfortunately it's still a bit obscure to me without a column with the command explanation. I'm really new to all this. I guess I'll experiment, wait for future guides and ask here if I'm really stuck.
    This program is great, I really hope a comprehensive guide will come. Maybe in twine, why not?
    Anyway, my immediate problem is solved, thanks.
  • greyelf wrote: »
    warning: There is a bug in the <<if>> part of @ryanindustries example, they incorrectly use a single equals sign = to compare the $lightspell variable with the "yes" value.

    A single equals sign is use for assignment, where as a double equals sign == should be used when doing a comparison. What @ryanindustries <<if>> example is actually doing is assigning the "yes" value to the $lightspell variable and then check if result of that assignment was true.

    note: This is a common mistake and one of the reasons using the to and is keywords is suggested.

    A corrected version of @ryanindustries SugarCube <<if>> example:
    <<if $lightspell == "yes">>
    [[Cast the light spell|PassageNameHere]]
    <<else>>
    You do not have a light spell.
    <<endif>>
    

    The Harlowe equivalent of @ryanindustries original example would be:
    (set: $lightspell to "yes")
    
    then later
    
    (if: $lightspell is "yes")[
    [[Cast the light spell|PassageNameHere]]
    ]
    (else:)[
    You do not have a light spell.
    ]
    

    I'd forgotten about that, I sorta rambled what I knew off of the top of my head. Glad you cleared that up!

    Normally, instead of "yes" or "no", I just set a value as a 1 or a 0 when nothing more elaborate is necessary. Then I can use all kinds of words like "if $value gte 1" or "if $value lte 1" or "if $value eq 1" to indicate what I want to happen from the given information. Some instances it would be confusing practice if all you could do was numbers and no words, but in all the code I've done I've had very rare cases where it wasn't simply a yes/no answer, which easily translates to 1/0.
Sign In or Register to comment.