Can I set a slider to change multiple 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
noeinan
Eileen-Class Veteran
Posts: 1153
Joined: Sun Apr 04, 2010 10:10 pm
Projects: Ren'Py QuickStart, Crimson Rue
Organization: Statistically Unlikely Games
Deviantart: noeinan
Github: noeinan
Location: Washington State, USA
Contact:

Can I set a slider to change multiple values?

#1 Post by noeinan »

Hey! I'm making some custom sliders for my game's preferences and I am wanting to make it so the player can change percentages of a variable with more than two outcomes. Is it possible to set a slider bar to change more than one variable at a time, the second one inverse?

This is what I have right now:

Code: Select all

                    label _("NPCs who have red hair: [rchance]%")

                    bar value VariableValue("rchance", 100):
                        style "slider"
                        xsize 525

                    label _("NPCs who have brown hair: [bchance]%")

                    bar value VariableValue("bchance", 100):
                        style "slider"
                        xsize 525

                    label _("NPCs who have white hair: [wchance]%")

                    bar value VariableValue("wchance", 100):
                        style "slider"
                        xsize 525
This is what I want, but I'm not sure if it's possible and/or if I'm using the wrong syntax. Basically if you add to the rchance value, you subtract the same amount from the bchance value, etc.

Code: Select all

                    label _("NPCs who have red hair: [rchance]%")

                    bar value [VariableValue("rchance", 100), VariableValue("bchance", -100)]:
                        style "slider"
                        xsize 525

                    label _("NPCs who have brown hair: [bchance]%")

                    bar value [VariableValue("bchance", 100), VariableValue("wchance", -100)]:
                        style "slider"
                        xsize 525

                    label _("NPCs who have white hair: [wchance]%")

                    bar value [VariableValue("wchance", 100), VariableValue("bchance", -100)]:
                        style "slider"
                        xsize 525
Last edited by noeinan on Tue Apr 07, 2020 7:41 pm, edited 2 times in total.
Image

Image
Image

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Can I set a slider to change multiple values?

#2 Post by Alex »

Just a thoughts, 1) try to make a function that calculates value of var_2 as 100 - var_1, and use it for first slider, like

Code: Select all

bar value VariableValue("var_1", 100, action=this_function)
or

Code: Select all

bar value VariableValue("var_1", 100, action=SetVariable("var_2", 100-var_1))
2) try to put labels in a box (frame/vbox/hbox) and set its width to force moving text to a new line.

https://www.renpy.org/doc/html/screen_a ... bar-values
https://www.renpy.org/doc/html/screen_a ... l#Function

User avatar
noeinan
Eileen-Class Veteran
Posts: 1153
Joined: Sun Apr 04, 2010 10:10 pm
Projects: Ren'Py QuickStart, Crimson Rue
Organization: Statistically Unlikely Games
Deviantart: noeinan
Github: noeinan
Location: Washington State, USA
Contact:

Re: Can I set a slider to change multiple values?

#3 Post by noeinan »

Thanks for the quick response!

For the first solution, I tried setting action and got this:

Code: Select all

I'm sorry, but errors were detected in your script. Please correct the
errors listed below, and try again.

File "game/screens.rpy", line 1191: u'action' is not a keyword argument or valid child for the bar statement.
    bar value VariableValue("rchance", 100) action Call("rpercentage"):
It seems like you can't attach actions to bars and sliders? Unless I'm doing it wrong somehow (putting the action in quotations as you had it was giving me a different error too), this is what I have:

Code: Select all

bar value VariableValue("rchance", 100) action Call("rpercentage"):

Code: Select all

label rpercentage:
    $bchance = 100 - (rchance + wchance)
    return
For the second, thanks! I figured it out right after posting so I removed it. I tried using xmaximum on my vbox and it wasn't resizing anything, but when I tried xsize it suddenly worked.
Image

Image
Image

User avatar
noeinan
Eileen-Class Veteran
Posts: 1153
Joined: Sun Apr 04, 2010 10:10 pm
Projects: Ren'Py QuickStart, Crimson Rue
Organization: Statistically Unlikely Games
Deviantart: noeinan
Github: noeinan
Location: Washington State, USA
Contact:

Re: Can I set a slider to change multiple values?

#4 Post by noeinan »

Okay I *kinda* have some of this working, if I put the calculations at the start of the screen. Then if I exit the screen and re-enter it will recalculate the variables. The only problem is, of course, needing to exit and re-enter the screen and the fact that even though the variable changes, the slider position does not move. It just stays in the same spot you had it last time.

Code: Select all

screen g_preferences():

    tag menu

    $ bchance = 100 - (rchance + wchance)
    $ wchance = 100 - (rchance + bchance)

In another game I'd forced variables to refresh by making a label loop, but I can't use labels in this screen, since it's the game preferences before the game has started.


I tried forcing some kinda loop after some googling but only managed to totally crash RenPy. T_T

Code: Select all

default loop = True

Code: Select all

textbutton _("Game") action [ShowMenu("g_preferences"), ShowMenu("chanceloop")] style "labelbutton"

Code: Select all

screen chanceloop:

    python:
        while loop == True:
            bchance = 100 - (rchance + wchance)
            wchance = 100 - (rchance + bchance)
Last edited by noeinan on Tue Apr 07, 2020 6:50 pm, edited 3 times in total.
Image

Image
Image

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Can I set a slider to change multiple values?

#5 Post by Alex »

Just tested it...

Code: Select all

default var_1 = 50
default var_2 = 50
screen test_scr():
    vbox:
        align(0.5,0.05)
        bar value VariableValue("var_1", 100, action=SetVariable("var_2", 100-var_1)) xsize 100
        bar value VariableValue("var_2", 100, action=SetVariable("var_1", 100-var_2)) xsize 100

User avatar
noeinan
Eileen-Class Veteran
Posts: 1153
Joined: Sun Apr 04, 2010 10:10 pm
Projects: Ren'Py QuickStart, Crimson Rue
Organization: Statistically Unlikely Games
Deviantart: noeinan
Github: noeinan
Location: Washington State, USA
Contact:

Re: Can I set a slider to change multiple values?

#6 Post by noeinan »

I'm not getting an error message when I use SetVariable as the action (guess I had the wrong syntax before?) but it's also not changing the variables on the screen.

When I test it out, the label for the second bar doesn't change, and the slider also doesn't move to show the new value, even if I exit the Preferences screen and go back into it.

[Edit] Ah! I found a typo in my code, it's working now. Even updates without refreshing the screen! Thank you very much!

Code: Select all


screen g_preferences():

    tag menu

    use game_menu(_("Preferences"), scroll="viewport"):

        vbox:

            hbox:
                style_prefix "slider"
                box_wrap True
                spacing 100

                vbox:
                    xsize 550

                    label _("NPCs with red hair: [rchance]%")
                    bar value VariableValue("rchance", 100, action=SetVariable("bchance", 100 - (rchance + wchance))):
                        style "slider"
                        xsize 525

                    label _("NPCs with brown hair: [bchance]%")
                    bar value VariableValue("bchance", 100, action=SetVariable("wchance", 100 - (rchance + bchance))):
                        style "slider"
                        xsize 525

                    label _("NPCs with white hair: [wchance]%")
                    bar value VariableValue("wchance", 100, action=SetVariable("bchance", 100 - (rchance + wchance))):
                        style "slider"
                        xsize 525

Image

Image
Image

User avatar
noeinan
Eileen-Class Veteran
Posts: 1153
Joined: Sun Apr 04, 2010 10:10 pm
Projects: Ren'Py QuickStart, Crimson Rue
Organization: Statistically Unlikely Games
Deviantart: noeinan
Github: noeinan
Location: Washington State, USA
Contact:

Re: Can I set a slider to change multiple values?

#7 Post by noeinan »

Okay, so it's almost 100% working but not quite. Right now I'm running into two problems.

First, which I haven't figured out what is causing the problem yet, is that with this code setup, the very first number change does not apply the SetVariable action. This means all the numbers are 1 off, but I can't just add or subtract 1 because I don't know which direction it will be off by.

(Ex. if the player changes the top bar from 45 to 46, the second bar stays 45. When it again changes to 47, then the second bar changes to 44. This also applies going the other way! So if top bar is 45 to 44, bottom bar stays at 45, but once it drops again to 43 the second bar moves to 46.)

If the first time I move the bar is to randomly click, say to 73, then the difference will be huge instead of just 1! The math just ends up getting really messed up.

Second problem is that the bar goes into the negatives, which is not ideal when I want all three bars to add up to 100 and be >= 0. However, I looked around and found you can have an If statement to actions, so I can kind of fix this like so:

Code: Select all

screen g_preferences():

    tag menu

    use game_menu(_("Preferences"), scroll="viewport"):

        vbox:

            hbox:
                style_prefix "slider"
                box_wrap True
                spacing 100

                vbox:
                    xsize 550

                    label _("NPCs with red hair: [rchance]%")
                    bar value VariableValue("rchance", 100, action = If(bchance > 0, SetVariable("bchance", 100 - (rchance + wchance)), SetVariable("wchance", 100 - rchance))):
                        style "slider"
                        xsize 525

                    label _("NPCs with brown hair: [bchance]%")
                    bar value VariableValue("bchance", 100, action = If(wchance > 0, SetVariable("wchance", 100 - (rchance + bchance)), SetVariable("rchance", 100 - bchance))):
                        style "slider"
                        xsize 525

                    label _("NPCs with white hair: [wchance]%")
                    bar value VariableValue("wchance", 100, action = If(bchance > 0, SetVariable("bchance", 100 - (rchance + wchance)), SetVariable("rchance", 100 - wchance))):
                        style "slider"
                        xsize 525
Interestingly, after applying the above conditional statements, the numbers will still go into the negative based on the glitched math, so I can end up with -34 even though it's set to not change that variable unless it's greater than zero.

Thought about finding a function to force the result to be a natural number, but that might cause -1 to become 1, similarly screwing up the numbers? And I think I can only have one If/Else statement, no Elifs, so I don't feel confident I can make that work...
Image

Image
Image

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

Re: Can I set a slider to change multiple values?

#8 Post by philat »

Honestly with three variables, forget the screen, the math itself is finicky. For instance, in the working version of what you wrote (presumably), changing r only affects b until b is zero and then starts affecting w. If you were trying for a specific set of r/b/w numbers, getting that wouldn't be a setting, it'd be a damn puzzle. (For instance, you increase r, which reduces b. You then increase b, which reduces w, which is now zero. You then decrease b, which increases w. Etc. etc.) I'm not really a math person myself so mind, I'm not offering advice on how you SHOULD set it up -- that'd require more thinking than I can bother with. But honestly this seems like it would be easier if it were an incremental bar (+/- on each side) than a slider.

That said, the reason it doesn't work is because you're checking if the chance is larger than 0 before setting it rather than after. In other words, what you were trying to do (presumably) is the following:

Code: Select all

r = (set by bar)

b = 100 - (r + w)

if b < 0:
    b = 0

w = 100 - (r + b)
whereas what you wrote was

Code: Select all

r = (set by bar)

if b > 0: # whether or not b is larger than zero, it can then become a negative number.
    b = 100 - (r + w)

w = 100 - (r + b)

User avatar
noeinan
Eileen-Class Veteran
Posts: 1153
Joined: Sun Apr 04, 2010 10:10 pm
Projects: Ren'Py QuickStart, Crimson Rue
Organization: Statistically Unlikely Games
Deviantart: noeinan
Github: noeinan
Location: Washington State, USA
Contact:

Re: Can I set a slider to change multiple values?

#9 Post by noeinan »

Hmmmm, I think I kinda see what you're getting at.

What I really want is for all three bars to be linked so the sum of each of the three variables can only add up to 100, and all variables can only be 0 or a positive number.

It seems simple when I try to explain in, because I'm just trying to make it percentages, you know? But the math is pretty difficult to figure out in context to the code. :/
Image

Image
Image

User avatar
noeinan
Eileen-Class Veteran
Posts: 1153
Joined: Sun Apr 04, 2010 10:10 pm
Projects: Ren'Py QuickStart, Crimson Rue
Organization: Statistically Unlikely Games
Deviantart: noeinan
Github: noeinan
Location: Washington State, USA
Contact:

Re: Can I set a slider to change multiple values?

#10 Post by noeinan »

Okay, still working on this slowly, I think maybe I'll need to do some python programming instead of using the default sliders, though? Found this link which talks about the same issue, I'll need to come back to it when my brain is a bit more functional though.

https://softwareengineering.stackexchan ... ways-total
Image

Image
Image

Post Reply

Who is online

Users browsing this forum: No registered users