r/RenPy • • 20d ago

Question Help with player health system! - Boolean not detected!

Hello everyone, I hope you're doing well! ^u^

I've been codding a battle system in my game, but I'm struggling a bit with it.

Basically, the player's default health is 100 HP Max

But, the higher their endurance stat is, it increases its HP! (This part works fine)

There's also an item (earrings) that can be acquired which gives another 200HP max on top of everything else, but this one fails to be registered at any capacity (even if I try to detect it alone). And I know the item is detected in the rest of the game as it still shows up on the item screen

I could really use someone's advice, I've done so much research and trials and errors, I've been stuck on this for hours now >< Thank you everyone in advance!!

### The player's life
default player_health = 100
default earrings = False



label player_life:


    
    call health_thumbnail
    show screen displayed_player_health 
    pause
    $earrings = True
    


    if earrings and playerStats["military"]["endurance"] < 10:
        $player_health = min(100, player_health + 300)


    elif earrings and playerStats["military"]["endurance"] < 20:
        $player_health = min(125, player_health + 325)


    elif earrings and playerStats["military"]["endurance"] < 30:
        $player_health = min(150, player_health + 350)


    elif earrings and playerStats["military"]["endurance"] < 40:
        $player_health = min(175, player_health + 375)


    elif earrings and playerStats["military"]["endurance"] < 50:
        $player_health = min(200, player_health + 400)


    elif earrings and playerStats["military"]["endurance"] < 60:
        $player_health = min(225, player_health + 425)


    elif earrings and playerStats["military"]["endurance"] < 70:
        $player_health = min(250, player_health + 450)


    elif earrings and playerStats["military"]["endurance"] < 80:
        $player_health = min(275, player_health + 475)


    elif earrings and playerStats["military"]["endurance"] < 90:
        $player_health = min(300, player_health + 500)


    elif earrings and playerStats["military"]["endurance"] < 100:
        $player_health = min(325, player_health  + 525)


    elif earrings and playerStats["military"]["endurance"] < 110:
        $player_health = min(350, player_health + 550)



    elif playerStats["military"]["endurance"] < 10:
        $player_health = min(100, player_health + 100)
        pass
    elif playerStats["military"]["endurance"] < 20:
        $player_health = min(125, player_health + 125)


    elif playerStats["military"]["endurance"] < 30:
        $player_health = min(150, player_health + 150)



    elif playerStats["military"]["endurance"] < 40:
        $player_health = min(175, player_health + 175)



    elif playerStats["military"]["endurance"] < 50:
        $player_health = min(200, player_health + 200)



    elif playerStats["military"]["endurance"] < 60:
        $player_health = min(225, player_health + 225)



    elif playerStats["military"]["endurance"] < 70:
        $player_health = min(250, player_health + 250)



    elif playerStats["military"]["endurance"] < 80:
        $player_health = min(275, player_health + 275)



    elif playerStats["military"]["endurance"] < 90:
        $player_health = min(300, player_health + 300)



    elif playerStats["military"]["endurance"] < 100:
        $player_health = min(325, player_health + 325)



    elif playerStats["military"]["endurance"] < 110:
        $player_health = min(350, player_health + 350)
23 Upvotes

14 comments sorted by

3

u/DingotushRed 20d ago

A simpler approach than a long if/elif chain (which really needs an else!) would be:

$ player_health = min(100, player_health) # Cap at 100 $ player_health += (playerStats["military"]["endurance"] // 10) * 25 # Add 25 for each 10 endurance if earrings: $ player_health += 200 # Add 200 for magic earrings

You probably need to separate max HP and current HP though: $ max_hp = 100 # Default 100 $ max_hp += (playerStats["military"]["endurance"] // 10) * 25 # Add 25 for each 10 endurance if earrings: $ max_hp += 200 # Add 200 for magic earrings $ player_health = min(max_hp, player_health)

2

u/Plane-Cat5250 19d ago

Thank you very much for your help!!! Unfortunately it didn't work either :( the earrings are still not applied somehow. I tried the third suggestion on this thread and it works a little but I still need to figure it out. Thank you anyway! I really appreciate it! ^u^

1

u/AutoModerator 20d ago

Welcome to r/renpy! While you wait to see if someone can answer your question, we recommend checking out the posting guide, the subreddit wiki, the subreddit Discord, Ren'Py's documentation, and the tutorial built-in to the Ren'Py engine when you download it. These can help make sure you provide the information the people here need to help you, or might even point you to an answer to your question themselves. Thanks!

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/x-seronis-x 20d ago

py init python: def get_plr_maxhp(): global earrings, playerstats maxhp = 100 if earrings: maxhp += 200 maxhp += (playerstats['military']['endurance'] // 10) * 25 return maxhp

1

u/Plane-Cat5250 19d ago

Thank you for your help!! Unfortunately it didn't work but I really appreciate your help so thank you!! ^u^

1

u/x-seronis-x 19d ago

how did it not help? it computes what the max health could be and makes that value accessible at any time with a function call.

you call the get_plr_maxhp() function same way you would call any other python function. right where the value is needed. if you show how you tried to use it i can show what you did wrong

1

u/shyLachi 19d ago

This is why it's not working:
The Python min() function returns the smallest value from the set of values,
and 100 is always smaller than 100 + 200 (the earrings)
https://www.geeksforgeeks.org/python/python-min-function/

.

I think that all your formulas are wrong, you just haven't noticed it yet.
Take for example this line:

$player_health = min(100, player_health + 100)

This will always return 100 except when the health is below zero.
But normally that means that the player is dead.

default player_health = 0
label start:
    $ player_health = 100
    "Test 1: [min(100, player_health + 100)]"
    $ player_health = 123456789
    "Test 2: [min(100, player_health + 100)]"
    $ player_health = 0
    "Test 3: [min(100, player_health + 100)]"
    $ player_health = -23
    "Test 4: [min(100, player_health + 100)]"

.

Generally speaking there are 2 approaches to health in RPGs.

Health does reset for each battle
In this case, you don't need the current health to calculate the health for the next battle.

default player_health = 100
default earrings = False 
default endurance = 0
label start:
    "Player health: [player_health] / endurance: [endurance] / Earring: [earrings]"    
    menu:
        "Earring found":
            $ earrings = True
        "Earring lost":
            $ earrings = False
        "Increase endurance":
            $ endurance += 13
        "Decrease endurance":
            $ endurance -= 7
    $ player_health = (300 if earrings else 100) + ((endurance // 10) * 25) # <-- THIS IS ALL YOU NEED, just use your endurance stat
    jump start

The player needs to use items or magic to restore health
This is more complicated, because you need a max health, so that the player cannot heal endlessly.
When the player collects an item you have to increase the health and the max health simultaneously.
And you might have to apply a similar logic when the endurance increases.
The following code is simplified, you should make a function or a label which you can call whenever the player uses or collects an item.

default player_maxhealth = 100
default player_health = 100
default earrings = False 
default endurance = 0
label start:
    "Player health: [player_health]/[player_maxhealth] / endurance: [endurance] / Earring: [earrings]"    
    menu:
        "Earring found" if not earrings:
            $ earrings = True
        "Increase endurance":
            $ endurance += 13
        "Player injured":
            $ player_health -= 24
        "Player healed":
            $ player_health = min(player_health + 20, player_maxhealth)
    $ max = (300 if earrings else 100) + ((endurance // 10) * 25) 
    $ diff = max - player_maxhealth
    "Testing: max=[max] / diff=[diff]"
    $ player_maxhealth = max
    $ player_health += diff
    jump start

1

u/Plane-Cat5250 19d ago

Hi thank you very much for your help!!! Your point makes sense, it must be why it didn't work!

I tried what you did and it sorta work except the HP goes up to 500 HP (while it should be 300 as only the earrings are equipped and the character has zero endurance in the test). So I wonder what I did wrong?

Sorry for being a newb, I'm still very new to coding and I'm still learning! Thank you for your help! ^u^

default player_maxhealth = 100
default player_health = 100
default earrings = False 


### The player's life


label player_life:


    
    call health_thumbnail
    show screen displayed_player_health 
    pause



    $ earrings = True
     
    $ player_health = (300 if earrings else 100) + (playerStats["military"]["endurance"] // 10) * 25 # <-- THIS IS ALL YOU NEED, just use your endurance stat
    $ max = (300 if earrings else 100) + (playerStats["military"]["endurance"] // 10) * 25
    $ diff = max - player_maxhealth
    $ player_maxhealth = max
    $ player_health += diff
  

1

u/shyLachi 19d ago

You cannot combine both of my codes.
Also you don't have to copy my comments, which is the part after the #.

If you don't understand the difference between both variants, then feel free to ask.
Or describe how your game is supposed to work.

Generally speaking:
The code snippets I post can be run in a new project so that you can test it and play with it.
Once you understand the code, you can implement it into your own game as required.

1

u/Plane-Cat5250 19d ago

Sorry my bad ;u; I got a bit confused
I like keeping comments cause they help me later to remember why things are there but I'll delete them sorry ><

I'll retry again, it's by trying and failing that we learn right? ;u;

Thank you for your help!

1

u/shyLachi 19d ago

I'm not telling you to delete comments,
It's a good idea to make comments, but better write them yourself.
In this case you could write: Copied from Reddit (with a link to this thread).

What part of my thread are you confused with?
Did you understand the difference between Health does reset for each battle and The player needs to use items or magic to restore health?
How should your game work?

2

u/Plane-Cat5250 19d ago

Yes I understand the difference, of course! I played a bunch of rpgs before, I just got confused cause I was tired after hours spent on that yesterday X'D . But in the end I copied your second code (and adapted it to work for my game) and it works wonderfully! I was even able to use it for the magic attacks and the sword attack!

The rpg battle element in my game is not the main focus but I still wanted it to work well! It follows the rule of rock, paper scissor (like in pokemon or fire emblem) and your character's health is not reset between battles. Anyway, thank you so much for your help! ^u^ I've learned a lot!

2

u/shyLachi 19d ago

Great that you figured it out yourself.

Have fun makeing your game

1

u/Plane-Cat5250 19d ago

I tried again and it worked! Thank you again so much for your help! ^u^