Howdy, Stranger!

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

How do I keep count of something?

Hi there! I'm a teacher trying to teach Twine with a bunch of enthusiastic primary-aged writers. They want to know how to do all kinds of things and I am doing my best to stay one step ahead of them. At the moment I am trying to figure out how I can keep count of something, like the number of keys collected.

At the moment I have (if: $key is 3) as a conditional to show the text that allows the reader to open the door. I have (set: $key is '0') and the beginning of my story.

What do I use to increase the count of $key by a given value? Something like (set: $key +1)?

Sorry if this question has been asked and answered elsewhere. I didn't exactly know what to search for. Thanks for your assistance! :)


  • You should write, what version and storyformat you are using, but generally this should answer all your questions.
  • Basically, just go with
    set $key to $key + 1
    The structure of doing that is dependent on your story format (harlow, sugarcube, etc) but that is the general method in Twine.
  • As stated by @idling you need to supply both the name and full version number of of the Story Format you are using, as answers can be different for each one. Based on the syntax of your examples I will assume you are using Harlowe, I will also assume you are using the latest default which is Harlowe v1.2.4 in Twine 2.1.3

    There are different primitive (Javascript) data types which can be used for values in any of the story formats, in your example you are assigning a String representation of the number zero instead of an actual numeric representation.

    You are also using the wrong operator (is) to do an assignment, the Example usage section of the (set: ) macro documentation shows it using the to operator.

    After changing both the data type and operator your initialisation example would look like the following.
    (set: $key to 0)
    ... notice the lack of single/double quotes and the replacement of is with to, once you make those changes you will be able use the value stored in $key as a number.

    The Details: section of the (set: ) macro documentation includes two examples of how to increment the current value stored within a variable by one.
    (set: $key to it + 1)
    (set: $key to $key + 1)
  • Read the following:

    and do the example provided at the end. It shows how to count things.
Sign In or Register to comment.