Random sounds for sound sample button

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Random sounds for sound sample button

#1 Post by Imperf3kt »

This is likely a very easy function that's probably been covered somewhere, but I didn't see anything while searching the forum, and figured some novices would potentially find this useful.

By default, Ren'Py comes with a volume test button in the preferences screen. You can set a sound to play by making sure sounds are enabled and defining the music file to play within Options.rpy

Code: Select all

define config.has_sound = True
define config.sample_sound = "music/effects/gun1.wav"
Then, within the Screens.rpy, within the preferences menu, you'll find:

Code: Select all

if config.has_sound:
                        label _("Sound Volume")

                        hbox:
                            bar value Preference("sound volume")

                            if config.sample_sound:
                                textbutton _("Test") action Play("sound", config.sample_sound)
So, let's assume we want multiple sounds at play at random.
For that, we can use Ren'Py random.

Directly underneath the start of the screen, add the following:

Code: Select all

screen preferences():
    $ sfx_test = renpy.random.randint(1, 3)# range of samples to use. For more or less samples, change the 3 to suit.
This example tells Ren'Py to choose between one of three possible choices.
So now you need to define what those choices are.
Go back to Options.rpy and replace this line define config.sample_sound = "music/effects/gun1.wav" with:

Code: Select all

define config.sample_sound = ["music/effects/gun1.wav", "music/effects/gun2.wav", "music/effects/glass1.wav"]
Remember to use your own file names/paths

Go to the Screens.rpy file again and change:

Code: Select all

                            if config.sample_sound:
                                textbutton _("Test") action Play("sound", config.sample_sound)
to

Code: Select all

                            if config.sample_sound:
                                if sfx_test == 1:
                                    textbutton _("Test") action [ Play("sound", "music/effects/gun1.wav") ]
                                if sfx_test == 2:
                                    textbutton _("Test") action [ Play("sound", "music/effects/gun2.wav") ]
                                if sfx_test == 3:
                                    textbutton _("Test") action [ Play("sound", "music/effects/glass1.wav") ]
And presto, whenever a player presses the test sound volume button, Ren'Py will choose one of the three (or more) available files to play.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

Post Reply

Who is online

Users browsing this forum: haitai, Majestic-12 [Bot]