Choose randomly from two stats if they are equal

Hello,

So at the moment I’m trying to make something work where in the player’s stat screen, there is a little bit of text that changes depending on what their highest stat is at that time.

I’ve figured out how to accomplish this with another wonderful user’s–Vendetta’s–code, which I have graciously stolen. At the moment, it looks a bit like this:

*label main_stat_calc
*temp mainstat
*set mainstat "Might"
*temp mainstatval
*set mainstatval might
*if charm > mainstatval
  *set mainstat "Charm"
  *set mainstatval charm
*if wit > mainstatval
  *set mainstat "Book-smarts"
  *set mainstatval wit
*return

And so on and so forth with the rest of the stats in my game, with a *gosub in the stats menu to re-run this check whenever needed. However, it occurred to me that there might be a problem if two stats happen to be exactly equal. I’d like for the game to randomly select one out of the stats that are equal to display that stat’s little line of text, but I’m not sure how to accomplish this. Something with *rand and *if charm = mainstatval or something like that? But I am not skilled in CS enough to have a go at it on my own.

If anyone has any ideas it would be much appreciated. Thanks so much in advance!

1 Like

First of all, I would recommend keeping the complexity of the code low use the >= operator to choose a stat if they are equal, so it becomes something like this


*if charm >= mainstatval
    *set mainstat "Charm"
    *set mainstatval charm
*if wit >= mainstatval
  *set mainstat "Book-smarts"
  *set mainstatval wit
*return

If keeping randomness is important, you can do something like this


*temp tiebreaker 0
*rand tiebreaker 0 1

*if charm >= mainstatval
  *if (tiebreaker = 1)
    *set mainstat "Charm"
    *set mainstatval charm
*if wit >= mainstatval
  *if (tiebreaker = 1)
    *set mainstat "Book-smarts"
    *set mainstatval wit
*return
3 Likes

I…uh… went a bit overboard with this :sweat_smile:
But it works as intended

*label main_stat_calc
*temp mainstat
*temp diceroll 0
*set mainstat "Might"
*temp mainstatval
*set mainstatval might
*if charm > mainstatval
  *set mainstat "Charm"
  *set mainstatval charm
*if wit > mainstatval
  *set mainstat "Book-smarts"
  *set mainstatval wit
*if ((might = charm) or ((wit = charm) or (wit = might)))
    *if might = charm
      *rand diceroll 0 1
      *if diceroll = 0
        *set mainstat "Charm"
        *set mainstatval charm
      *if diceroll = 1
        *set mainstat "Might"
        *set mainstatval might
    *if wit = charm
      *rand diceroll 0 1
      *if diceroll = 0
        *set mainstat "Charm"
        *set mainstatval charm
      *if diceroll = 1
        *set mainstat "Book-smarts"
        *set mainstatval wit
    *if wit = might
      *rand diceroll 0 1
      *if diceroll = 0
        *set mainstat "Might"
        *set mainstatval might
      *if diceroll = 1
        *set mainstat "Book-smarts"
        *set mainstatval wit
    *if ((wit = might) and (might = charm))
      *rand diceroll 0 2
      *if diceroll = 0
        *set mainstat "Might"
        *set mainstatval might
      *if diceroll = 1
        *set mainstat "Book-smarts"
        *set mainstatval wit
      *if diceroll = 2
        *set mainstat "Charm"
        *set mainstatval charm
*return
2 Likes

This is one that CSLIB has an implementation of you can reference: cslib/cslib_number.txt at main · ChoicescriptIDE/cslib · GitHub

(Look for the “max_stat” label).

Usage:

*create player_strength 5
*create player_dexterity 8
*create player_intelligence 8

*gosub_scene cslib_number max_stat "player_strength" "player_dexterity" "player_intelligence"
${cslib_ret} == "player_dexterity"

The only difference is that equal values aren’t randomised, it just sticks with the first encountered one instead. Which is probably fine for most use cases?

2 Likes

Just sticking to the first one works perfect. I’ll be researching a bit more to try and understand it better but this is great. Thank you so so much!

I…admittedly, did not consider the first solution, which would work just fine for my purposes, however these are both excellent. Thank you so so much!!

Oh you’re a lifesaver, this is actually perfect as well. Thank you so so much for taking the time to write this up!!!

saviour, was looking for this.

@alphayash @halfmooncroissant There is a mistake in the second code snippet I provide, it will set the max only if tie-breaker is 1 even if the stat is greater. To fix it, just do this

1 Like

This topic was automatically closed 24 hours after the last reply. If you want to reopen your WiP, contact the moderators.