[SOLVED] Sound effect while typing

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
bloobeary
Regular
Posts: 36
Joined: Fri Oct 04, 2019 3:02 pm
Deviantart: bloobeary
Soundcloud: user-212448702
Contact:

[SOLVED] Sound effect while typing

#1 Post by bloobeary »

Since I didn't create the original thread:
viewtopic.php?f=8&t=40217

... I'm going to create a new one here so that I can mark this problem as solved.

Here's the solution:

Add this bit of code before the label start:

Code: Select all

init python:
    def Typing(what):
        renpy.music.play(renpy.random.choice(['sounds/typing1.wav','sounds/typing2.wav','sounds/typing3.wav']), channel="audio")
 
typing1.wav through typing3.wav should be short single beeps or keypress sounds.

just below that add this bit of code:

Code: Select all

init -2 python:
    class GetInput(Action):
        def __init__(self,screen_name,input_id):
            self.screen_name=screen_name
            self.input_id=input_id
        def __call__(self):
            if renpy.get_widget(self.screen_name,self.input_id):
                return str(renpy.get_widget(self.screen_name,self.input_id).content)
Down in the dialogue section of the code, this is what I am using to generate the input event:

Code: Select all

    python:
        name = renpy.input(_("Type in your first name:"))

        name = name.strip() or __("Player")
and finally, make this edit to the input section of the screens.rpy

Code: Select all

screen input(prompt):
    style_prefix "input"

    window:

        vbox:
            xalign gui.dialogue_text_xalign
            xpos gui.dialogue_xpos
            xsize gui.dialogue_width
            ypos gui.dialogue_ypos

            text prompt style "input_prompt"
            input id "input" changed Typing
            key "K_RETURN" action GetInput("input", "input")
Note that the only changes are in the last two lines, there.

Adding changed Typing calls the sound event.
and the key "K_RETURN" line makes the Enter key work.

Someone better at the code may find a more elegant solution, but for now, I have this and it works. So, here it is.

Many thanks and much credit to ShiroVN over on /r/Renpy

bloobeary
Regular
Posts: 36
Joined: Fri Oct 04, 2019 3:02 pm
Deviantart: bloobeary
Soundcloud: user-212448702
Contact:

Re: [SOLVED] Sound effect while typing

#2 Post by bloobeary »

update:

ShiroVN polished up the code a bit, to make it easier to use:

* In your script, before Label Start:

First create a variable:

Code: Select all

default input_value = ""
Add a Typing function to play the sounds (use short, individual keypress sounds):

Code: Select all

init python:
    def Typing(what):
        global input_value
        renpy.music.play(renpy.random.choice(["audio/typing.mp3","audio/typing2.mp3"]), channel="audio")
        input_value = what
* Over in Screens.rpy find the input screen section and make these changes (note, it's only the bottom two lines):

Code: Select all

screen input(prompt):
    style_prefix "input"

    window:

        vbox:
            xalign gui.dialogue_text_xalign
            xpos gui.dialogue_xpos
            xsize gui.dialogue_width
            ypos gui.dialogue_ypos

            text prompt style "input_prompt"
            input id "input" changed Typing
            key "K_RETURN" action Return()
* And Finally, in the dialogue section of your project, here's the code used to generate the input:

Code: Select all

python:
    renpy.input(_("Type in your first name:"))

    name = input_value

    name = name.strip() or __("Player")
As ShiroVN noted: "it's more understandable for beginner than creating a whole new class, when you can do it in just 3 more lines of code."

Hope folks get some use out of this.

Post Reply

Who is online

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