[solved] How can I make it play a random sound?

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
Nanahs
Regular
Posts: 169
Joined: Wed Aug 22, 2018 5:50 pm
Contact:

[solved] How can I make it play a random sound?

#1 Post by Nanahs » Tue Oct 09, 2018 5:20 pm

So, I'm using hotspot. There's a buttom that when you click, you play a sound (not music).
But I wanted to have more than one sound, that would play randomly when the person clicks.

I was trying to use:

Code: Select all

            hotspot(549, 88, 57, 47) action Play("sound","jhoneffect.mp3", "annaeffect.mp3", "markeffect.mp3", loop=True, shuffle=True)
But it didn't work. Only the first sound (jhoneffect.mp3) plays when I click the buttom.
I can't change "sound" to "music", because that would conflit with the background music in the scene. I need it to be a "sound channel".

ps: this label is sumulating a radio. I have a buttom to "play/stop" music. And another one to "play/stop" sound.
That's why I have to put them separately. Because I want to be able to stop the "sound" without stopping "music".

Does anyone know where the mistake is in the code?

Thanks :)
Last edited by Nanahs on Wed Oct 10, 2018 2:21 pm, edited 1 time in total.

User avatar
Imperf3kt
Eileen-Class Veteran
Posts: 1948
Joined: Mon Dec 14, 2015 5:05 am
Location: Your monitor
Contact:

Re: How can I make it play a random sound?

#2 Post by Imperf3kt » Tue Oct 09, 2018 5:56 pm

You can use renpy.random to select a file from a list.
See my cookbook entry for an example.
viewtopic.php?f=51&t=42259
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Nanahs
Regular
Posts: 169
Joined: Wed Aug 22, 2018 5:50 pm
Contact:

Re: How can I make it play a random sound?

#3 Post by Nanahs » Tue Oct 09, 2018 6:28 pm

Imperf3kt wrote:
Tue Oct 09, 2018 5:56 pm
You can use renpy.random to select a file from a list.
See my cookbook entry for an example.
viewtopic.php?f=51&t=42259
Thank you! I think I understood the process :)
The only thing I'm still in doubt is, after making those changes, how would I use this in a hotspot action?

User avatar
Imperf3kt
Eileen-Class Veteran
Posts: 1948
Joined: Mon Dec 14, 2015 5:05 am
Location: Your monitor
Contact:

Re: How can I make it play a random sound?

#4 Post by Imperf3kt » Tue Oct 09, 2018 9:34 pm

An "action" will work the same for a Hotspot as it would for an imagebutton, textbutton or any other button.

I can't go over it right now, but just use it as shown in the example, replacing the appropriate buttons with your Hotspot.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Nanahs
Regular
Posts: 169
Joined: Wed Aug 22, 2018 5:50 pm
Contact:

Re: How can I make it play a random sound?

#5 Post by Nanahs » Tue Oct 09, 2018 10:51 pm

Imperf3kt wrote:
Tue Oct 09, 2018 9:34 pm
An "action" will work the same for a Hotspot as it would for an imagebutton, textbutton or any other button.

I can't go over it right now, but just use it as shown in the example, replacing the appropriate buttons with your Hotspot.
Oh, ok! Thank you! :)

User avatar
IrinaLazareva
Veteran
Posts: 272
Joined: Wed Jun 08, 2016 1:49 pm
Projects: Legacy, Эра Тёмных Небес
Organization: IKG
Location: St.Petersburg, Russia
Contact:

Re: How can I make it play a random sound?

#6 Post by IrinaLazareva » Wed Oct 10, 2018 1:38 pm

Nanahs wrote:
Tue Oct 09, 2018 5:20 pm
So, I'm using hotspot. There's a buttom that when you click, you play a sound (not music).
But I wanted to have more than one sound, that would play randomly when the person clicks.
I was trying to use:

Code: Select all

            hotspot(549, 88, 57, 47) action Play("sound","jhoneffect.mp3", "annaeffect.mp3", "markeffect.mp3", loop=True, shuffle=True)
Try:

Code: Select all

            hotspot(549, 88, 57, 47) action Play("sound", renpy.random.choice(["jhoneffect.mp3", "annaeffect.mp3", "markeffect.mp3"]))
Or somewhere outside of main code:

Code: Select all

define mysound = ["jhoneffect.mp3", "annaeffect.mp3", "markeffect.mp3"]
and then:

Code: Select all

            hotspot(549, 88, 57, 47) action Play("sound", renpy.random.choice(mysound))
About random -> https://renpy.org/doc/html/other.html#renpy-random
Nanahs wrote:
Tue Oct 09, 2018 5:20 pm
I can't change "sound" to "music", because that would conflit with the background music in the scene. I need it to be a "sound channel".
You can create a new channel - > https://renpy.org/doc/html/audio.html#r ... er_channel
There is a light at the end of the tunnel, just pray it's not a train.
___
TODO: unresolved/нерешённое

User avatar
IrinaLazareva
Veteran
Posts: 272
Joined: Wed Jun 08, 2016 1:49 pm
Projects: Legacy, Эра Тёмных Небес
Organization: IKG
Location: St.Petersburg, Russia
Contact:

Re: How can I make it play a random sound?

#7 Post by IrinaLazareva » Wed Oct 10, 2018 1:48 pm

And here's an example (It will work with hotspot too).

Code: Select all

         textbutton 'Power on' activate_sound renpy.random.choice(mysound) action Play('music', 'some_music.mp3')
         textbutton 'Power off' activate_sound renpy.random.choice(mysound) action Stop('music')
https://renpy.org/doc/html/style_proper ... vate_sound
There is a light at the end of the tunnel, just pray it's not a train.
___
TODO: unresolved/нерешённое

Nanahs
Regular
Posts: 169
Joined: Wed Aug 22, 2018 5:50 pm
Contact:

Re: How can I make it play a random sound?

#8 Post by Nanahs » Wed Oct 10, 2018 2:21 pm

IrinaLazareva wrote:
Wed Oct 10, 2018 1:48 pm
And here's an example (It will work with hotspot too).

Code: Select all

         textbutton 'Power on' activate_sound renpy.random.choice(mysound) action Play('music', 'some_music.mp3')
         textbutton 'Power off' activate_sound renpy.random.choice(mysound) action Stop('music')
https://renpy.org/doc/html/style_proper ... vate_sound
The first option worked! Thank you so much! :)

Nanahs
Regular
Posts: 169
Joined: Wed Aug 22, 2018 5:50 pm
Contact:

Re: How can I make it play a random sound?

#9 Post by Nanahs » Wed Oct 10, 2018 9:59 pm

IrinaLazareva wrote:
Wed Oct 10, 2018 1:48 pm
And here's an example (It will work with hotspot too).

Code: Select all

         textbutton 'Power on' activate_sound renpy.random.choice(mysound) action Play('music', 'some_music.mp3')
         textbutton 'Power off' activate_sound renpy.random.choice(mysound) action Stop('music')
https://renpy.org/doc/html/style_proper ... vate_sound
I used the first code you suggested. "loop=True" is not working to make the sound loop. Do you know why? :?:

ps: oh, never mind. It's working now hah

Nanahs
Regular
Posts: 169
Joined: Wed Aug 22, 2018 5:50 pm
Contact:

Re: How can I make it play a random sound?

#10 Post by Nanahs » Thu Oct 11, 2018 12:38 pm

IrinaLazareva wrote:
Wed Oct 10, 2018 1:48 pm
And here's an example (It will work with hotspot too).

Code: Select all

         textbutton 'Power on' activate_sound renpy.random.choice(mysound) action Play('music', 'some_music.mp3')
         textbutton 'Power off' activate_sound renpy.random.choice(mysound) action Stop('music')
https://renpy.org/doc/html/style_proper ... vate_sound
Hello! Sorry to "call" you again hah

I used:

Code: Select all

            hotspot(549, 88, 57, 47) action Play("sound", renpy.random.choice(["jhoneffect.mp3", "annaeffect.mp3", "markeffect.mp3"]),loop=True)
But now, how should I make this sound stop when jumping to the next label? I keeps playing.
I tried a few codes I know, but they didn't work. Thanks.

User avatar
Imperf3kt
Eileen-Class Veteran
Posts: 1948
Joined: Mon Dec 14, 2015 5:05 am
Location: Your monitor
Contact:

Re: [solved] How can I make it play a random sound?

#11 Post by Imperf3kt » Thu Oct 11, 2018 2:34 pm

One way is to stop the sound channel

Code: Select all

            hotspot(549, 88, 57, 47) action Play("sound", renpy.random.choice(["jhoneffect.mp3", "annaeffect.mp3", "markeffect.mp3"]),loop=True)


label start:
    "I assume you start here."

label silence:
    stop sound
    "Ahh, nice and quiet."
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Nanahs
Regular
Posts: 169
Joined: Wed Aug 22, 2018 5:50 pm
Contact:

Re: [solved] How can I make it play a random sound?

#12 Post by Nanahs » Thu Oct 11, 2018 4:44 pm

Imperf3kt wrote:
Thu Oct 11, 2018 2:34 pm
One way is to stop the sound channel

Code: Select all

            hotspot(549, 88, 57, 47) action Play("sound", renpy.random.choice(["jhoneffect.mp3", "annaeffect.mp3", "markeffect.mp3"]),loop=True)


label start:
    "I assume you start here."

label silence:
    stop sound
    "Ahh, nice and quiet."
Hello! For some reason "stop sound" was not working, but it worked now. Thank you! :)

Post Reply

Who is online

Users browsing this forum: No registered users