Sound in screens

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
jagharhemma
Newbie
Posts: 14
Joined: Wed Nov 29, 2017 10:49 am
Contact:

Sound in screens

#1 Post by jagharhemma »

I have created a combination lock screen and i want it to play a sound once the lock is opened.

I know how to play sounds in buttons, but here i am using a if statement that once var1=cvar2=var3 should play a click sound, how do i do???

Tried a multitude of variations but i always get errors, what is the magic words :)

Very grateful for help!

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Sound in screens

#2 Post by kivik »

If I understand you correctly, you have multiple buttons that when clicked ultimately can trigger the unlock condition. In which case, you should probably separate the logic from the screen code by calling a function on each button's action. Your function will then check whether the lock is unlocked, and if so, play the sound:

Code: Select all

init python:
    def check_lock():
        if condition_here: # put your own condition here
            renpy.play("audio filename.ogg") # refer to this for more info https://www.renpy.org/doc/html/audio.html#renpy.play
            # other things that should happen when lock is opened. e.g. renpy.hide("your_lock_screen")

...

imagebutton:
    # your button code
    action [whatever changes your combination here, Function(check_lock)]
If your lock is triggered by changing the combination directly (e.g. dial buttons or num pad buttons), then you'd need to call that function at the end of each button's action list, if you have a dedicated button that can be used to test if the code is correct, then you only need to put the function at the end of that button. Your logic may differ depending on the rest of your code, but this is a quick suggestion of how to do it.

The reason you want to avoid putting logic directly in your screen is because screens have predictions by default and will trigger any python code multiple times.

jagharhemma
Newbie
Posts: 14
Joined: Wed Nov 29, 2017 10:49 am
Contact:

Re: Sound in screens

#3 Post by jagharhemma »

So, decided top paste the code...

As you can see i have no test button,. i want the lock to react to the right code, without being forced to test every combo manually.

All i would like to achieve it to play a click where i have ####Here i would like the click sound to play####

Not sure how i am to use the the suggested solution, sorry i am really crap coding... Trial and error is my chosen method, pretty much. And if it works, i don´t question it too much...

BTW, i presume there is a better way to solve this but as i mentioned, if it works...


Code: Select all



screen lock:
    modal True

    if masterkey == True:
         text "Skipping."xalign 0.0 yalign 0.5
         imagebutton auto "save_%s.png" action ShowMenu('save') xalign 0.5 yalign 0.5
    else:
        text "Not Skipping." xalign 0.1 yalign 0.78
        if lock1 == 0:
            imagebutton auto "numbers/noll_%s.png" action Play("sound", "click.ogg"), SetVariable("lock1", lock1 +1) xalign 0.3 yalign 0.5
        if lock1 == 1:
            imagebutton auto "numbers/ett_%s.png" action Play("sound", "click.ogg"),SetVariable("lock1", lock1 +1) xalign 0.3 yalign 0.5
        if lock1 == 2:
            imagebutton auto "numbers/two_%s.png" action Play("sound", "click.ogg"),SetVariable("lock1", 0) xalign 0.3 yalign 0.5
##*********************************************************************************************************************
        if lock2 == 0:
            imagebutton auto "numbers/noll_%s.png" action Play("sound", "click.ogg"),SetVariable("lock2", lock2 +1) xalign 0.5 yalign 0.5
        if lock2 == 1:
            imagebutton auto "numbers/ett_%s.png" action Play("sound", "click.ogg"),SetVariable("lock2", lock2 +1) xalign 0.5 yalign 0.5
        if lock2 == 2:
            imagebutton auto "numbers/two_%s.png" action Play("sound", "click.ogg"),SetVariable("lock2", 0) xalign 0.5 yalign 0.5
##*********************************************************************************************************************

        if lock3 == 0:
            imagebutton auto "numbers/noll_%s.png" action Play("sound", "click.ogg"),SetVariable("lock3", lock3 +1) xalign 0.7 yalign 0.5
        if lock3 == 1:
            imagebutton auto "numbers/ett_%s.png" action Play("sound", "click.ogg"),SetVariable("lock3", lock3 +1) xalign 0.7 yalign 0.5
        if lock3 == 2:
            imagebutton auto "numbers/two_%s.png" action Play("sound", "click.ogg"),SetVariable("lock3", 0) xalign 0.7 yalign 0.5
##*********************************************************************************************************************
        if lock1 == 2 and lock2 ==1 and lock3 == 2:
            text "OPEN."xalign 0.0 yalign 0.5
            ####Here i would like the click sound to play####
            add "numbers/lock_un_locked.png" xalign 0.0 yalign 0.5
            text "Line 1{w} Line 1{w=1.0} Line 1"
            text "{color=#0000ffff}{size=52}You hear a clicking sound and the lock is unlocked.{/size}Blue{/color}" xalign 0.5 yalign 0.50
            timer 2.0 action Hide("lock", dissolve)
        else:
            text "Locked."xalign 0.0 yalign 0.5
            add "numbers/lock_locked.png" xalign 0.0 yalign 0.5





    textbutton "Return" action [ Hide("lock")]

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Sound in screens

#4 Post by kivik »

Ok this is just a quick untested mock up before I go to bed, so you'll most likely need to tweak it:

Code: Select all

define t_nums = {0: "noll", 1: "ett", 2: "two"}

default lock1 = 0
default lock2 = 0
default lock3 = 0
default unlocked = False

init python:
    def lock_increment(lock):
        globals()[lock] = (globals()[lock] + 1) % 3
        global lock1, lock2, lock3, unlocked
        if lock1 == 2 and lock2 == 1 and lock3 == 2:
            unlocked = True
            renpy.play("unlock.ogg") # not sure what your sound file is called

screen lock:
    modal True

    if masterkey == True:
        text "Skipping."xalign 0.0 yalign 0.5
        imagebutton auto "save_%s.png" action ShowMenu('save') xalign 0.5 yalign 0.5
    else:
        text "Not Skipping." xalign 0.1 yalign 0.78
        imagebutton auto "numbers/%s.png" % t_nums[lock1]:
            xalign 0.3 yalign 0.5
            action [Play("sound", "click.ogg"), Function(lock_increment, "lock1")]
        imagebutton auto "numbers/%s.png" % t_nums[lock2]:
            xalign 0.5 yalign 0.5
            action [Play("sound", "click.ogg"), Function(lock_increment, "lock2")]
        imagebutton auto "numbers/%s.png" % t_nums[lock3]:
            xalign 0.7 yalign 0.5
            action [Play("sound", "click.ogg"), Function(lock_increment, "lock3")]

        if unlocked:
            text "OPEN."xalign 0.0 yalign 0.5
            ####Here i would like the click sound to play####
            add "numbers/lock_un_locked.png" xalign 0.0 yalign 0.5
            text "Line 1{w} Line 1{w=1.0} Line 1"
            text "{color=#0000ffff}{size=52}You hear a clicking sound and the lock is unlocked.{/size}Blue{/color}" xalign 0.5 yalign 0.50
            timer 2.0 action Hide("lock", dissolve)
        else:
            text "Locked."xalign 0.0 yalign 0.5
            add "numbers/lock_locked.png" xalign 0.0 yalign 0.5
A few things I've done here: I've simplified your imagebutton code to take the current lock1, lock2 and lock3 status and translated them into the suitable imagebutton filenames. %s is python's string operator, allowing you to substitute variables in. Personally I'd name your files numbers/0.png, numbers/1.png, numbers/2.png for ease, but to keep to what you've done I've created a dictionary to translate the 0, 1 and 2 into "noll", "ett" and "two" to make your imagebuttons work. You can skip that entirely if you name your files with numerals.

This means you don't need multiple condition statements for each number, as well as mixing in the logic of what to set the variables to - because we can now just use the function lock_increment() to handle the changes.

lock_increment takes the name of the variable, then using python's global() dictionary to grab the right lock variable, and calculate what the next value should be. The calculation is:

n = (n + 1) % 3

That increments your variable by 1, then it uses the python mod calculation (returns the remainder of a division) to essentially roll your numbers back to 0 when it hits 3. Then we immediately check whether the 3 locks are the correct value and plays the unlock sound (you have to change it to your unlock sound's filename) and set the lock to unlocked. I had a look at the screen page on Renpy docs and there's no mention of adding sound effects - so I've opted for doing it directly from the function.

Now your screen just checks whether unlocked is True and does the rest of your code.


If you plan on having multiple locks, I'd strongly recommend you create lock objects with attributes for each digit, a correct combination, and a lock and unlock state. Look up python object orientation to see how to do it but we can easily help you. I just had a look and codeacademy has a python course with classes. Not tried it to know if it's any good though: https://www.codecademy.com/courses/lear ... to-classes

jagharhemma
Newbie
Posts: 14
Joined: Wed Nov 29, 2017 10:49 am
Contact:

Re: Sound in screens

#5 Post by jagharhemma »

Been trying this for a while and there is something not going as expected. I can get the code to run until " % t_nums[lock1]:", if i exclude this the code works. Also, in my code i cycle through the numbers 0, 1 and 2, can't see that this code will do that

The code works though, i can get the lock to open and it does play a sound!

So, thanks! if i only can get the numbers to cycle, perfection!

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Sound in screens

#6 Post by kivik »

Did you read through everything I wrote that explains how the code works?
lock_increment takes the name of the variable, then using python's global() dictionary to grab the right lock variable, and calculate what the next value should be. The calculation is:

n = (n + 1) % 3

That increments your variable by 1, then it uses the python mod calculation (returns the remainder of a division) to essentially roll your numbers back to 0 when it hits 3.

I just realised why the imagebutton doesn't work - I tested my code with textbuttons, forgetting that imagebuttons uses string substitution to create the idle, hover and insensitive versions. Before I suggest code changes - I need to know if you've changed your filenames first.

jagharhemma
Newbie
Posts: 14
Joined: Wed Nov 29, 2017 10:49 am
Contact:

Re: Sound in screens

#7 Post by jagharhemma »

I really appreciate your help!

Not changed the name of the images!

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Sound in screens

#8 Post by kivik »

In that case you probably need to do this:

Code: Select all

        imagebutton auto "numbers/" + t_nums[lock1] + "_%s.png":
Change the other imagebuttons to lock2 and lock3 as well.


But I'd highly highly recommend you change the filenames so it uses 0,1,2,3,4,5,6,7,8,9 etc. instead of noll, ett, two etc. Because you'd be able to directly translate the in game numeric value into filenames. As it is, I've had to use a dictionary to translate the number into string first.

Post Reply

Who is online

Users browsing this forum: Google [Bot]