[SOLVED] Resetting points/ stats to initial values

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
Kinmoku
Miko-Class Veteran
Posts: 591
Joined: Mon Aug 11, 2014 9:39 am
Completed: One Night Stand
Projects: VIDEOVERSE
Tumblr: gamesbykinmoku
itch: kinmoku
Location: Germany
Contact:

[SOLVED] Resetting points/ stats to initial values

#1 Post by Kinmoku »

Hi all,

I have some health and mana stat bars which goes up and down during battles.

At the end of the battle, I'd like to restore them to their initial values (i.e. 100). Now, I did just +100 points to each characters HP/MP, but, unless I get my maths right every time, they are sometimes a few numbers out.

To make my life much easier (because of multiple outcomes, routes etc), I'm wondering if there's a way to reset any points/ stats to their initial values OR an earlier state? It would be much more accurate and save me a lot of time.

Here's my characters stat bars:

Code: Select all

    class player_character:
        def __init__ (self, avatar, name, hp, hpmax, mp, mpmax):
            self.avatar = avatar
            self.name = name
            self.hp = hp
            self.hpmax = hpmax
            self.mp = mp
            self.mpmax = mpmax
            
    #CHARACTERS
    female = player_character("profile.png", "Female", 100, 100, 100, 100)
    
    nightshade = player_character("party_nightshade.png", "Nightshade", 100, 100, 100, 100)
    watto = player_character("party_watto.png", "Watto", 100, 100, 100, 100)
    emarin = player_character("party_emarin.png", "Emarin", 100, 100, 100, 100)
    celes = player_character("party_celes.png", "Celes", 100, 100, 100, 100)
    
    profile = female
    player1 = nightshade
    player2 = watto
    player3 = emarin
    player4 = celes
And this was my "quick fix":

Code: Select all

    "We rest up and restore our health and mana."
    
    $ profile.hp+=100
    $ profile.mp+=100
    $ player1.hp+=100
    $ player1.mp+=100
    $ player2.hp+=100
    $ player2.mp+=100
    $ player3.hp+=100
    $ player3.mp+=100
    $ player4.hp+=100
    $ player4.mp+=100
Last edited by Kinmoku on Mon Feb 01, 2016 7:06 am, edited 1 time in total.

philat
Eileen-Class Veteran
Posts: 1912
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Resetting points/ stats to initial values

#2 Post by philat »

Er... why not just player1.hp = 100?

I mean, you can optimize the writing experience further by throwing it into a label and calling it or something, but there's nothing wrong with just assigning a value to the attribute.

User avatar
Iylae
Regular
Posts: 73
Joined: Sat Jan 09, 2016 6:57 am
Location: Cornwall, UK
Contact:

Re: Resetting points/ stats to initial values

#3 Post by Iylae »

You're talking about a few different things so let me go into some specifics.

If you want to restore their hp to their maximum, do:

Code: Select all

$ profile.hp = profile.hpmax
If you want to restore their hp to their pre-battle value, do:

Code: Select all

$ profile.hp_before_battle = profile.hp
# Battle part goes here
"We rest up and restore our health and mana."
$ profile.hp = profile.hp_before_battle
If you want to restore 100 hp, then what you posted is fine, but it has the capacity to let players exceed their max hp, so use the following:

Code: Select all

$ profile.hp = min(profile.hp + 100, profile.hpmax)
# This sets hp to the smaller of (hp + 100) and hpmax
Finally, if you're doing this for all your characters, I'd suggest putting them in a list and running a loop. It's good practice and easier to read. It also won't cause problems when you're changing your party members, providing you update your list.

Code: Select all

$ party = [profile, player1, player2, player3, player4]
"We rest up and restore our health and mana."
python:
    for character in party:
        character.hp = character.hpmax
        character.mp = character.mpmax
Image
  If we are what we repeatedly do, then good coding is not an act, but a habit

User avatar
Kinmoku
Miko-Class Veteran
Posts: 591
Joined: Mon Aug 11, 2014 9:39 am
Completed: One Night Stand
Projects: VIDEOVERSE
Tumblr: gamesbykinmoku
itch: kinmoku
Location: Germany
Contact:

Re: Resetting points/ stats to initial values

#4 Post by Kinmoku »

philat wrote:Er... why not just player1.hp = 100?
Perfect! I knew there was a reason I couldn't find an answer to this (cause it's so obvious!) Lol, I'm dumb.
Iylae wrote:Finally, if you're doing this for all your characters, I'd suggest putting them in a list and running a loop. It's good practice and easier to read. It also won't cause problems when you're changing your party members, providing you update your list.
Thanks Lylae, this is useful to know. I'll add this to what I have :)

Post Reply

Who is online

Users browsing this forum: Dark79