Page 1 of 1

[solved] Stat rankings

Posted: Mon Nov 30, 2020 11:53 pm
by The King
Hello, so here's my latest question. Basically, what I want to do is have different endings based on the highest stat in the game. Suppose we have these 6 stats:
Strength
Toughness
Health
Intelligence
Stamina
Speed
Is there a way for me to make the game check which of these stats is the highest out of them all, and then go to the respective ending? (e.g. if strength is highest, it goes to the strength ending, etc.) If so, how can I do this? Also, it would be helpful if this would allow for ties, between any 2, 3, 4, 5 or all 6 of these stats. Please help me if you can, thank you. :)

Re: Stat rankings

Posted: Tue Dec 01, 2020 1:07 am
by _ticlock_
Hi The King,

Something like this:

Code: Select all

label endings:
    python:
        stats = [Strength, Toughness, Health, Intelligence, Stamina, Speed]
        m = max(stats)
        max_stat_index = [i for i, j in enumerate(stats) if j == m]
        number_of_stats =  len(max_stat_index)
     if number_of_stats == 1:
         #One max stat
         if max_stat_index[0] == 0:
             #Strength is max
             jump end_strength
         elif max_stat_index[0] == 1:
             #Toughness is max
              jump end_toughness
          ...
      if number_of_stats > 1:
          # Several stats are max
          #Some code to process
Basically, python code finds the max value and how many stats have this max value. Then you can process it in whatever way you prefer. I just gave a simple example with if statements and jump to the corresponding label.

Re: Stat rankings

Posted: Tue Dec 01, 2020 1:53 am
by hell_oh_world
u can use the max function then probably a dictionary to store the stats and create a known pattern in your label name so you can jump on them easily.

Code: Select all

default stats = dict(
  strength=3,
  stamina=5,
  # and more...
)

label start:
  $ stats["strength"] += 3 # this is how you access the stat, as a string index (key). the expected value of strength now is 6.
  $ highest = max(stats, key=lambda k: stats[k]) # get the key of the highest value in the dict.

  jump expression "{}_ending".format(highest)
  
label strength_ending:
  "Strength"
  
label stamina_ending:
  "Stamina"

Re: Stat rankings

Posted: Tue Dec 01, 2020 9:26 am
by gas
The above is a perfect approach.
To check for ties, the thing is quite more complex.
There are various possible approaches, this is one.

Code: Select all

label start:
    $ stats["strength"] += 3 # this is how you access the stat, as a string index (key). the expected value of strength now is 6.
    $ highest = max(stats, key=lambda k: stats[k]) # get the key of the highest value in the dict.
    $ paired = [i for i,j in stats.items() if j == highest] # return how many skills are equal the highest one
    if len(paired) == 1: # just a single high stat!
        jump expression "{}_ending".format(highest)
    if len(paired) == 2: # two stats are equal!
        jump double_ending
    if len(paired) == 3: # three stats are equal!
        jump triple_ending

label strength_ending:
    "Strength"
    return

label stamina_ending:
    "Stamina"
    return

label double_ending:
    "You've end with a couple of equal stats..."
    return

label triple_ending:
    "Three skills are the same!"
    return

EDIT: Now you can further indepth and check the values of 'paired' list to know WHAT stats are a tie. Have fun.

Re: Stat rankings

Posted: Tue Dec 01, 2020 1:55 pm
by Alex
The King wrote:
Mon Nov 30, 2020 11:53 pm
...
Is there a way for me to make the game check which of these stats is the highest out of them all, and then go to the respective ending? ...
Check this article - https://patreon.renpy.org/picking-a-winner.html

The others are interesting too...))
viewtopic.php?f=51&t=47328#p473308

Re: Stat rankings

Posted: Wed Dec 02, 2020 12:33 am
by The King
Okay, so here's how I implimented the code:

label statrankmenu:
$ stats["strength"] += 6 # this is how you access the stat, as a string index (key). the expected value of strength now is 6.
$ stats["stamina"] += 6
$ stats["spd"] += 6
$ stats["toughness"] += 6
$ stats["hp"] += 6
$ stats["iq"] += 6
$ highest = max(stats, key=lambda k: stats[k]) # get the key of the highest value in the dict.
$ paired = [i for i,j in stats.items() if j == highest] # return how many skills are equal the highest one
if len(paired) == 1: # just a single high stat!
jump expression "{}_ending".format(highest)
if len(paired) == 2: # two stats are equal!
jump double_ending
if len(paired) == 3: # three stats are equal!
jump triple_ending
if len(paired) == 4:
jump quadruple_ending
if len(paired) == 5:
jump quintuple_ending
if len(paired) == 6:
jump balanced_ending

label strength_ending:
"Strength is highest."
return
label stamina_ending:
"Stamina is highest."
return
label speed_ending:
"Speed is highest."
return
label intelligence_ending:
"IQ is highest."
return
label toughness_ending:
"You're a real toughie, aren't you?"
return
label health_ending:
"HP is highest."
return
label double_ending:
"You've end with a couple of equal stats..."
return
label triple_ending:
"Three skills are the same!"
return
label quadruple_ending:
"Four skills are the same!"
return
label quintuple_ending:
"Five skills are the same!"
return
label balanced_ending:
"You're very well rounded!"
return

The default stats I placed closer to the top, before the start label, which I wrote as such:

default stats = dict(
iq=1,
strength=1,
toughness=1,
spd=1,
stamina=1,
hp=1,
)

The issue is, that given all of these stats being equal, this should give the balanced ending. However, no matter what I do, no matter which value I raise to the highest, every single time I get the strength ending. Have I done something wrong? Should I keep the stats dictionary right above the stat ranking section, or is there something else wrong with the code I added? Please help me out, thank you.

Re: Stat rankings

Posted: Wed Dec 02, 2020 12:45 am
by hell_oh_world
Gas missed some part in the code.

Code: Select all

$ paired = [i for i,j in stats.items() if j == highest]
should be...

Code: Select all

$ paired = [i for i,j in stats.items() if j == stats[highest]]
and like i said, you better off naming your label with some pattern so you can just avoid all of these conditions.

Code: Select all

label start:
  # your previous codes before the tiresome conditions...
  if len(paired) > 1:
    jump expression "tie{}_ending".format(len(paired))
    
  else:
    jump expression "{}_ending".format(highest)

label tie2_ending:
  "2 Stats Tied"
  
label tie3_ending:
  "3 Stats Tied"

Re: Stat rankings

Posted: Wed Dec 02, 2020 11:53 pm
by The King
Okay, everything seems to be in working order now, I even tested every outcome and they all gave the results I wanted. Thank you everyone for your help. :)