How to get Choicescript to determine the highest stat

It’s entirely possible to over-think a particular problem searching for an elegant solution . . . The “brute force” method would work fine here, which in this case would be a simple series of checks using temporary “mainstat” and “mainstatval” variables to carry the name and value of the player’s highest current stat.

Begin by setting these to match the first stat, e.g. Leadership, then iterate through the *if checks, changing these two mainstat values only if another stat is higher, along the following lines.


*label main_stat_calc
*temp mainstat
*set mainstat "Leadership"
*temp mainstatval
*set mainstatval leadership
*if strength > mainstatval
  *set mainstat "Strength"
  *set mainstatval strength
*if intelligence > mainstatval
  *set mainstat "Intelligence"
  *set mainstatval intelligence
*if wisdom > mainstatval
  *set mainstat "Wisdom"
  *set mainstatval wisdom
*if dexterity > mainstatval
  *set mainstat "Dexterity"
  *set mainstatval dexterity
(etc. for as many stats as needed)
*return

Also, by giving this segment of code its own *label and ending with a *return command, you effectively turn it into a subroutine, meaning that you can run this check again (just by using the *gosub command) anywhere in the same scene file to update the name and value of the player’s current “main stat”, without having to repeat any of the actual code.

In your actual story scripting you would first update the name and value of the player’s current main stat using the *gosub command, then make use of it as required, e.g.


*gosub main_stat_calc
*if mainstat = "Wisdom"
  (etc.)

If you’re likely to use this subroutine in more than one scene file, just copy-paste it to the bottom of each file and define “mainstat” and “mainstatval” in mygame.js rather than as *temp variables.

4 Likes