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
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. 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.
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.