About Bars "Lifebars" and how they work... and a bit more.

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.
Message
Author
User avatar
mugenjohncel
Hentai Poofter
Posts: 2121
Joined: Sat Feb 04, 2006 11:13 pm
Organization: Studio Mugenjohncel
Location: Philippines
Contact:

About Bars "Lifebars" and how they work... and a bit more.

#1 Post by mugenjohncel »

This will be a long one...

Searched the whole Ren'py site for lifebars and/or other similar related stuff but almost always come up empty handed... so I'm going to ask questions regarding this mysterious ren'py function involving bars...

Question :?:
Let's say there are two guys (B1 and B2) arguing who gets to eat the cream pie lying on the floor...

Note: POV is B1
B1 "Whoa!... a Cream Pie!..."
B2 "It's delicious Cream Pie, I must eat it!..."
B1 "FUCK YOU!... I saw it first... therefore it's mine!"
B2 "NO U... I saw it first... so I say mine!"
Then, a fierce arguement started leading into...
B1 "FUCK this civilized arguement!... I say we use brute force!
B1 "That delicious CREAM PIE WILL BE MINE Dickhead!"
B2 "Oh yeah!?... Over my Dead Body..."
After this point there will be a scene change. From this point, it will be a turn-based slapfest...

Then a life bar (an image whose size is 10px tall 50px wide will appear on both side of the screen each bar is 20px away from upper and left side of the screen on the B1 side and 20px away from upper and right side of the screen on the B2 side).

Each side will initially be given 50 points of Life...

Then it will be B1's turn to slap... you are currently on "Label Offence"

He will have two choices...
- Slap
- Wait
Let's say B1 chooses Slap and ren'py will generate random number (1 to 10) and if Ren'py generates...
5 or lower
Slap will miss

6 or higher
Slap will hit
after that ren'py will again generate random number (this time 1 to 5)

and the number generated will be deducted to B2's life points

and the graphical lifebar will be shortened (Lets say the Hitpoint generated is 5, then the bar will be reduced by 5px horizontally... sorry, the way that lifebar works is still a bit of a mystery for me)

then ren'py will check B2's life points and if B2's life point is depleted (or zero) then you will go to "label Winner" or else go to "label Defence"

Let's say B2 is still alive and you will go to the "Label Defence"

You (B1) will have two choices...
- Dodge
- Block
Let's say you choose Dodge. Ren'py will generate (again) random number (this time 1 to 4)

and if the number is...

- 2 or lower
You will be hit. Renpy will generate random number (1 to 5) and the number generated will be deducted to your (B1's) life and then ren'py will check your (B1's) lifepoint and if B1's life point is depleted (or zero) then you will go to "label Loser" or else go to "label Offence"

- 3 or higher
you will successfully dodge the attack and get to choose how you will taunt B2 and a menu will pop up...
- "Hah! Try again!... Slowpoke!"
- "Thats it!?... that Creampie is as good as mine!"
- "Haha!... you missed!..."
You may choose whatever but after the colorful dialogue you will go to "label Offence"


now lets go back to the menu earlier...
- Dodge
- Block
Let's say you choose Block this time. Ren'py will generate (again) random number (this time 1 and 2)

and if the number is

- 1
You will block the hit and renpy will generate random number (1 to 2) and the number generated will be deducted to your (B1's) life and then ren'py will check your (B1's) lifepoint and if B1's life point is depleted (or zero) then you will go to "label Loser" or else go to "label Offence"

- 2
You will block the hit and have a chance to counter attack and renpy will generate random number (1 to 2) and the number generated will be deducted to your (B1's) life and then ren'py will check your (B1's) lifepoint and if B1's life point is depleted (or zero) then you will go to "label Loser" but if you (B1) still have life points left, you will go to "Label Counter" and there... a menu will pop-up

You will have 2 choices
- Counterattack
- Taunt
If you choose Counterattack:
Ren'py will generate random number (1 to 10) and if Ren'py generates...
5 or lower
Slap will miss

6 or higher
Slap will hit
after that ren'py will again generate random number (this time 1 to 5)

and the number generated will be deducted to B2's life points

then ren'py will check B2's life points and if B2's life point is depleted (or zero) then you will go to "label Winner" or else go to "label Offence"... and from there the vicious cycle repeats once again.

If you choose Taunt:

a menu will pop up...
- "Lame!..."
- "Is that it?... "
- "You limp dick!"
You may choose whatever but after the colorful dialogue you will go to "label Offence"

OK, what I written above is a glimpse on how my supposedly fighting engine works... it's inneficient and very much hard to track (and debug...) but this is the only way I know it (a very buggy version already exist). I can expand it to accomodate other things but for now I'll keep it simple since I still have to figure out how that "Lifebar" works... also if there is a much more efficient way to reproduce the above behaviour please point it out.

Thanks in advance.

"POOF" (Nope!... not going to dissapear, will wait...)

monele
Lemma-Class Veteran
Posts: 4101
Joined: Sat Oct 08, 2005 7:57 am
Location: France
Contact:

Re: About Bars "Lifebars" and how they work... and a bit more.

#2 Post by monele »

I'm not sure that's what you're asking but ui.bar() seems like the solution to display a... bar :).

Code: Select all

$ ui.bar(width, height, maxvalue, currentvalue)
All of these values are numbers. Now, to have the bar look different from the regular Ren'Py bar, you'd have to copy & modify the regular bar style into something new. Haven't really looked into this though.

Tayruh
Regular
Posts: 36
Joined: Tue Sep 04, 2007 1:34 am
Contact:

Re: About Bars "Lifebars" and how they work... and a bit more.

#3 Post by Tayruh »

Edit:
In case you saw my previous code post, I changed the code so that the HP bars are displayed in an overlay so that you don't need to keep redrawing them. Much, much slicker than before.

Code: Select all

init:
    $ e = Character('Eileen', color="#c8ffc8")
    # Define HP
    $ p1 = 50
    $ p2 = 50
    
    python:
        # Define the hit point display
        def hp_overlay():
            ui.at(Position(xpos=100, ypos=10, xanchor='left', yanchor='top'))
            ui.bar(200, 50, 50, p1)
            ui.at(Position(xpos=500, ypos=10, xanchor='left', yanchor='top'))
            ui.bar(200, 50, 50, p2)

label start:
    e "Let the battle begin!"
    # Start the battle
    call battle
    # End the game
    jump end   


# Initialize battle
label battle:
    # Enable the HP display
    $ config.overlay_functions.append(hp_overlay)
    # Start battle loop
    call offense
    # Disable the HP display
    $ config.overlay_functions.remove(hp_overlay)
    return

# Battle loop
label offense:
    menu:
        "Slap":
            if renpy.random.randint(1, 10) <= 5:
                jump dodge
            jump hit
        "Wait":
            "You do nothing."
            jump offense
    
label hit:
    $ p2 -= renpy.random.randint(1, 5)
    if p2 <= 0:
        jump win
    #fall through

label dodge:
    menu:
        "Dodge":
            if renpy.random.randint(1, 10) <= 2:
                $ p1 -= renpy.random.randint(1, 5)
                if p1 <= 0:
                    jump lose
                jump offense
            menu:
                "Hah! Try again!... Slowpoke!":
                    e "Hah! Try again!... Slowpoke!"
                "Thats it!?... that Creampie is as good as mine!":
                    e "Thats it!?... that Creampie is as good as mine!"
                "Haha!... you missed!...":
                    e "Haha!... you missed!..."
            jump offense
        "Block":
            $ p1 -= renpy.random.randint(1, 2)
            if p1 <= 0:
                jump lose
            if renpy.random.randint(1, 2) == 1:
                jump offense
            #fall through

label counter:
    menu:
        "Counterattack":
            if renpy.random.randint(1, 10) >= 6:
                $ p2 -= renpy.random.randint(1, 5)
                if p2 <= 0:
                    jump win
        "Taunt":
            menu:
                "Lame!...":
                    e "Lame!..."
                "Is that it?... ":
                    e "Is that it?... "
                "You limp dick!":
                    e "You limp dick!"
    jump offense


# You win
label win:
    e "Take that, fucker!"
    "Game Over -- YOU WIN! :D"
    return

# You lose
label lose:
    e "Amagad, I'm ded!"
    "Game Over -- YOU LOSE! :("
    return 


label end:
    "Thanks for playing!"
    # The end

User avatar
mugenjohncel
Hentai Poofter
Posts: 2121
Joined: Sat Feb 04, 2006 11:13 pm
Organization: Studio Mugenjohncel
Location: Philippines
Contact:

Re: About Bars "Lifebars" and how they work... and a bit more.

#4 Post by mugenjohncel »

Thanks Tayruh... this is exactly what I need!
Unfortunatelly, I don't posses what some people call a "Coder's Mindset"... I know how it should work, I know how it should be but when the time for it to be written into a workable code I always slam into a brick wall.

Umm... one more question, how do you make the bars dissapear after the battle? (Say after the winner/loser label) and will go to another label...

...also, is it possible to define your own HP bar, I mean is it possible for me to use a .PNG image as a substitute for a bar? If it's possible, please teach me how...

Thanks in advance...

Tayruh
Regular
Posts: 36
Joined: Tue Sep 04, 2007 1:34 am
Contact:

Re: About Bars "Lifebars" and how they work... and a bit more.

#5 Post by Tayruh »

how do you make the bars dissapear after the battle?
I included that in the code so you could see how. I even commented it. See the in 'battle' that says:

Code: Select all

    # Disable the HP display
    $ config.overlay_functions.remove(hp_overlay)
That's all you need to hide them. By default, I have them show at the start of battle and then hide at the end.

If you notice in 'start', it calls 'battle'. After the battle ends, the code continues down and jumps to the 'end' label. Notice the line in the 'end' label doesn't have the HP bars displayed. That's because they're hidden (removed) after the battle ends.


Edit:
is it possible to define your own HP bar
I'm a bit new to renpy right now. Give me a bit and I'll see if I can figure out a way. I'm sure it's possible, and most likely pretty easy to do.

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: About Bars "Lifebars" and how they work... and a bit more.

#6 Post by PyTom »

Actually, you can't change config.overlay_functions (or most other config variables) during gameplay. It won't work correctly over saving and loading. The right way to do this is:

Code: Select all

init:
    python:
         show_hp_overlay = False

         def hp_overlay():
              if not show_hp_overlay:
                  return

              # ...
You can then set show_hp_overlay to True or False to cause the overlay to be shown or hidden, respectively.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

Tayruh
Regular
Posts: 36
Joined: Tue Sep 04, 2007 1:34 am
Contact:

Re: About Bars "Lifebars" and how they work... and a bit more.

#7 Post by Tayruh »

Oh? You're not allowed to add/remove them from the config.overlay_functions list as you wish? Hmm.. Granted, your way is simpler, but I thought the other way would just work.

User avatar
mugenjohncel
Hentai Poofter
Posts: 2121
Joined: Sat Feb 04, 2006 11:13 pm
Organization: Studio Mugenjohncel
Location: Philippines
Contact:

Re: About Bars "Lifebars" and how they work... and a bit more.

#8 Post by mugenjohncel »

Tayruh wrote:I included that in the code so you could see how. I even commented it.
Umm... sorry about that... It's just I'm currently excited and simply copy/pasted it into my work and changed a few things... so I kinda overlooked that one a bit...

Pytom wrote:

Code: Select all

init:
    python:
         show_hp_overlay = False

         def hp_overlay():
              if not show_hp_overlay:
                  return

              # ...
I'm currently seeing traceback's right now... err... how should I write the code Tayruh wrote earlier and combine it with this one?...

Oh SHIT!... I actually forgot to "POOF" in my earlier post... (It's a tradition)

Oh well...

"POOF" (Not going to dissapear... goes into lurking mode)

Tayruh
Regular
Posts: 36
Joined: Tue Sep 04, 2007 1:34 am
Contact:

Re: About Bars "Lifebars" and how they work... and a bit more.

#9 Post by Tayruh »

how should I write the code Tayruh wrote earlier and combine it with this one?
Change 'init' to this:

Code: Select all

init:
    python:
        # Define the hit point display
        def hp_overlay():
            if not show_hp:
                return
            ui.at(Position(xpos=100, ypos=10, xanchor='left', yanchor='top'))
            ui.bar(200, 50, 50, p1)
            ui.at(Position(xpos=500, ypos=10, xanchor='left', yanchor='top'))
            ui.bar(200, 50, 50, p2)    
    
    $ e = Character('Eileen', color="#c8ffc8")
    # Define HP
    $ p1 = 50
    $ p2 = 50
    $ show_hp = False
    $ config.overlay_functions.append(hp_overlay)
And change 'battle' to this:

Code: Select all

# Initialize battle
label battle:
    # Enable the HP display
    $ show_hp = True
    # Start battle loop
    call offense
    # Disable the HP display
    $ show_hp = False
    return
That's it.

Edit:
Btw, PyTom is right. Using his way, saving the game mid-battle and reloading it has the HP bars and everything reappear correctly. My previous way didn't do that. The game would pick up in the middle of battle, but the bars would be missing.

In other words, stick with this way of doing it, since it works the way it should.

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: About Bars "Lifebars" and how they work... and a bit more.

#10 Post by PyTom »

Basically, config variables and styles aren't saved, and so they shouldn't be changed outside of init blocks.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
mugenjohncel
Hentai Poofter
Posts: 2121
Joined: Sat Feb 04, 2006 11:13 pm
Organization: Studio Mugenjohncel
Location: Philippines
Contact:

Re: About Bars "Lifebars" and how they work... and a bit more.

#11 Post by mugenjohncel »

Thanks for the fast reply... now in another "unrelated question" (I know not everyone is going to agree to this)

How do I disable rollback and saving while were in combat?

I just wanted to prevent players to abuse rollback and "cheat" their way into this sequence...

IIRC, someone used rollback on Magical Boutique to Rollback a bit if things didn't go quite well...

OK, ok... don't treat players like the enemy... I've heard that before but still...

"POOF" (Waiting...)

Tayruh
Regular
Posts: 36
Joined: Tue Sep 04, 2007 1:34 am
Contact:

Re: About Bars "Lifebars" and how they work... and a bit more.

#12 Post by Tayruh »

@ PyTom

Where does the game come back in on a reload? I'm assuming it doesn't hit init every time. Well.. maybe it does.. but then it must reset all of the variables after init, right?

Basically what I was thinking is, for a hack we could set/clear a "show_hp" flag whenever we add/remove the hp_overlay function. This way, when the game is reloaded, we could check the the show_hp flag and add the hp_overlay function to the list if the flag is set.

This hack is by no means necessary for most games. But it would be handy to avoid something like JQuartz was saying earlier.

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: About Bars "Lifebars" and how they work... and a bit more.

#13 Post by PyTom »

Tayruh wrote:Where does the game come back in on a reload? I'm assuming it doesn't hit init every time. Well.. maybe it does.. but then it must reset all of the variables after init, right?
It doesn't re-run init. IIRC, it makes a copy of the variables at the end of init, stores them, and replaces the store with those variables when the game is restarted. But this is a shallow copy... so while the value of config will be reset, the value of config.overlay_functions won't be.
Basically what I was thinking is, for a hack we could set/clear a "show_hp" flag whenever we add/remove the hp_overlay function. This way, when the game is reloaded, we could check the the show_hp flag and add the hp_overlay function to the list if the flag is set.
Using a flag in the overlay function is the recommended way to do this, as it avoids updating the config variables entirely.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

Tayruh
Regular
Posts: 36
Joined: Tue Sep 04, 2007 1:34 am
Contact:

Re: About Bars "Lifebars" and how they work... and a bit more.

#14 Post by Tayruh »

Yeah, I understand. It was just a thought. But since it doesn't return to a specific function on loading, there's no way to impliment the hack like I wanted to anyway.

How do I disable rollback and saving while were in combat?
Rollback can be disabled with:

Code: Select all

$ config.rollback_enabled = False
As for disabling save.. I really don't know. I did hunt for a way, but I couldn't find one that was obvious. Maybe someone else can help you with that. Sorry. :/

JQuartz
Eileen-Class Veteran
Posts: 1265
Joined: Fri Aug 31, 2007 7:02 am
Projects: 0 completed game. Still haven't made any meaningfully completed games...
Contact:

Re: About Bars "Lifebars" and how they work... and a bit more.

#15 Post by JQuartz »

Tayruh wrote:But it would be handy to avoid something like JQuartz was saying earlier.
This is what i said earlier.
Evaluating 30+ ifs everytime an interaction happens would cause serious lag.
mugenjohncel wrote:How do I disable...saving while were in combat?
One way is to disable the right click altogether. But that means you can't load either (unless you use overlay save and load in the cookbooks, written by rocket, i believe)
The other way almost impractical way is to code the whole save and load system so that it won't allow saving when you're in battle. Maybe there is other ways but I don't think i'll try to find out since i'll be using both of these alternatives.
mugenjohncel wrote:...also, is it possible to define your own HP bar, I mean is it possible for me to use a .PNG image as a substitute for a bar? If it's possible, please teach me how...
Well can try this. You need 2 bar image, 1 for fully empty and 1 for fully filled. then add the properties left_bar and right_bar to the ui.bars like so:

Code: Select all

ui.bar(200, 50, 50, p1, left_bar=Frame('fully filled.png', 1,1), right_bar=Frame('fully empty.png',1,1))
You can get more information about Frame here:http://www.renpy.org/wiki/renpy/doc/ref ... ions/Frame
about bar properties here:http://www.renpy.org/wiki/renpy/doc/ref ... Properties
and ui.bars here:http://www.renpy.org/wiki/renpy/doc/ref ... ons/ui.bar
I suspect somebody is stealing my internet identity so don't believe everything I tell you via messages. I don't post or send messages anymore so don't believe anything I tell you via messages or posts.

Post Reply

Who is online

Users browsing this forum: Red-Baby