# Is there a simple way to find the variable with the highest value out of a group?

(Hello! My first post - diving right in with a question )
I’ve already found a way to do this, but I was wondering if there was an easier one! I wanted to have my game’s plot change based on which of several character stats was currently highest. So far, I’ve used a bunch of *if commands to check each variable to see if it was higher than all the others, but it’s pretty messy with six different variables to compare:

``````*temp sympathy
*temp severity
*temp independence
*temp discipline
*temp caution

*label stat_calculate
*if (((((sympathy > severity) and (sympathy > discipline)) and (sympathy > independence)) and (sympathy > caution)) and (sympathy > bravado))
*set highest_stat "sympathy"
*goto calc_bottom
*elseif (((((severity > sympathy) and (severity> discipline)) and (severity > independence)) and (severity > caution)) and (severity > bravado))
*set highest_stat "severity"
*goto calc_bottom
*elseif (((((independence > discipline) and (independence > sympathy)) and (independence > severity)) and (independence > caution)) and (independence > bravado))
*set highest_stat "independence"
*goto calc_bottom
*elseif (((((discipline > independence) and (discipline > sympathy)) and (discipline > severity)) and (discipline > caution)) and (discipline > bravado))
*set highest_stat "discipline"
*goto calc_bottom
*goto calc_bottom
*elseif (((((caution > bravado) and (caution > sympathy)) and (caution > severity)) and (caution > discipline)) and (caution > independence))
*set highest_stat "caution"
*goto calc_bottom
*else
*set highest_stat "perfect balance"
*goto calc_bottom
*label calc_bottom
``````

I plan to just use this as a subroutine if I happen to need it again, but it occurred to me I might want to do it again with completely different stats (which of three characters likes you the most, for example), and I was wondering if there was a shortcut so I wouldn’t have to count all those parentheses again!

1 Like

There might be a simple command I don’t know, but what I would do is add an extra couple of variables for shorter code. Call them temp_trait_trait1 and temp_trait2
Then just compare 3 variables twice and set the winners (highest) as the temp_trait variables. Finally compare the temp_trait variables and set whichever wins as temp_trait1

Now in your story, you can type
`*if temp_trait1 = "sympathy" Insert sympathy story here. Etc.`

Have the first part as a gosub_scene and you can update and check every time it’s needed.

If you look up at here, under “truly bizarre references,” there’s a method called “curly parens.”

Try check it out. I’m currently on so I can’t provide some actual feedback
When I home, I’ll try to work a code for you

1 Like

Your variables can either hold a number or a string.
If they’re going to hold a number, you’ll want to have a number after the name of the variable.

Finding the highest stat also has to account for instances where one stat may be the same as another.
Example: If sympathy is equal to bravado; etc.

1 Like

Given the relatively low number of comparisons you’re looking at here (or for similar purposes), I’d probably just step through them in simple fashion, comparing each to the previous one.

``````*set top_stat sympathy
*set highest_stat "Sympathy"

*if severity > top_stat
*set top_stat severity
*set highest_stat "Severity"
*if independence > top_stat
*set top_stat independence
*set highest_stat "Independence"
*if discipline > top_stat
*set top_stat discipline
*set highest_stat "Discipline"
*if caution > top_stat
*set top_stat caution
*set highest_stat "Caution"

Best Stat Value: \${top_stat}
Best Stat Name: \${highest_stat}
``````

As @Carlos.R pointed out, you do need to decide what to do in the event of identical values. In this example the Best Stat would remain as “Sympathy” even if another value matched it. You may, for instance, decide to simply list each *if check in a particular order of priority.

6 Likes

Okay, so this is my code.

``````*temp sympathy 1
*temp severity 2
*temp independence 3
*temp discipline 4
*temp caution 6

*create highest_stat "perfect balance"

*label stat_calculate
*if (sympathy < severity)
*set highest_stat "severity"
*if ({highest_stat} < independence)
*set highest_stat "independence"
*if ({highest_stat} < discipline)
*set highest_stat "discipline"
*if ({highest_stat} < caution)
*set highest_stat "caution"

*label calc_bottom
\${highest_stat}
``````

If you take a look, I use a series of `*if` instead of combination between `*if/*else/*elseif`. And yes, it does work.

Play around with it, try with other variables name, use it in the actual game. And lastly, have fun coding!

Feel free to ask again if you’re not sure of something.
Edit: Ninja’d again

5 Likes

Maybe, but you score bonus points for using fewer lines and the coolness factor of curly braces for indirect reference.

3 Likes

I guess we’re even, then

P.S. I just realized that I use `*create` below the `*temp`
Such magic.

2 Likes

Just want to throw this out there in case you want code that’ll work for a variety of different variable comparisons:

``````*temp sympathy
*temp severity
*temp independence
*temp discipline
*temp caution
*comment other variables go here

*temp max_num 0
*temp max_stat ""
*temp n_1 ""
*temp n_2 ""
*temp n_3 ""
*temp n_4 ""
*temp n_5 ""
*temp n_6 ""
*temp i 0
*temp fin 0
*comment if you want to use gosub_scene you'll need to make all
of the variables in startup using *create instead of *temp

*label set_variable_refs_1
*set n_1 "sympathy"
*set n_2 "severity"
*set n_3 "independence"
*set n_4 "discipline"
*set n_6 "caution"
*set fin 6
*goto max_loop

*comment to do the same with other variable sets, add other
*labels (set_variable_refs_2 etc) - you'll need to set n_1, n_2, etc
to the variable names and fin to the number of variables, and
make sure to *goto max_loop

*label max_loop
*set i +1
*if ({n[i]} > max_num)
*set max_num {n[i]}
*set max_stat n[i]
*goto where_next

*label where_next
*if i = fin
*set i 0
*return
*else
*goto max_loop
``````

This is a simple version that doesn’t account for ties, as mentioned before.

Also I just want to point out that your variables seem to be 3 opposed pairs - just as a reminder, opposed pairs shouldn’t have two variables; instead, you’ll add or subtract from one variable to give the illusion of two opposed variables (as in, +sympathy would make more sympathy, while -sympathy would make more severity). Of course that would also change how you calculate the highest variable, though.

4 Likes

I hadn’t even noticed that those six variables appear to be three opposed_pair types for *stat_chart purposes, so ‘Severity’, ‘Discipline’ and ‘Caution’ would not themselves actually exist as permanent variables. Well spotted @Nonvita

@Alexandra - In which case, something like this should work fine for your purposes instead:

``````*temp sympathy 30
*temp independence 40

*temp top_stat sympathy
*temp highest_stat "Sympathy"

*if independence > top_stat
*set top_stat independence
*set highest_stat "Independence"
*if (100 - sympathy) > top_stat
*set top_stat (100 - sympathy)
*set highest_stat "Severity"
*if (100 - independence) > top_stat
*set top_stat (100 - Independence)
*set highest_stat "Discipline"
*if (100 - bravado) > top_stat
*set highest_stat "Caution"

Best Stat Value: \${top_stat}
*line_break
Best Stat Name: \${highest_stat}
``````

The above would result in:

Best Stat Value: 70
Best Stat Name: Severity

…even though Severity is not an actual permanent variable *created in startup.txt.

3 Likes

Thank you everyone! @Vendetta and @Szaal, that was just the kind of clear solution I was hoping for. @Nonvita’s code was a bit trickier for me to follow, but it was really interesting to get an idea of what choicescript can do (my brain is stretching!) . And you’re right, they are opposed pairs, but I know what I’m doing with them (I think ). I just skipped the part of my code where I defined them:
``` *set severity 100 - sympathy *set independence 100 - discipline *set bravado 100 - caution ```

(Yeah, I wrote them out of order in my comparison. It just kind of happened, and I left it.)

3 Likes

Heh. And here I am, contemplating to split my opposed-pair stats into 2 individual stats.

2 Likes