Resetting bar value to a default one?

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
fullmontis
Regular
Posts: 129
Joined: Sun May 05, 2013 8:03 am
Deviantart: fullmontis
itch: fullmontis
Location: Italy
Contact:

Resetting bar value to a default one?

#1 Post by fullmontis » Thu Dec 25, 2014 8:25 pm

This is hopefully a simple question :)

I have a bar that shows progress over time with an AnimatedValue. I want that value to reset to 0 when the player clicks a button, but I'm oblivious on how this could be done, since I don't know how to access the value of a bar and how to influence it...

Any help? Thanks!

User avatar
mjshi
Regular
Posts: 179
Joined: Wed Mar 13, 2013 9:55 pm
Completed: MazeSite01, Ponderings of Time
Contact:

Re: Resetting bar value to a default one?

#2 Post by mjshi » Thu Dec 25, 2014 9:16 pm

Assuming you're using a bar in screen language, shouldn't simply changing the value of the variable to 0 work?

Example:

Code: Select all

$ var = 0

screen thebar:
  bar value StaticValue(var, 100):
    xalign 1.0 yalign 0.0
    ## etc bar properties

label start:
  show screen thebar
  $ var += 100
  "Eileen" "The bar's value is at max."
 $ var = 0
  "Eileen" "Now, the bar is empty."
If you want to have the player reset the value upon clicking a button, try adding a textbutton.
Working demo:

Code: Select all

init:
  $ var = 100
# Declare characters used by this game.
define e = Character('Eileen', color="#c8ffc8")


screen textbutton_screen:
    vbox:
        textbutton "Wine" action SetVariable("var", 0)
        
# The game starts here.
label start:
  
  show screen textbutton_screen
  
  "Hey. [var]"
##Click the reset button here to change the var value
  "[var]"

  return
You can combine these two concepts to create what you asked for.

User avatar
fullmontis
Regular
Posts: 129
Joined: Sun May 05, 2013 8:03 am
Deviantart: fullmontis
itch: fullmontis
Location: Italy
Contact:

Re: Resetting bar value to a default one?

#3 Post by fullmontis » Fri Dec 26, 2014 7:31 am

Thank you for the detailed answer mjshi! I found your tutorial very useful in understand how bars work :D
mjshi wrote:Assuming you're using a bar in screen language, shouldn't simply changing the value of the variable to 0 work?
Not really because I need the value to be animated over time.

Since posting this I discovered I could use a DynamicDisplayable. The following code shows what I wanted to do:

Code: Select all

init python:
    showbar = False
    barvalue = 0.0

    def bar_load(st,at):
        global barvalue
        global showbar
        barvalue += 0.01
        if barvalue < 3 and showbar:
            return Bar(range=3, value=barvalue, thumb=None),0.01
        else:
            showbar = False
            return Null(),None

screen loadbar:
    frame:
        has vbox

        hbox:
            text "Click"
            button:
                action SetVariable("barvalue",0), SetVariable("showbar", True)
                text "Load"

        if showbar:
            add DynamicDisplayable(bar_load)

# The game starts here.
label start:
    show black
    call screen loadbar
    jump start
But I'm wondering if I'm reinventing the wheel here and if there is a simpler and more readable way of doing this...

User avatar
mjshi
Regular
Posts: 179
Joined: Wed Mar 13, 2013 9:55 pm
Completed: MazeSite01, Ponderings of Time
Contact:

Re: Resetting bar value to a default one?

#4 Post by mjshi » Fri Dec 26, 2014 12:44 pm

By "animated over time", do you mean that it will slide from its old value to its new value (like a decreasing HP bar)?

Otherwise, if the code works for you, it should be fine, right? ^_^
If there was a simpler way, you could still use the more complicated way that you created. Congrats~

User avatar
fullmontis
Regular
Posts: 129
Joined: Sun May 05, 2013 8:03 am
Deviantart: fullmontis
itch: fullmontis
Location: Italy
Contact:

Re: Resetting bar value to a default one?

#5 Post by fullmontis » Sat Dec 27, 2014 2:07 pm

mjshi wrote:By "animated over time", do you mean that it will slide from its old value to its new value (like a decreasing HP bar)?
Yep exactly. Sorry, I'm horrible at explaining myself.

I knew that AnimatedValue does exactly that. Problem is, I don't know how to change it once it is set.

For example, say a character lost 30 hp, from 50 to 20, and the bar slides down for that amount. Alright. But then, when the animation is finished, the value is stuck ah 20HP. I don't know how to change that value again if I need it.

In my example above I circumvent the problem by using a DynamicDisplayable that generates a new Bar based on the barvalue variable. I don't know if I'm overcomplicating it but hey, it works.

Hope I explained myself ^^

User avatar
mjshi
Regular
Posts: 179
Joined: Wed Mar 13, 2013 9:55 pm
Completed: MazeSite01, Ponderings of Time
Contact:

Re: Resetting bar value to a default one?

#6 Post by mjshi » Sat Dec 27, 2014 7:00 pm

You could always use that, but I think that yes, there's a simpler way. It's explained in my tutorial, but I'll paste it here :3
Minor edits denoted by ##
##The simpler way is by using Ren'py's "while" and "pause" statements.

The animation's logic is based on something like this: the while statement makes something loop over and over again until a condition is fulfilled (in this case, a variable increasing or decreasing by one until it equals the variable that is counting how many times the while statement has looped) , and the pause statement will give the visual effect of the bar seeming to 'slide' into place.

Step 1: Define a 'counting' variable (can be named anything, I named it "counting" to keep things simple) to keep track of how many times the while statement will repeat. Set it to zero right before the while statement every time you plan to use this sliding animation thing.
Step 2: Create a while statement, which is the basis for the sliding motion
Step 3: Add a "pause 0.01" to allow the player to view the bar in all of its sliding magnificence

## The less than or equal sign is the value you want the bar to "slide" to, for example 40 could be replaced by 100, 20, or even another variable.

The final code should look something like this:
Code:

Code: Select all

$ counting = 0

while counting < 40:
    $ variable += 1
    $ counting += 1
    pause 0.01

"Continue script here"
By using these two functions, we can ignore a lot of hassle and it will work perfectly fine xD If you feel that the animation is too fast, you can just change the pause 0.01 to pause 0.05 or 0.1 or something.

Here is a working demo. The scripts are completely accessible, so if you need examples you can open up the game's scripts.

Post Reply

Who is online

Users browsing this forum: No registered users