0 votes
by (130 points)

I'm working with twine 2 for the first time now and i searched for a while for an answer to my problem, but i can't really find anything so i thought i simply ask myself.

My Problem is simple: I'm working with some kind of point system in my story. It's scaled from 1-20 and the player is able to collect or lose points depending on his choices. But i want to prevent the player from collecting too many points (more than 20 in my case) so i thought just declare that if the value that i use for this is higher than 20, he should simply reset it back to 20.

That works for now, but i'm looking for a way to set this for every passage without rewriting it over and over again. I don't really know how to set "global" rules (not sure if thats how they're called).

if someone could help me with that problem, i would be really glad.

Have a nice day!

2 Answers

+1 vote
by (63.1k points)

Harlowe doesn't have any sort of clamping function, but you can shorten your expressions using the (min:) macro. For example: 

(set: $points to (min: $points + 1, 20))

You could use a (display:) macro to do the deed quickly as well. Say you write a passage called "check points" and put this in it: 

(if: $points > 20)[
    (set: $points to 20)

Then when you raise your points, you can do this: 

(set: $points to it + 1)
(display: "check points")

The other alternative is to use a header- or footer-tagged passage and include basically the same code as in the "check points" passage, but this will not prevent the number of points from going over twenty at any time, only when passages change, meaning its not really ideal for this purpose. 

The other potential solution is to write a JavaScript function that clamps things for you, but this is probably overkill. 

window.clamp = function (val, max) {
    max = max || 20;
    return (val > max) ? max : val; 

Then, in your passage code: 

(set: $points to clamp($points + 1))

Note that I didn't test any of these, so it's possible I made an error in logic or syntax. 

0 votes
by (159k points)

The Twine Cookbook's repository now contains a yet unpublished recipe related to Limiting the range of a number (Harlowe recipe) which extends the window.clamp() function example shown by @Chapel.

There are also SugarCube and Snowman versions of the same recipe.

by (63.1k points)
I'm not sure what Harlowe's default browser support is or what it targets, but the use of const and let in those functions may cause some users to lose a bit of support, since most twine authors are unlikely to transpile or minify their code.
by (159k points)
edited by

@Chapel Thank you for point that out, I forgot to check for the usages of >ES5 related features.

NOTE: These unpublished recipes have now been updated to remove said ES6 related features.

(<sigh> I keep forgetting to either not use ES6 related code in the JavaScript code I write, or to down-grade any such features in any code I obtain from else where. The later being the case with this particular recipe.)