Randomly picking sfx from a list to play (SOLVED)

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
Geckos
Veteran
Posts: 471
Joined: Fri Aug 17, 2012 8:33 am
Completed: Brilliant Shadows, Perceptions of the Dead, The Phantom Icecream Truck
Projects: Embers of Magic, Pale Spectrum, Perceptions of the Dead
Organization: Ithaqua Labs
Tumblr: geckosart
Deviantart: sitaart
Contact:

Randomly picking sfx from a list to play (SOLVED)

#1 Post by Geckos »

I'm trying to set it up so that the script chooses from a list of sound effects to play randomly.

So it appears to be choosing a sound to play, but instead of randoming the sound each time it just replays the same.
Upon inspecting lint, it says
"game/script.rpy:64 unable to evaluate filename u'chalkshort'" for each instance of chalkshort and chalklong

I'm fairly certain the randoming works if I add $import random before the sound instances, but that command stops the game from being able to create new save files.

Any thoughts?

For the record:
"am1" is setup as
renpy.music.register_channel("am1", "sfx", False, file_suffix=".ogg")

Code: Select all


#This is placed before start of script
init python:
    import random

#this is placed just inside of script
label start:
#this is a chalk writing sfx that is 3 seconds + long
$ linenum2 = random.randint(0,5)
$ randomvoiceclips = ["sfx/chalk/chalk_long/chalk1", "sfx/chalk/chalk_long/chalk2", "sfx/chalk/chalk_long/chalk3", "sfx/chalk/chalk_long/chalk4", "sfx/chalk/chalk_long/chalk5", "sfx/chalk/chalk_long/chalk6",]
$ chalklong = randomvoiceclips[linenum2]

#this is a chalk sfx that is ~1 second long

$ linenum = random.randint(0,10)
$ randomvoiceclips = ["sfx/chalk/chalk_short/chalk1", "sfx/chalk/chalk_short/chalk2", "sfx/chalk/chalk_short/chalk3", "sfx/chalk/chalk_short/chalk4", "sfx/chalk/chalk_short/chalk5", "sfx/chalk/chalk_short/chalk6", "sfx/chalk/chalk_short/chalk7", "sfx/chalk/chalk_short/chalk8", "sfx/chalk/chalk_short/chalk9", "sfx/chalk/chalk_short/chalk10", "sfx/chalk/chalk_short/chalk11"]
$ chalkshort = randomvoiceclips[linenum]


play am1 chalkshort
"test test test chalk short"


play am1 chalklong
"plab plab blab chalk long"

Last edited by Geckos on Tue Mar 21, 2017 4:26 am, edited 3 times in total.
Image ImageImage

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

Re: Randomly picking sfx from a list to play

#2 Post by philat »

Why random and not renpy.random?

User avatar
xavimat
Eileen-Class Veteran
Posts: 1460
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love, unknown
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Contact:

Re: Randomly picking sfx from a list to play

#3 Post by xavimat »

Random is already imported inside renpy (with some adaptations to work with rollback) so you can use renpy.random,as philat suggests.

The code you have shown lacks "labels" so we can't see how the flow works.

This is my suggestion:

Code: Select all

## outside any label:
default randomvoiceclips_long = ["sfx/chalk/chalk_long/chalk1", "sfx/chalk/chalk_long/chalk2", "sfx/chalk/chalk_long/chalk3", "sfx/chalk/chalk_long/chalk4", "sfx/chalk/chalk_long/chalk5", "sfx/chalk/chalk_long/chalk6",]

default randomvoiceclips_short = ["sfx/chalk/chalk_short/chalk1", "sfx/chalk/chalk_short/chalk2", "sfx/chalk/chalk_short/chalk3", "sfx/chalk/chalk_short/chalk4", "sfx/chalk/chalk_short/chalk5", "sfx/chalk/chalk_short/chalk6", "sfx/chalk/chalk_short/chalk7", "sfx/chalk/chalk_short/chalk8", "sfx/chalk/chalk_short/chalk9", "sfx/chalk/chalk_short/chalk10", "sfx/chalk/chalk_short/chalk11"]

# Inside your labels:
label some_label:
    play sound renpy.random.choice(randomvoiceclips_long)
    "Now a long sound is playing..."
    play sound renpy.random.choice(randomvoiceclips_short)
    "Now a short sound is playing..."
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

User avatar
Geckos
Veteran
Posts: 471
Joined: Fri Aug 17, 2012 8:33 am
Completed: Brilliant Shadows, Perceptions of the Dead, The Phantom Icecream Truck
Projects: Embers of Magic, Pale Spectrum, Perceptions of the Dead
Organization: Ithaqua Labs
Tumblr: geckosart
Deviantart: sitaart
Contact:

Re: Randomly picking sfx from a list to play

#4 Post by Geckos »

Using the other code does generate a random sound for each line that calls it, but lint still pulls up:

game/mountain.rpy:5033 unable to evaluate filename u'renpy.random.choice(randomvoiceclips_long)'


The reason why I was trying the initial code is because I would also like to use it to pick random lines+the same random voice lines to play. Is Renpy.random capable of this as well?

Code: Select all


$linenum = random.randint(0,2)
$randomlines = ["Hello this is line 1","Hello this is line 2","Hello this is line 3"]
$randomvoiceclips = ["aku/chap4/aku01", "aku/chap4/aku02", "aku/chap4/aku03"]
$selectedline = randomlines[linenum]
$selectedvoice = randomvoiceclips[linenum]

play voice selectedvoice
aku "[selectedline]."

Image ImageImage

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

Re: Randomly picking sfx from a list to play

#5 Post by Imperf3kt »

Your files have no extension
Chalk3, for example is not a file but Chalk3.mp3 or Chslk3.ogg is a file
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

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Randomly picking sfx from a list to play

#6 Post by xela »

Files are playing so suffix is likely to be setup for the channel. Maybe:

Code: Select all

## outside any label:
default randomvoiceclips_long = ["sfx/chalk/chalk_long/chalk1", "sfx/chalk/chalk_long/chalk2", "sfx/chalk/chalk_long/chalk3", "sfx/chalk/chalk_long/chalk4", "sfx/chalk/chalk_long/chalk5", "sfx/chalk/chalk_long/chalk6",]

default randomvoiceclips_short = ["sfx/chalk/chalk_short/chalk1", "sfx/chalk/chalk_short/chalk2", "sfx/chalk/chalk_short/chalk3", "sfx/chalk/chalk_short/chalk4", "sfx/chalk/chalk_short/chalk5", "sfx/chalk/chalk_short/chalk6", "sfx/chalk/chalk_short/chalk7", "sfx/chalk/chalk_short/chalk8", "sfx/chalk/chalk_short/chalk9", "sfx/chalk/chalk_short/chalk10", "sfx/chalk/chalk_short/chalk11"]

# Inside your labels:
label some_label:
    $ temp = renpy.random.choice(randomvoiceclips_long)
    play audio temp
    "Now a long sound is playing..."
    $ temp = renpy.random.choice(randomvoiceclips_short)
    play audio temp
    "Now a short sound is playing..."
will work? (code borrowed from xavimat's post)
Like what we're doing? Support us at:
Image

User avatar
Geckos
Veteran
Posts: 471
Joined: Fri Aug 17, 2012 8:33 am
Completed: Brilliant Shadows, Perceptions of the Dead, The Phantom Icecream Truck
Projects: Embers of Magic, Pale Spectrum, Perceptions of the Dead
Organization: Ithaqua Labs
Tumblr: geckosart
Deviantart: sitaart
Contact:

Re: Randomly picking sfx from a list to play

#7 Post by Geckos »

Imperf3kt wrote:Your files have no extension
Chalk3, for example is not a file but Chalk3.mp3 or Chslk3.ogg is a file
I have my sound channels set up to do that for me automatically, sorry for any confusion there!

Giving the latest code a whirl!
Image ImageImage

User avatar
Arowana
Miko-Class Veteran
Posts: 531
Joined: Thu May 31, 2012 11:17 pm
Completed: a2 ~a due~
Projects: AXIOM.01, The Pirate Mermaid
Organization: Variable X, Navigame
Tumblr: navigame-media
itch: navigame
Contact:

Re: Randomly picking sfx from a list to play

#8 Post by Arowana »

EDIT: Sorry, I think I misunderstood the problem the first time. If this isn't relevant, please skip to my post after this. :lol:

***

FYI, renpy.random participates in rollback, so you'll generate the same result regardless of how many times you roll back (see documentation here). If you want the result to change each time, you'll need random instead of renpy.random, which is actually what you had in your first post.

I actually think the code in your first post looks mostly okay (assuming that everything was indented properly), but there is an extra comma in one of the lists.
$ randomvoiceclips = ["sfx/chalk/chalk_long/chalk1", "sfx/chalk/chalk_long/chalk2", "sfx/chalk/chalk_long/chalk3", "sfx/chalk/chalk_long/chalk4", "sfx/chalk/chalk_long/chalk5", "sfx/chalk/chalk_long/chalk6",] #####<------ extra comma at the end here
Try removing that comma and see if that helps? Or if you want to use xela's code, you could do:
## outside any label:
default randomvoiceclips_long = ["sfx/chalk/chalk_long/chalk1", "sfx/chalk/chalk_long/chalk2", "sfx/chalk/chalk_long/chalk3", "sfx/chalk/chalk_long/chalk4", "sfx/chalk/chalk_long/chalk5", "sfx/chalk/chalk_long/chalk6",]

default randomvoiceclips_short = ["sfx/chalk/chalk_short/chalk1", "sfx/chalk/chalk_short/chalk2", "sfx/chalk/chalk_short/chalk3", "sfx/chalk/chalk_short/chalk4", "sfx/chalk/chalk_short/chalk5", "sfx/chalk/chalk_short/chalk6", "sfx/chalk/chalk_short/chalk7", "sfx/chalk/chalk_short/chalk8", "sfx/chalk/chalk_short/chalk9", "sfx/chalk/chalk_short/chalk10", "sfx/chalk/chalk_short/chalk11"]

init python: #<----- add this like in your first post
import random

# Inside your labels:
label some_label:
$ temp = random.choice(randomvoiceclips_long) #<---- change renpy.random to random
play audio temp
"Now a long sound is playing..."
$ temp = random.choice(randomvoiceclips_short) #<---- change renpy.random to random
play audio temp
"Now a short sound is playing..."
Last edited by Arowana on Tue Mar 21, 2017 4:08 am, edited 1 time in total.
Complete: a2 ~a due~ (music, language, love)
In progress: The Pirate Mermaid (fairytale otome)
On hold: AXIOM.01 (girl detective game)

Image

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

Re: Randomly picking sfx from a list to play

#9 Post by philat »

Why would you want the sound to change each time you rollback? You'd want the sound to change each time you want to play a new sound, yes. But why on rollback?

User avatar
Arowana
Miko-Class Veteran
Posts: 531
Joined: Thu May 31, 2012 11:17 pm
Completed: a2 ~a due~
Projects: AXIOM.01, The Pirate Mermaid
Organization: Variable X, Navigame
Tumblr: navigame-media
itch: navigame
Contact:

Re: Randomly picking sfx from a list to play

#10 Post by Arowana »

philat wrote:Why would you want the sound to change each time you rollback? You'd want the sound to change each time you want to play a new sound, yes. But why on rollback?
I'm not sure why in this case. But that's what Geckos requested, if I understand them correctly? :lol:

EDIT: Ah okay, I think I misunderstood after all, sorry. :oops: The problem was not that the sounds aren't randomized correctly, but rather that Lint is giving errors for chalkshort and chalklong (or whichever stand-in for the filename you are using)?

Maybe the play statement is having trouble with the stand-in filename. Have you tried the equivalent renpy function instead? So, for example, in place of a play statement like this:

Code: Select all

play am1 chalkshort
Use:

Code: Select all

$renpy.music.play(chalkshort, channel="am1")
Similarly, for xela's example:

Code: Select all

play audio temp
would become:

Code: Select all

$renpy.music.play(temp, channel="audio")
I gave this a try and didn't get any Lint errors, so I hope it works for you. :D
Complete: a2 ~a due~ (music, language, love)
In progress: The Pirate Mermaid (fairytale otome)
On hold: AXIOM.01 (girl detective game)

Image

User avatar
Geckos
Veteran
Posts: 471
Joined: Fri Aug 17, 2012 8:33 am
Completed: Brilliant Shadows, Perceptions of the Dead, The Phantom Icecream Truck
Projects: Embers of Magic, Pale Spectrum, Perceptions of the Dead
Organization: Ithaqua Labs
Tumblr: geckosart
Deviantart: sitaart
Contact:

Re: Randomly picking sfx from a list to play

#11 Post by Geckos »

Alright!
I apologize for bringing a question that was more like 2 or 3 questions here, that was sloppy of me, and me just biting off more than I could chew as usual.

That being said, I was able to remedy the randoming sound effect to my liking.
So it plays a random sound effect for every instance of it being called, instead of just picking one sound effect and replaying it over and over again.

I achieved this (or Caelem did, who helped me!) by doing this:

Code: Select all

init python:
    import random
    

    def chalklong():
            linenum2 = random.randint(0,5)
            randomvoiceclips = ["sfx/chalk/chalk_long/chalk1", "sfx/chalk/chalk_long/chalk2", "sfx/chalk/chalk_long/chalk3", "sfx/chalk/chalk_long/chalk4", "sfx/chalk/chalk_long/chalk5", "sfx/chalk/chalk_long/chalk6",]
            chalk = randomvoiceclips[linenum2]
            return chalk

    def chalkshort():
            linenum = random.randint(0,10)
            randomvoiceclips = ["sfx/chalk/chalk_short/chalk1", "sfx/chalk/chalk_short/chalk2", "sfx/chalk/chalk_short/chalk3", "sfx/chalk/chalk_short/chalk4", "sfx/chalk/chalk_short/chalk5", "sfx/chalk/chalk_short/chalk6", "sfx/chalk/chalk_short/chalk7", "sfx/chalk/chalk_short/chalk8", "sfx/chalk/chalk_short/chalk9", "sfx/chalk/chalk_short/chalk10", "sfx/chalk/chalk_short/chalk11"]
            chalk = randomvoiceclips[linenum]
            return chalk    

label start:    

play am1 chalkshort()
speaker "This plays a short chalk sound!"
play am1 chalklong()
speaker "this plays a long chalk sound!"



That aside, using the code that Arowana suggested,
$renpy.music.play(selectedvoice, channel="voice")
Lint is no longer angry at me (yay!), and I can use the code below to say a random line from a list, as well as play the same, random voiced line at the same time!

It's everything silly I can now do in ren'py and more.

Code: Select all


$linenum = random.randint(0,2)
$randomlines = ["Hello this is line 1","Here kitty, kitty!","Uh..."]
$randomvoiceclips = ["aku/chap4/aku01", "aku/chap4/aku02", "aku/chap4/aku03"]
$selectedline = randomlines[linenum]
$selectedvoice = randomvoiceclips[linenum]

$renpy.music.play(selectedvoice, channel="voice")
speaker "[selectedline]."

That is all of my problems fixed, and you are all awesome and thank you for letting me pick your brains.

P.s.
Would it be better to use
play voice selectedvoice
aku "[selectedline]."

To play the random clips as opposed to $renpy.music.play(selectedvoice, channel="voice")?
Lint complained with

game/script.rpy:121 unable to evaluate filename u'selectedvoice'

But there were otherwise no apparent errors from using

play voice selectedvoice
aku "[selectedline]."

instead of

$renpy.music.play(selectedvoice, channel="voice")
aku "[selectedline]."
Image ImageImage

Post Reply

Who is online

Users browsing this forum: Google [Bot]