Compare variables and return the highest value (Sugarcube)?

Twine Version: 2.3.14
Story Format: Sugarcube

Hi, is there a handy macro to return the highest value from a list of variables in Sugarcube? I’ve found this example for Harlowe, I basically need to do the same in Sugarcube:

(max: $compliment, $insult, $something, $anotherthing)

Assuming we’re talking about numbers, Math.max().

Math.max($compliment, $insult, $something, $anotherthing)
2 Likes

It took me a while to figure out I needed to create a new variable and assign it to the result! But then it worked. Many thanks amigo.

Can you show us a copy of your code, so that we can understand what you mean? I’m trying to understand Math.max() and I’m not finding any indication that it works with variables, and I think the solution you found might be the one I need, but I can’t understand what you mean.

First. Holy necropost, Batgirl! After 10 months, it’s probably better to simply ask your own question rather than attempting to coattail someone else’s.

 

By “variables” I’m assuming you mean story format managed variables—i.e., story and temporary variables.

Scripting-wise, at least, most story formats are largely just varying amounts of sugar atop JavaScript, so the general answer to “does this JavaScript built-in work on story format managed variables” is yes.

As to the Math.max() static method in particular, it simply returns the greatest number value of those it was passed.

For an example:

/* These story variables would be set prior. */
<<set $compliment to 13>>
<<set $insult to 9>>
<<set $something to 21>>
<<set $anotherthing to 4>>

/* Set the temporary variable `_maxVal` to the greatest value. */
<<set _maxVal to Math.max($compliment, $insult, $something, $anotherthing)>>

/* Print the value stored in `_maxVal` (will be `21` in this example). */
The max value is: <<= _maxVal>>

First off, the Holy Necropost made me giggle on a very bad day, so thank you for that! I probably should start my own post, because even with your explanation, I don’t think it quite answers what I’m really asking. TAT So I’m going to start a new topic and post the link to that topic here: My Weird Topic