Page 1 of 1

BarValue (adjustment and animation)

Posted: Sun Mar 08, 2015 5:42 pm
by trooper6
Hello All,

So I've been doing an exploration of bars in order to answer NaNaNaNoo's question about bars as fully as possible. I've been trying to utilize all the Bar Variants in the documentation:
The basic bar screen language doc: http://www.renpy.org/doc/html/screens.html#bar

Using the different BarValues as my value object, especially the AnimatedValue: http://www.renpy.org/doc/html/screen_ac ... tml#values

Making Bars that can adjust a value using ui.adjustment:http://www.renpy.org/doc/html/screen_py ... adjustment

Then I checked to see if I could create a basic BarValue object and have that work: http://www.renpy.org/doc/html/screen_py ... #barvalues

I've got everything working so far. And note, I will post the results of my experimentation when I'm completely finished with it.

But I'm stuck at something. According to the BarValue documentation, I should be able to use periodic to make it animate value changes just like the AnimatedValue. BarValue also can take an adjustment. So, I figured I could create a BarValue that would use an adjustment and that would use animations. This I'm having trouble with. Any help?

So here is my version of using BarValue without Animation that does work:

Code: Select all

screen barvaluebar():
    vbox:
        xmaximum 300
        bar value bhvaluebar
        text "HP: [hp]/[max_hp]" 
        
init -1 python:
    def updateHP(variable):
        global hp
        hp = variable
        renpy.restart_interaction()
        
    class HPValue(BarValue):
        def __init__(self, value=0.0, range=1.0):
            self.value = value
            self.range = range
            
        def get_adjustment(self):
            return ui.adjustment(value=hp, range=max_hp, changed=updateHP)
            
# The game starts here.
label start:
    $max_hp = 100
    $hp = 50
    $bhvaluebar = HPValue(value=hp, range=max_hp)
    
    scene black
    
    "This is going to be an exploration of Bars."
    show screen barvaluebar()
    "Now we do an adjustable value bar using BarValue."
    "Try it out."
    $hp += 25
    "Adding 25."
    $hp -= 25
    "Subtracting 25."
    hide screen barvaluebar 
    return
So all of that works. But I want to use the periodic function to animate it when I add or subtract from the variable. I tried just copying the AnimatedValue class with a few customizations, but while it adjusts the variables and adds to/subtracts from the variables properly...it doesn't animated the changes.

Code: Select all

screen barvaluebar2():
    vbox:
        xmaximum 300
        bar value bhvaluebar2
        text "HP: [hp]/[max_hp]" 
        
init -1 python:
    def updateHP(variable):
        global hp
        hp = variable
        renpy.restart_interaction()
            
    class HPValue2(BarValue):
        def __init__(self, value=0.0, range=1.0, delay=1.0, old_value=None):
            if old_value == None:
                old_value = value

            self.value = value
            self.range = range
            self.delay = delay
            self.old_value = old_value
            self.start_time = None

            self.adjustment = None

        def get_adjustment(self):
            self.adjustment = ui.adjustment(value=hp, range=max_hp, changed=updateHP)
            return self.adjustment

        def periodic(self, st):

            if self.start_time is None:
                self.start_time = st

            if self.value == self.old_value:
                return

            fraction = (st - self.start_time) / self.delay
            fraction = min(1.0, fraction)

            value = self.old_value + fraction * (self.value - self.old_value)

            self.adjustment.updateHP(value)

            if fraction != 1.0:
                return 0

        def replaces(self, other):

            if not isinstance(other, AnimatedValue):
                return

            if self.value == other.value:
                self.start_time = other.start_time
                self.old_value = other.old_value
            else:
                self.old_value = other.value
                self.start_time = None
                
            
# The game starts here.
label start:
    $max_hp = 100
    $hp = 50
    $hpadjust1 = ui.adjustment(value=hp, range=max_hp, changed=updateHP)
    $bhvaluebar2 = HPValue2(value=hp, range=max_hp, delay=2.0)
    
    scene black
    
    "This is going to be an exploration of Bars."
    show screen barvaluebar2()
    "Now we do an adjustable value bar using BarValue with animation."
    "Try it out."
    $hp += 25
    "Adding 25."
    $hp -= 25
    "Subtracting 25."
    hide screen barvaluebar2
 

    return
So any idea how to use BarValue to include both adjustable and animation?

Help will be included in my exploration that I will eventually publish in the cookbook!

Re: BarValue (adjustment and animation)

Posted: Tue Mar 10, 2015 11:59 am
by trooper6
Any expert Ren'py coders have an idea about this?

Re: BarValue (adjustment and animation)

Posted: Fri Mar 13, 2015 4:14 pm
by trooper6
I'd like to bump this again, because I don't want to make a cookbook without understanding this element.

Re: BarValue (adjustment and animation)

Posted: Fri Mar 13, 2015 4:17 pm
by xela
Lets take a look :D

===
Just read yours posts, I've never really used barvalue before but I see no reason for animated value to fail, do you need barvalue debugged? Or just get something to work right?

Re: BarValue (adjustment and animation)

Posted: Fri Mar 13, 2015 4:51 pm
by xela
This works perfectly:

Code: Select all

screen barvaluebar2():
    vbox:
        xmaximum 300
        bar value AnimatedValue(value=hp, range=max_hp, delay=4.0)
        text "HP: [hp]/[max_hp]"
           
label start:
    $ hp = 50
    $ max_hp = 100
   
    scene black
    
    show screen barvaluebar2
    "Now we do an adjustable value bar using animation."
    "Try it out."
    $ hp += 25
    "Adding 25."
    $ hp -= 25
    "Subtracting 25."
    hide screen barvaluebar2
    return
It behaves like it supposed to, I am not sure that I am following the issue in your post.

Re: BarValue (adjustment and animation)

Posted: Fri Mar 13, 2015 4:58 pm
by trooper6
So let me show you the entire experiment--that works progressively so I can later explain it to new folks. In the experiment I go through each different version of the bar, using each element laid out in the documentation (Basic, pre-made Values, ui.adjustment, custom BarValue). I've been able to get 5 of the 7 options I've been working on to function. But I've not been able to combine adjustment and animation in the same bar.

So if you want to see the entire experiment (going through each of the different versions of the bar--not yet with custom graphics) you might see better what I'm talking about:

The order of the bars are:
1) Just Text, no bar
2) Basic Bar (no adjust, no animation)
3) Animated Bar (uses AnimatedValue, no animation)
4) Adjustable Bar (uses ui.adjustment, no animation)
*5) This was me trying to add both a value (Animated Value) and an adjustment to a bar with two values. This didn't work. It is doable?
6) Me trying out custom BarValue, to make a bar that is adjustable. Works.
*7) Trying to create a custom BarValue that is both adjustable and animated (the adjustment works, the animation doesn't).

So #5 and #7 aren't woking as a means to combine both animation and adjustment in the same bar.

Here's the code:

Code: Select all

screen textbar ():
    text "HP: [hp]/[max_hp]" 
    
screen basicbar1():
    vbox:
        xmaximum 300
        #ymaximum 300
        bar value hp range max_hp
        #vbar value hp range max_hp
        text "HP: [hp]/[max_hp]" 
        
screen animvalbar():
    vbox:
        xmaximum 300
        bar value AnimatedValue(value=hp, range=max_hp, delay=2.0)
        text "HP: [hp]/[max_hp]" 
        
screen adjustbar1():
    vbox:
        xmaximum 300
        bar adjustment hpadjust1
        text "HP: [hp]/[max_hp]" 

screen adjustbar2(): #This doesn't work
    vbox:
        xmaximum 300
        bar value AnimatedValue(value=hp, range=max_hp, delay=2.0) adjustment hpadjust1
        text "HP: [hp]/[max_hp]" 
        
screen barvaluebar():
    vbox:
        xmaximum 300
        bar value bhvaluebar
        text "HP: [hp]/[max_hp]" 
        
screen barvaluebar2():
    vbox:
        xmaximum 300
        bar value bhvaluebar2
        text "HP: [hp]/[max_hp]" 
        
init -1 python:
    def updateHP(variable):
        global hp
        hp = variable
        renpy.restart_interaction()
        
    class HPValue(BarValue):
        def __init__(self, value=0.0, range=1.0):
            self.value = value
            self.range = range
            
        def get_adjustment(self):
            return ui.adjustment(value=hp, range=max_hp, changed=updateHP)
            
    class HPValue2(BarValue):
        def __init__(self, value=0.0, range=1.0, delay=1.0, old_value=None):
            if old_value == None:
                old_value = value

            self.value = value
            self.range = range
            self.delay = delay
            self.old_value = old_value
            self.start_time = None

            self.adjustment = None

        def get_adjustment(self):
            self.adjustment = ui.adjustment(value=hp, range=max_hp, changed=updateHP)
            return self.adjustment

        def periodic(self, st):

            if self.start_time is None:
                self.start_time = st

            if self.value == self.old_value:
                return

            fraction = (st - self.start_time) / self.delay
            fraction = min(1.0, fraction)

            value = self.old_value + fraction * (self.value - self.old_value)

            self.adjustment.updateHP(value)

            if fraction != 1.0:
                return 0

        def replaces(self, other):

            if not isinstance(other, AnimatedValue):
                return

            if self.value == other.value:
                self.start_time = other.start_time
                self.old_value = other.old_value
            else:
                self.old_value = other.value
                self.start_time = None
                
label splashscreen:
    scene black
    $renpy.pause(2)
    
    show text "AmatureManga Productions Presents\n a story by: AmatureManga and Art by: SilverKazeNinja" with dissolve
    $renpy.pause(2)
    
    hide text with dissolve
    $renpy.pause(2)
    
    return
        
        
# The game starts here.
label start:
    $max_hp = 100
    $hp = 50
    $hpadjust1 = ui.adjustment(value=hp, range=max_hp, changed=updateHP)
    $bhvaluebar = HPValue(value=hp, range=max_hp)
    $bhvaluebar2 = HPValue2(value=hp, range=max_hp, delay=2.0)
    
    scene black
    
    "This is going to be an exploration of Bars."
    show screen textbar()
    "We begin with the text bar."    
    $hp += 25
    "Adding 25."
    $hp -= 25
    "Subtracting 25."
    hide screen textbar

    show screen basicbar1()
    "Now we do the basic bar."
    $hp += 25
    "Adding 25."
    $hp -= 25
    "Subtracting 25."
    hide screen basicbar1  

    show screen animvalbar()
    "Now we do the animated value bar."
    $hp += 25
    "Adding 25."
    $hp -= 25
    "Subtracting 25."
    hide screen animvalbar  
    
    show screen adjustbar1()
    "Now we do the adjustable value bar."
    "Try it out."
    $hp += 25
    "Adding 25."
    $hp -= 25
    "Subtracting 25. As you see, this manual adding doesn't work...so I think I need a BarValue here."
    hide screen adjustbar1  
    
    show screen barvaluebar()
    "Now we do an adjustable value bar using BarValue."
    "Try it out."
    $hp += 25
    "Adding 25."
    $hp -= 25
    "Subtracting 25."
    hide screen barvaluebar  
    
    show screen barvaluebar2()
    "Now we do an adjustable value bar using BarValue with animation."
    "Try it out."
    $hp += 25
    "Adding 25."
    $hp -= 25
    "Subtracting 25."
    hide screen barvaluebar2
    
    e "Once you add a story, pictures, and music, you can release it to the world!"

    return

Re: BarValue (adjustment and animation)

Posted: Fri Mar 13, 2015 5:07 pm
by xela
Right... so you want the animated value to "catch up" to what you've adjusted. This is prolly tricky but I'll take a look, never tried this before...

===
Going to try and figure this out, it's Friday night witch doesn't help since it's not a workday tomorrow and I am a bit drunk but I am not falling a sleep before this is solved or nature takes over :D

Re: BarValue (adjustment and animation)

Posted: Fri Mar 13, 2015 7:14 pm
by xela
Oki... so after taking a look at the your code and Ren'Py codebase, you're asking for a new feature which merit is debatebale at best. I do not believe that there is a simple solution and a cross between two classes is required. This is not something Ren'Py provides out of box so you can either create a cookbook entry without this or I (will take me few hours of trial and error) or someone else (PyTom can prolly mange this in 5 min) will fix this at some point. I know what is required but I need a good night of rest before anything can be done about it.

I cannot really see a merit in your request other than curiosity which killed the cat :), there is very little point in pursuing this. At it's best it will end up looking like engine cannot keep up with the user input :(

===
PS: Since this is one of the things that I cannot find absolutely no real application for, I'll rather focus on things that do have merit ;) maybe someone else will find time to figure this out as a simple exercise.

Re: BarValue (adjustment and animation)

Posted: Sat Mar 14, 2015 1:22 pm
by trooper6
I think I'm just curious (there goes that cat) because the documentation says you can create animation with your custom BarValue, and I can't seem to get that to work. But also, if you can't combine animation with adjust...what exactly is the point of using a custom BarValue?

PyTom provides pre-made Values that are great...I think I'm having a failure of imagination of what I'd want the custom BarValue for...and I feel like I should really understand all the elements related to bars.

Re: BarValue (adjustment and animation)

Posted: Sat Mar 14, 2015 1:43 pm
by xela
I think that it is perfectly doable, it's just not something useful.

Re: BarValue (adjustment and animation)

Posted: Sat Mar 14, 2015 2:15 pm
by trooper6
Note, I don't want there to be animation when I adjust the bar with the mouse but when I add to the variable in the text.

Re: BarValue (adjustment and animation)

Posted: Sat Mar 14, 2015 3:30 pm
by xela
Noted, that just complicates the task a bit. In any case, good luck :)

I still think it's a bloody waste of time. I've never seen one game or application or can think of a use for it. Just because it can be done, doesn't mean that it should...