[solved] Stat rankings

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
The King
Regular
Posts: 35
Joined: Fri Sep 25, 2020 12:37 pm
Completed: 0
Projects: LB
Contact:

[solved] Stat rankings

#1 Post by The King » Mon Nov 30, 2020 11:53 pm

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. :)
Last edited by The King on Wed Dec 02, 2020 11:53 pm, edited 1 time in total.

User avatar
_ticlock_
Veteran
Posts: 393
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: Stat rankings

#2 Post by _ticlock_ » Tue Dec 01, 2020 1:07 am

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.

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Projects: The Button Man
Organization: NILA
Github: hell-oh-world
Location: Philippines
Contact:

Re: Stat rankings

#3 Post by hell_oh_world » Tue Dec 01, 2020 1:53 am

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"

User avatar
gas
Miko-Class Veteran
Posts: 838
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Stat rankings

#4 Post by gas » Tue Dec 01, 2020 9:26 am

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.
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll not be notified about. << now red so probably you'll see it.

10 ? "RENPY"
20 GOTO 10

RUN

User avatar
Alex
Lemma-Class Veteran
Posts: 2981
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Stat rankings

#5 Post by Alex » Tue Dec 01, 2020 1:55 pm

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

User avatar
The King
Regular
Posts: 35
Joined: Fri Sep 25, 2020 12:37 pm
Completed: 0
Projects: LB
Contact:

Re: Stat rankings

#6 Post by The King » Wed Dec 02, 2020 12:33 am

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.

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Projects: The Button Man
Organization: NILA
Github: hell-oh-world
Location: Philippines
Contact:

Re: Stat rankings

#7 Post by hell_oh_world » Wed Dec 02, 2020 12:45 am

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"

User avatar
The King
Regular
Posts: 35
Joined: Fri Sep 25, 2020 12:37 pm
Completed: 0
Projects: LB
Contact:

Re: Stat rankings

#8 Post by The King » Wed Dec 02, 2020 11:53 pm

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. :)

Post Reply

Who is online

Users browsing this forum: No registered users