Howdy, Stranger!

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

How can I check a variable for a string value?

edited March 2016 in Help! with 2.0
Hi,
I'm using Harlowe in Twine 2.

I've set a value to a variable in a specific passage and I want an easy way for another passage to check if the variable has been set. If it has, it returns one thing, if not, it returns something else.

As an example, here is code I've used to set the variable:
(link: "Choice one: cowboy hat")[(set: $hat to "cowboy hat")(goto: "next passage")]
(link: "Choice two: fedora")[(set: $hat to "fedora")(goto: "next passage")]

In the next passage I can do this:
(if: $hat is "fedora")[Here's your $hat.](elseif: $hat is "cowboy hat")[Here's your $hat.](else:)[... Oh! You don't have a hat.]

But I'm thinking there may be a shortcut that can check if $hat has a value and print out one thing, or if it's null, print out another. Instead of the above where I'm coding the same thing for each choice.

I did a search and couldn't find what I'm looking for. Thanks for the help!

Edit: Oops, first post. Should have picked "Ask a question." I don't see an option to delete this or change it.

Comments

  • It is generally a good idea to initialize (assign a default value to) a variable before you check/use it, and in your case doing this can help you know if it's value has been changed or not.

    One place initializing of variables in Harlowe is generally done in a startup tagged passage which gets processed just before the first passage of your story is shown. You can also initialize a variable earlier in the same passage (or a previous passage to that) you plan check/use the variable in. Add the following to your startup tagged passage, it assigns an empty string to the $hat variable.
    (set: $hat to "")
    
    note: If you don't have a startup tagged passage yet then create a new passage (the name is not important but I name mine Startup) and add a startup tag (all lower case letters) to it.

    The (else:) macro in your "next passage" example will now work, and you can also now use any of the following to test if the $hat variable has been changed since it was initialized.
    (if: $hat is "")[The hat variable has not been changed]
    
    (if: $hat is not "")[The hat variable has been changed]
    (if: not ($hat is ""))[The hat variable has been changed]
    (if: $hat !== "")[The hat variable has been changed]
    
  • Thanks that helps. I'll explain how I used your response below in case anyone else is having a simular issue.

    I created a startup passage like you suggested and put this in it:
    (set: $hat to "")

    Then on the passage I wanted to execute the variable, I wrote this:
    (if: $hat is "")[... Oh! You don't have a hat.](else:)[Here's your $hat.]

    That accomplishes what my goal was. Which was to have lean code that outputs a variable with multiple possibilities, but accounts for a possible null value.
Sign In or Register to comment.