0 votes
by (200 points)

Hi I'm pretty new to twine but I'm making an encounter system. Everything works except for the health system. I have $health set to 100 initially at the begining of the game. During my encounter I am trying to get the health to decrease by 25 each time the monster lands a succesful attack. For whatever reason when I try to test it crashes and gives me an error message. This is the following code for if the monster lands a successful attack (I have the same exact code with different variable names for if you hit the monster). 

 

HP: (print: $health)
Werewolf HP: (print: $werewolfHP) 
(display: "To Hit")

	(if: $attackNumber is 0)[
		(set: $attackNumber to 6)
	]
	
	(set: $attackNumber to $attackNumber - 1)
	
	(set: $chance to (random: 1, $attackNumber) )
	


(if: $chance is 1 ) [The werewolf lunges at you with its fangs and claws! Take 25 damage!
(set: $attackNumber to 0)
(set: $health to "$health - 25")] 

(else:)[You evade the werewolf's attack!] 

 

1 Answer

0 votes
by (63.1k points)

Lose the quotes around the math. You only need quotes for strings, and that's an expression. Setting what Harlowe thinks is a number to a string is bound to cause errors.

So: 

(set: $health to $health - 25)

<!-- or -->

(set: $health to it - 25)

You may want to check and make sure you aren't doing that elsewhere, too .

by (200 points)
If I run

 

it - 25 then I get an error that it isn't a number

 

If I set $health - 25 then the whole thing crashes

 

regardless of if I use quotations or not
by (63.1k points)
You must be quoting it elsewhere in the code, thus it isn't a number when it's encountered.
by (200 points)
The whole code I initially put is the code in it's entirety. The only other place I reference $health is at the begining of the story. Could that be causing it?
by (63.1k points)
It could be if you've quoted the value.
by (159k points)

@diazepamdiva

You should be initialising your $health variable within your story's startup tagged special passage like so:

(set: $health to 100)

... obviously the 100 in the above example should be replaced with whatever number you want.

by (200 points)
I put (set: $health to 100) at the begining of the story, like not in that passage, but at the begining of the game. Would I have to reiterate that within my encounter set up?
by (200 points)
@Chapel thank you for your help. I'm new to twine though I've made a very basic game before. If you could help me a little further I'd really appreciate it.

I went through everything and changed all instances of $health to "#" to $health to $health to # (without quotations, all instances) now it's says "the string "100" isn't the same type of data as the number 25►I tried to perform an operation on some data, but the data's type was incorrect."

 

So I don't know what that means or what to do?
by (200 points)
OMG I got it to work. Thank you all for all your help!
...