Checking a variable plus a number isn't larger than another variable

Hi all

At the start of my adventure, my character has a variable called $InitialStamina set. During the story, another variable called $CurrentStamina is used to record the value as it will vary due to battle damage etc.

When the character has a meal, I want the value of 4 added to $CurrentStamina but if that addition would result in a number greater than the $InitialStamina variable, I want that returned instead.

If not, then 4 should be added to $CurrentStamina.

The code I’m trying is this:

(if: $CurrentStamina + 4 > $InitialStamina)[(set: $CurrentStamina to $InitialStamina)]
(else: (set: $CurrentStamina to $CurrentStamina + 4))

But it keeps adding 4 to the $InitialStamina regardless.

I’ve tried a couple of variations (losing the square brackets, using commas, using parentheses within parentheses) but can’t seem to get that first test of “is $CurrentStamina + 4 greater than $InitialStamina” to work.

Still new to Twine so have no doubt it’s me not understanding the syntax. :slight_smile:

(if: $CurrentStamina + 4 > $InitialStamina)[(set: $CurrentStamina to $InitialStamina)]
(else:)[(set: $CurrentStamina to $CurrentStamina + 4)]

Yeah, watch out for the location of your parentheses (relevant documentation page) :wink:

And Angularity of those parentheses, of course.

(As a writer, I am entitled to make up new words whenever I want.)

1 Like

Thanks - that works a treat! :+1:

The (min:) macro can also be used to achieve the “Current should never exceed Initial” behaviour you’re asking for…

(set: $CurrentStamina to (min: it + 4, $InitialStamina))

And the following test can be used to prove that the above code works…

<!-- assign known values for debugging -->
(set: $InitialStamina to 10, $CurrentStamina to 7)

(set: $CurrentStamina to (min: it + 4, $InitialStamina))

Debug: New Current Stamina: $CurrentStamina  (should equal 10 instead of 11)

<!-- assign another known value for debugging -->
(set: $CurrentStamina to 2)

(set: $CurrentStamina to (min: it + 4, $InitialStamina))

Debug: New Current Stamina: $CurrentStamina  (should equal 6 instead of 10)

yea; one of the initial difficulties I got (back in 80s…) in learning to code was the lack of “angularity” in programming languages, e.g. {4*[5+3]- [6/(4-2)]} becoming (4*(5+3)-(6/(4-2)))
no wonder that I dislike lisp-based languages in general…

Best regards from Italy,
dott. Piergiorgio.