Counting how many times one visits a side

This is bit embarrasing, but I can’t seem to code into twine counting how many times the unfortunate chap in my story have visited a spesific site in the story.

I will be intergrating this in an if function to “unlock” spesific sites if you have died x amount. That I can do. But i can’t count the specific chapter and how many times i have visited that site.

For exsample: Counting how many chapteres that have ended in deaths you have gotten and make it a floating variable to use later.

Welcome @AndMos.

Please use the New Topic’s Optional Tags to state the Name and Version of the Story Format you are using, as answers can vary based on that information. I will assume that you are using the 3.x series of Harlowe (harlowe3), as that is the default story format of the Twine 2.x application.

There are a number of methods you can use to achieve the result you want, the following are two of them.

1: Querying the story’s History to determine how many times a specific Passage has been visited.

You can use the (history:) macro to obtain an Array that contains the Passage Name of all previously visited Passages, in the order that they were visited. (that Array will exclude an entry for the current Passage).

(set: _history to (history:))

You could then use the (count:) macro to determine how many times a specific Passage Name occurs within that Array, remembering that there is no entry representing the current visit to the current Passage.
eg. Assuming you are interested in visits to the Library passage.

(set: _count to (count: ...(history:), 'Library'))

warning: The Array returned by the (history:) macro needs to be generated each time the macro is called, and the size of that Array increases each time a Passage is visited. This means both the generation of the Array, and the counting of specific elements within it, takes a little longer as more Passages are visited.

2: Using a Numeric Story Variable to track the occurrences of something.

You can use the (set:) macro to initialise a story variable to zero. This initialisation should be done sometime before you want to change that variable’s value, like within your project’s startup tagged passage.

(set: $counter to 0)

You can use the same macro to increase/decrease that variable’s current value whenever the ‘thing’ you’re tracking occurs.

(set: $counter to it + 1)
(set: $counter to it - 1)

Obviously you should name your story variable(s) something that makes sense in the context the variable is being used.

Thank you so much mate. I will be using Optional Tags alot more. Right now im new to this site and famiarizing with both Twine and the Harlowe code

1 Like