Playing a sound when opening a screen - sound keeps looping!

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
thirstyoctopus
Regular
Posts: 72
Joined: Mon Aug 27, 2018 12:04 pm
Projects: Romance Visual Novel story
Organization: Thirsty Octopus
Contact:

Playing a sound when opening a screen - sound keeps looping!

#1 Post by thirstyoctopus » Wed Apr 24, 2019 4:42 pm

Hi everyone

This is getting a bit annoying now, but what I'm trying to do is create a screen in my game that's like a notebook that the player can open to check some progress notes. I would like a sound to play when this happens, so I sourced a clip of a page being turned which sounds quite fitting.

However, it seems to play on every mouse click / button press as you are progressing through the dialogue, as well as the screen too.

Here's my code:

To set up the toggle key:

Code: Select all

screen notepad_toggle:
    if renpy.get_screen("crush_notes"):
        key "n" action Hide("crush_notes")
    else:
        key "n" action Show("crush_notes")

The screen that opens:

Code: Select all

screen crush_notes():

    $ renpy.play("fx/open_notebook.ogg")

    key "dismiss" action Hide("crush_notes")

    style_prefix "notebook"

    vbox:
        spacing 15

        frame:
            xalign 0.5
            ypos 0
            xpadding 50
            ypadding 70
            xfill True
            yfill True
            background "notepad bg"

            vbox:
                box_wrap True
                xsize 340
                xpos 480
                ypos 35

                text notes
I'm assuming that what's happening is that the play function is triggering because the screen is always 'there' even though it's not showing - but I don't know how to only get it to play once when the screen is opened, and then only again once the screen is re-opened.

Is there a way of doing this?

User avatar
Matalla
Veteran
Posts: 202
Joined: Wed Mar 06, 2019 6:22 pm
Completed: The Lost Smile
itch: matalla-interactive
Location: Spain
Contact:

Re: Playing a sound when opening a screen - sound keeps looping!

#2 Post by Matalla » Thu Apr 25, 2019 12:04 am

I usually use this for these kind of things

Code: Select all

timer 0.01 action Play("sound", "sounds/page turn.mp3")
This is actually a line from the project I'm working on, not a "whatever" example. And I'm doing it with a notebook like yours and similar sound effect (stop copying me, damn it!!).

On a (slightly) more serious note, also check that the channel you're playing the sound on, isn't on a loop. To my shame, it has happened to me.

Also, using "on show" could work too (and probably is the proper way to do it), but I'm using this clumsy method since I started with renpy and I had absolutely no freaking clue about anything. Now that I have ascended to the "barely no clue" category, I keep using it because I'm a sentimental guy who gets attached to silly things.
Comunidad Ren'Py en español (Discord)
Honest Critique

User avatar
thirstyoctopus
Regular
Posts: 72
Joined: Mon Aug 27, 2018 12:04 pm
Projects: Romance Visual Novel story
Organization: Thirsty Octopus
Contact:

Re: Playing a sound when opening a screen - sound keeps looping!

#3 Post by thirstyoctopus » Mon Apr 29, 2019 10:34 am

Hey, thanks for the info.

Doing a bit more digging I found using the 'on' statement worked:

Code: Select all

on "show" action Play("sound", "fx/open_notebook.ogg")
on "hide" action Play("sound", "fx/open_notebook.ogg")
This code is placed directly at the end of the screen block, so it only activates on show and hide of that particular screen.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]