Trouble with multiple characters using health/ stat bars

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: 560
Joined: Mon Aug 11, 2014 9:39 am
Completed: One Night Stand
Projects: Love IRL, Memories
Tumblr: gamesbykinmoku
itch: kinmoku
Location: Germany
Contact:

Trouble with multiple characters using health/ stat bars

#1 Post by Kinmoku » Tue Jan 19, 2016 8:54 am

Hi all,

I'm trying to add health/ MP/ stat bars to my game. So far so good, here's what I have for the main character's profile:

Code: Select all

init python: 

    show_profilestats=False

    def stats_overlay():               
        
        if show_profilestats:
            #ui.window(
                #xalign = 20,
                #ypos = 20,)
            ui.image("profile.png",
                xpos = 20,
                ypos = 15)
            ui.text("{color=#FFF}{size=-10}Kinmoku{/size}{/color}",
                xpos = 172,
                ypos = 52,)
            ui.vbox(xpos = 164,
                ypos = 95)
            ui.bar(max_hp, profile_hp, 
                style="profile_hp_bar")
            
            ui.close()
            
            ui.vbox(xpos = 164,
                ypos = 134)
            ui.bar(max_mp, profile_mp, 
                style="profile_mp_bar")
            
            ui.close()

    config.overlay_functions.append(stats_overlay)

init -2 python:
    #Health points
    profile_hp=100
    max_hp = 100

    #Mana points
    
    profile_mp=100
    max_mp = 100

init -5 python:
    #PROFILE HP BAR
    
    style.profile_hp_bar = Style(style.default)
    style.profile_hp_bar.xalign = 0.5
    style.profile_hp_bar.xmaximum = 279 # bar width
    style.profile_hp_bar.ymaximum = 36 # bar height
    
    style.profile_hp_bar.left_bar = Frame("profile_HPfull.png", 0, 0)
    style.profile_hp_bar.right_bar = Frame("profile_HPempty.png", 0, 0)
    
    #PROFILE MP BAR
    
    style.profile_mp_bar = Style(style.default)
    style.profile_mp_bar.xalign = 0.5
    style.profile_mp_bar.xmaximum = 263 # bar width
    style.profile_mp_bar.ymaximum = 36 # bar height
    
    style.profile_mp_bar.left_bar = Frame("profile_MPfull.png", 0, 0)
    style.profile_mp_bar.right_bar = Frame("profile_MPempty.png", 0, 0)

And in the script I have a test:

Code: Select all

$ show_profilestats=True

pause 1

$ profile_hp-=50

$ show_profilestats=True 

$ renpy.pause()

$ show_profilestats=False
And this works fine. But now I'm trying to add player 1, 2, 3 and 4 (to form a full party). I get that I can just copy and paste and edit the positions of what I have here 4 times, but player 1, 2, 3 and 4 will be different characters each time, or may not all be present.

E.G.

The first time you party, it will be You (profile), Nightshade (player1) and Celes (player2).
The next time you party, you will be with other characters: You (profile), Emarin (player1), Watto (player2) and Ivana (player3).

I understand I can choose not to show player 3 or 4 or whatever quite easily, but how do I define who player 1, 2, 3 and 4 are on a case by case basis? The reason for this is I'd like to customise each party member with their image and name. I also want to use the layout as I have in my design (see attached image) and not have awkward gaps because Nightshade (original player1) and Celes (original player2) are no longer present.

I hope I'm making sense! I'm really stuck on what to do next (I'm not a coder!) >_<
Attachments
party_stat_bars.jpg

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

Re: Trouble with multiple characters using health/ stat bars

#2 Post by Iylae » Tue Jan 19, 2016 9:20 am

This depends on how you store the character information.

You should probably be storing them as a class. If so, then you should have a list of your party which references the instances of your character class. To swap party members, you then just change where the references point in your party list.

in your example two party configurations:

Code: Select all

#1st party

$player1 = nightshade
$player2 = celes
$player3 = None

#things happen here with the first party

#2nd party

$player1 = emarin
$player2 = watto
$player3 = ivana
Then in your screen code, alter lines to pull the variables from player1, and not directly from the character

Code: Select all

ui.bar(emarin.max_mp, emarin.mp, style="profile_mp_bar") #wrong
ui.bar(player1.max_mp, player1.mp, style="profile_mp_bar") #right
This only works if you store your players as objects. (a class or a list)

Considering you say you're not a coder, it's best to ask you: How do you store e.g. nightshade's stats? If you store them as only variables, you cannot use this method without building them into a class first which I can show you.
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: 560
Joined: Mon Aug 11, 2014 9:39 am
Completed: One Night Stand
Projects: Love IRL, Memories
Tumblr: gamesbykinmoku
itch: kinmoku
Location: Germany
Contact:

Re: Trouble with multiple characters using health/ stat bars

#3 Post by Kinmoku » Tue Jan 19, 2016 9:36 am

That sounds like what I'm after, although I don't think I'm storing the character's information as a class. I haven't used classes yet (I don't think).

Stat changes would be done something like this:

Code: Select all

$ player2_hp-=50

nightshade "Oww!"

"You use a potion on Nightshade."

$ player2_hp+=50

nightshade "Thanks!"

That's what I had in mind, as there will be a narrative and not too many stat changes (I hope!).

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

Re: Trouble with multiple characters using health/ stat bars

#4 Post by Iylae » Tue Jan 19, 2016 9:49 am

So what you're doing at the moment is a very primitive but valid way of handling your characters, but notice that I used the . in my variables rather than an underscore. This is important.

There's a lot of tutorials out there on classes and variable types, but as you're using what seems a simple system, so I'll give you a commented snippit. IF you don't understand anything, let me know.

Code: Select all

init python:
    #create the player_character class
    class player_character:
        def __init__ (self, name, hp, mp): #repeat as necessary
            self.name = name
            self.hp = hp
            self.mp = mp
            #repeat as necessary
        
    #create an instance of the player_character class for nightshade

    
label start:
     
    # This sets the name to Nightshade, hp to 100, mp to 10.  Save it in the nightshade var
    nightshade = player_character("Nightshade",100,10)
    
    # This sets the name to Wattoo, hp to 50, mp to 20.  Save it in the wattoo var
    watto = player_character("Watto",50,20)
    
    #now to set up your party
    player1 = nightshade   

    #let's test some variables
    "[player1.name] has [player1.hp] and [player1.hp]"
    
    #now let's replace nightshade with watto
    $ player1 = watto
    
    #now let's test the party variables again
    "[player1.name] has [player1.hp] and [player1.hp]"
The reason why you should use this method, is it's a lot easier to read and you'll have no problems with removing a character temporarily then re-adding them without having to update all their info with just the single line player1 = watto
Last edited by Iylae on Tue Jan 19, 2016 10:13 am, edited 2 times in total.
Image
  If we are what we repeatedly do, then good coding is not an act, but a habit

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

Re: Trouble with multiple characters using health/ stat bars

#5 Post by philat » Tue Jan 19, 2016 10:10 am

This is generally the right idea, and will in fact largely obviate the need for player1, player2 anyway, as you'll probably want to append each character to a list and iterate through the list/use a list index. You'll obviously want to expand the Player_Character class as necessary -- for instance, giving it an image attribute for the profile page, a color attribute for the colors, etc. You would also want to switch to a screen rather than ui functions and use a for loop to show the player party, something like below.

Code: Select all

screen party_stats(player_party): # pass the screen the list containing the current members of the party
    for character in player_party:
        hbox:
            add character.image
            text character.name
            bar value character.current_hp range character.max_hp
            # and so forth
That said, for one thing, there are some vital colons missing in Iylae's code, which should probably be fixed. For another, when using classes, you need to be careful of where you initialize the instances -- defining classes and methods should go in init python but actually creating instances (by which I mean typing nightshade = Player_Character("Nightshade", 50, 50) ) should always be done after label start. (This is because of rollback/save/load compatibility.)

Lastly, this is all really python stuff more than renpy stuff, so it will probably be helpful if you study up on classes before you tackle it. I recommend codecademy's Python course -- I'm not affiliated with them in any way, but it's just a useful hands-on tool where you get to actually do shit directly in the browser and see if it works or not. Anyway, this was another recent thread that covered some of the basics of classes and referring to instances. http://lemmasoft.renai.us/forums/viewto ... =8&t=36487
Last edited by philat on Tue Jan 19, 2016 10:20 am, edited 3 times in total.

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

Re: Trouble with multiple characters using health/ stat bars

#6 Post by Iylae » Tue Jan 19, 2016 10:12 am

philat wrote:That said, for one thing, there are some vital colons missing in Iylae's code, which should probably be fixed. For another, when using classes, you need to be careful of where you initialize the instances -- defining classes and methods should go in init python but actually creating instances (by which I mean typing nightshade = Player_Character("Nightshade", 50, 50) ) should always be done after label start.
Thanks, I've updated my post with your observations.

I didn't really want to flood the self-proclaimed "not a coder" OP with lists on-top of objects, but I agree that's how I'd handle things.
Image
  If we are what we repeatedly do, then good coding is not an act, but a habit

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

Re: Trouble with multiple characters using health/ stat bars

#7 Post by philat » Tue Jan 19, 2016 10:19 am

Iylae wrote:
philat wrote:That said, for one thing, there are some vital colons missing in Iylae's code, which should probably be fixed. For another, when using classes, you need to be careful of where you initialize the instances -- defining classes and methods should go in init python but actually creating instances (by which I mean typing nightshade = Player_Character("Nightshade", 50, 50) ) should always be done after label start.
Thanks, I've updated my post with your observations.

I didn't really want to flood the self-proclaimed "not a coder" OP with lists on-top of objects, but I agree that's how I'd handle things.
Well, I would think lists are much easier to understand than classes, so if you're telling OP to use classes in the first place, might as well go for the whole thing. And given that the initial question was showing a dynamic party stat screen with a variable number of characters in it...

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

Re: Trouble with multiple characters using health/ stat bars

#8 Post by Iylae » Tue Jan 19, 2016 10:33 am

So you'd suggest a list of lists? Feel free to extrapolate with helpful code.

There's always someone better, so I don't mind seeing alternatives for my own knowledge.
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: 560
Joined: Mon Aug 11, 2014 9:39 am
Completed: One Night Stand
Projects: Love IRL, Memories
Tumblr: gamesbykinmoku
itch: kinmoku
Location: Germany
Contact:

Re: Trouble with multiple characters using health/ stat bars

#9 Post by Kinmoku » Tue Jan 19, 2016 10:36 am

Ok so I added a colon

Code: Select all

def __init__ (self, name, hp, mp):
...as it failed to launch. Anyway, it works, but I am unsure of how to get the ui.bar to link to the player class. Also, how do I have a max HP and current HP?

I have this at the moment but it doesn't do anything :(

Code: Select all

            ui.bar([player1.hp], 
                style="party_hp_bar")
philat wrote: You would also want to switch to a screen rather than ui functions and use a for loop to show the player party, something like below.

Code:
screen party_stats(player_party): # pass the screen the list containing the current members of the party
    for character in player_party:
        hbox:
            add character.image
            text character.name
            bar value character.current_hp range character.max_hp
            # and so forth
What are lists, loops and why is it better for me to use a screen? Sorry, I've not done anything like this yet!

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

Re: Trouble with multiple characters using health/ stat bars

#10 Post by Iylae » Tue Jan 19, 2016 10:38 am

Ah to explain the problem you're having. When outputting variables in text in renpy, it's wrapped in square brackets:

Code: Select all

"[player1.hp] is the hp of [player1.name]"
But when you're dealing with python variables, you don't use the square brackets:

Code: Select all

ui.bar(player1.hp, style="party_hp_bar")
Image
  If we are what we repeatedly do, then good coding is not an act, but a habit

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]