(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 bravado
*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
*elseif (((((bravado > caution) and (bravado > sympathy)) and (bravado > severity)) and (bravado > discipline)) and (bravado > independence))
*set highest_stat "bravado"
*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!
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.
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.
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.
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 bravado
*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 ""
*comment add however many you need for all your variables
*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_5 "bravado"
*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.
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:
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.)