Sliders and bindings

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
Yuuji
Regular
Posts: 96
Joined: Tue Jan 20, 2015 3:08 am
Location: Russia, Perm
Contact:

Sliders and bindings

#1 Post by Yuuji »

Hello! I've got a problem again. :( I'm trying to understand how can I bind slider value to persistent variable or channel volume.
For now my code for bar is:

Code: Select all

    bar xpos 360 ypos 79 value MixerValue('bgm') style "pref_slider" left_gutter 0 right_gutter 0

    style pref_slider:
        xmaximum 118
        ymaximum 28
        right_bar "image/slider_empty.png"
        left_bar "image/slider_full.png"
        thumb "image/slider.png"
        thumb_offset 11
I want to control volume of MY channel through this bar. Is there a way to do it? Thanks in advance. :)

User avatar
shivanshs9
Regular
Posts: 54
Joined: Sun Jul 20, 2014 1:59 pm
Projects: The Destiny(http://thedestiny-cxz.blogspot.com)
Organization: Cyber-X-Zone
Location: India
Contact:

Re: Sliders and bindings

#2 Post by shivanshs9 »

Well, basically, it's practically quite easy even though there's not much theoritical information about it... At least none of what I could find... But, this is the method I used for my Music Room...

Code: Select all

init python:
    # Creates a new music channel, named "bgm", which uses "bgm" mixer...
    renpy.music.register_channel("bgm", mixer="bgm", loop=True)
    # Creates a MusicRoom instance, using the just-defined music channel!
    mr = MusicRoom(channel="bgm")

    # This function is based on 00preferences.rpy in renpy/common directory!
    def set_bgm_volume(value=None):
        if value is None:
            return MixerValue('bgm')
        else:
            return SetMixer('bgm', value)

screen music_room:
    # Buttons and more...
    vbar value set_bgm_volume()
Now, for the explanation part!
Function used to register a new music channel: renpy.music.register_channel(name, mixer=None, loop=None, stop_on_mute=True, tight=False, file_prefix='', file_suffix='', buffer_queue=True)(source)
I named the custom channel as "bgm" and used another custom mixer, which is also called "bgm"...

Then, I created my own function, set_bgm_volume(), which does all the volume adjustment work basically, since now you can just use this function as-it-is in the bar after value, like

Code: Select all

bar value set_bgm_volume()
If you encounter any difficulties, feel free to ask right away!
"Destiny is a no matter of chance
It is a matter of choice
It is not a thing to be waited for
It is a thing to be achieved..."

-William Jennings Bryan
If you can dream and not make dreams your master;
If you can think and not make thoughts your aim,
If you can meet with Triumph and Disaster;
And treat those two impostors just the same,
Only then can you ever win against yourself...

User avatar
Yuuji
Regular
Posts: 96
Joined: Tue Jan 20, 2015 3:08 am
Location: Russia, Perm
Contact:

Re: Sliders and bindings

#3 Post by Yuuji »

Thank you for your answer! But it doesn't seem to work... I can post my code, maybe you can give me a hint, where I've mistaken?
That's the code in script.rpy(registering bgm channel):

Code: Select all

init:
    $renpy.music.register_channel('bgm', loop = True, file_prefix = 'bgm/bgm', file_suffix = '.ogg')
Now, title screen code:

Code: Select all

screen main_menu():
    tag menu

    //some buttons description here...

    on "show" action Function(playBGM, "32")
playBGM function:

Code: Select all

    def playBGM(file):
        persistent.currentBGM = file
        if persistent.bgm_on:
            renpy.music.play(filenames = file, channel = 'bgm')
Sound settings screen:

Code: Select all

screen sound_settings(fromTitle):
    bar xpos 360 ypos 79 value BGMVolumeChanged() style "pref_slider" left_gutter 0 right_gutter 0

init -2:
    style pref_slider:
        xmaximum 118
        ymaximum 28
        right_bar "image/slider_empty.png"
        left_bar "image/slider_full.png"
        thumb "image/slider.png"
        thumb_offset 11
And your's implementation of volume change function(function name doesn't matter, does it?):

Code: Select all

    def BGMVolumeChanged(value = None):
        if value is None:
            return MixerValue('bgm')
        else:
            return SetMixer('bgm', value)
Slider moves fine, but playing BGM volume doesn't change :( By the way, functions playBGM and BGMVolumeChanged is in separate file(I named it serviceFunctions.rpy), is it all right? playBGM works fine.
There's no point in standing around with your mouth open waiting for talent to fall from the sky. You make your own. No matter what you're doing, the most essential thing is not to give up.

User avatar
shivanshs9
Regular
Posts: 54
Joined: Sun Jul 20, 2014 1:59 pm
Projects: The Destiny(http://thedestiny-cxz.blogspot.com)
Organization: Cyber-X-Zone
Location: India
Contact:

Re: Sliders and bindings

#4 Post by shivanshs9 »

Found the mistake! You registered the music channel in a wrong way...
Your code:

Code: Select all

$renpy.music.register_channel('bgm', loop = True, file_prefix = 'bgm/bgm', file_suffix = '.ogg')
How it should have been:

Code: Select all

$renpy.music.register_channel('bgm', loop = True, file_prefix = 'bgm/bgm', file_suffix = '.ogg', mixer = 'bgm')
Specifying the mixer is important, because the given code basically changes the volume of the mixer, not the channel...
"Destiny is a no matter of chance
It is a matter of choice
It is not a thing to be waited for
It is a thing to be achieved..."

-William Jennings Bryan
If you can dream and not make dreams your master;
If you can think and not make thoughts your aim,
If you can meet with Triumph and Disaster;
And treat those two impostors just the same,
Only then can you ever win against yourself...

User avatar
Yuuji
Regular
Posts: 96
Joined: Tue Jan 20, 2015 3:08 am
Location: Russia, Perm
Contact:

Re: Sliders and bindings

#5 Post by Yuuji »

Wow! Thank you very much!!! It works! Can I ask you one more question? Is there a way I can change through this slider value of persistent variable? Well, maybe I can write some code in BGMVolumeChanged function? Something like:

Code: Select all

    def BGMVolumeChanged(value = None):
        if value is None:
            return MixerValue('bgm')
        else:
            persistent.bgm_volume = value
            return SetMixer('bgm', value)
But it's not working though(persistent.bgm_volume doesn't change). Maybe there's another solution?
There's no point in standing around with your mouth open waiting for talent to fall from the sky. You make your own. No matter what you're doing, the most essential thing is not to give up.

User avatar
shivanshs9
Regular
Posts: 54
Joined: Sun Jul 20, 2014 1:59 pm
Projects: The Destiny(http://thedestiny-cxz.blogspot.com)
Organization: Cyber-X-Zone
Location: India
Contact:

Re: Sliders and bindings

#6 Post by shivanshs9 »

Well, it's to be expected that it won't work... It can't exactly change any variable...
But, no fear! There's also another way... Instead of adjusting MixerValues directly, you can adjust renpy.music.set_volume()(Source)...
Delete the previous function and replace that with this:

Code: Select all

init python:
    def bgm_volume_update(value):
        persistent.bgm_volume = value
        renpy.music.set_volume(value, channel="bgm")

    renpy.music.register_channel("bgm", mixer="bgm", loop=True)
    bgm_volume_adjust = ui.adjustment(range=1.0, value=1.0, adjustable=True, changed=bgm_volume_update)
And, as for the bar:

Code: Select all

bar adjustment bgm_volume_adjust
Now, for the explanation...
What I did now is that the bar uses ui.adjustment to call bgm_volume_update function every time the bar is changed... The changed value is sent to the function and then the function puts that in persistent.bgm_volume and changes the volume of the "bgm" channel...

And, that's about it! There are only 2 differences in this and the previous code:
  1. Uses ui.adjustment for its working.
  2. Adjusts the channel's volume, instead of mixer's volume, using the built-in renpy function.
"Destiny is a no matter of chance
It is a matter of choice
It is not a thing to be waited for
It is a thing to be achieved..."

-William Jennings Bryan
If you can dream and not make dreams your master;
If you can think and not make thoughts your aim,
If you can meet with Triumph and Disaster;
And treat those two impostors just the same,
Only then can you ever win against yourself...

User avatar
Yuuji
Regular
Posts: 96
Joined: Tue Jan 20, 2015 3:08 am
Location: Russia, Perm
Contact:

Re: Sliders and bindings

#7 Post by Yuuji »

Oh yeah! It works. Thank you very much! Now I can work with some confidence in this game's future :D
There's no point in standing around with your mouth open waiting for talent to fall from the sky. You make your own. No matter what you're doing, the most essential thing is not to give up.

User avatar
shivanshs9
Regular
Posts: 54
Joined: Sun Jul 20, 2014 1:59 pm
Projects: The Destiny(http://thedestiny-cxz.blogspot.com)
Organization: Cyber-X-Zone
Location: India
Contact:

Re: Sliders and bindings

#8 Post by shivanshs9 »

Glad to be of help! :smile:
"Destiny is a no matter of chance
It is a matter of choice
It is not a thing to be waited for
It is a thing to be achieved..."

-William Jennings Bryan
If you can dream and not make dreams your master;
If you can think and not make thoughts your aim,
If you can meet with Triumph and Disaster;
And treat those two impostors just the same,
Only then can you ever win against yourself...

Post Reply

Who is online

Users browsing this forum: No registered users