0 votes
by (120 points)
Hi! So I'm new to using Twine, making a little short text-based RPG, using Harlowe. My question, which probably has a really simple answer that I've just been missing, is this: how would I change a variable by, say, minus 2 when a certain passage is reached? I have it set up so that when a combat is lost, it takes you to a "lost combat" page, but I want it so that when you get to that page, not only does it display text saying that you lost the combat, but it also takes whatever the current health variable is and subtracts 2. I tried doing (set: $health to $health-2) and (set: $health to $health minus 2), but neither worked- both set my health to "NaN" (which I can only assume stands for "Not a Number".)

Anyone know how to easily do this?

Thanks!

1 Answer

0 votes
by (2.2k points)
(set: $var to it - 2)

That's what it is. You can also do,

(set: $var to $var - 2)

but that's extra typing that's not necessary.

Spaces are also important. 

by (159k points)

You should also initialise all your story variables before you manipulate them.

I suggest doing that within a startup tagged special passage, if your story doesn't have one yet then simple create a new passage (give it whatever name you like, I use Startup) and assign it a startup tag.

(set: $health to 0)

note: Some people may point out that Harlowe automatically assigns the value of zero (0) to any undefined/uninitialised story variable referenced within a passage, I suggest not relying on that automatic behaviour because:
a. You can't predict if that behaviour will be changed in a later version of the story format.
b. Manually initialising gives you a list of all the story variables which can help you remember which ones you have and (more importantly) how you spelt their names.

Use CSS like the following in your story's Story Stylesheet area to hide any visual output generated within the startup tagged special passage.

tw-include[type="startup"] {
	display: none;
}

 

...