Problem with cache and 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
User avatar
runs
Regular
Posts: 41
Joined: Mon Nov 28, 2011 12:31 pm
Location: Spain
Contact:

Problem with cache and sound

#1 Post by runs » Sun Jul 10, 2016 5:32 pm

I have this line:

m "My name is John" #here starts to play the sound of the below Shake function.
m "Whats up"
m "Hi guy"
m "Stop" with Shake((0.5, 1.0, 0.5, 1.0), 1.0, dist=5)

That Shake function is in the official documentation. Simply I've add a sound to this function to play a sound meanwhile the shaking effect.
The problem is in the previous dialogue when the sound is also played. I think this issue is related to cache. How can I avoid it?

Thanks.

User avatar
theCodeCat
Regular
Posts: 62
Joined: Sun Sep 06, 2015 8:40 pm
Projects: Lucid9, Mystic Destinies: Serendipity of Aeons
Skype: theCodeCat
Contact:

Re: Problem with cache and sound

#2 Post by theCodeCat » Sun Jul 10, 2016 6:02 pm

Can you show the code for how you added the sound effect to 'Shake'?

You might have added it in such a way that the sound is triggered when the object is created, and not necessarily when the visual effect is executed.

User avatar
runs
Regular
Posts: 41
Joined: Mon Nov 28, 2011 12:31 pm
Location: Spain
Contact:

Re: Problem with cache and sound

#3 Post by runs » Sun Jul 10, 2016 7:59 pm

Code: Select all

#Shake effect
init:
 python:        
        import math
        class Shaker(object):          
            anchors = {
                'top' : 0.0,
                'center' : 0.5,
                'bottom' : 1.0,
                'left' : 0.0,
                'right' : 1.0,
                }     
            def __init__(self, start, child, dist):
                if start is None:
                    start = child.get_placement()
                #
                self.start = [ self.anchors.get(i, i) for i in start ]  # central position
                self.dist = dist    # maximum distance, in pixels, from the starting point
                self.child = child
            def __call__(self, t, sizes):
                # Float to integer... turns floating point numbers to
                # integers.                
                def fti(x, r):
                    if x is None:
                        x = 0
                    if isinstance(x, float):
                        return int(x * r)
                    else:
                        return x
                xpos, ypos, xanchor, yanchor = [ fti(a, b) for a, b in zip(self.start, sizes) ]
                xpos = xpos - xanchor
                ypos = ypos - yanchor                
                nx = xpos + (1.0-t) * self.dist * (renpy.random.random()*2-1)
                ny = ypos + (1.0-t) * self.dist * (renpy.random.random()*2-1)
                return (int(nx), int(ny), 0, 0)
        def _Shake(start, time, child=None, dist=100.0, **properties):            
            renpy.sound.play("sound/effects/shock2.ogg") 
            move = Shaker(start, child, dist=dist)        
            return renpy.display.layout.Motion(move,
                          time,
                          child,
                          add_sizes=True,
                          **properties) 
        Shake= renpy.curry(_Shake)
Last edited by runs on Mon Jul 11, 2016 5:47 pm, edited 2 times in total.

User avatar
theCodeCat
Regular
Posts: 62
Joined: Sun Sep 06, 2015 8:40 pm
Projects: Lucid9, Mystic Destinies: Serendipity of Aeons
Skype: theCodeCat
Contact:

Re: Problem with cache and sound

#4 Post by theCodeCat » Mon Jul 11, 2016 12:27 am

I think it might work if you move the "renpy.sound.play("sound/effects/shock2.ogg")" line into the __init__ function of the 'Shaker' class.
So something like:

Code: Select all

def __init__(self, start, child, dist):
    renpy.sound.play("sound/effects/shock2.ogg")
    <rest of code>

User avatar
runs
Regular
Posts: 41
Joined: Mon Nov 28, 2011 12:31 pm
Location: Spain
Contact:

Re: Problem with cache and sound

#5 Post by runs » Mon Jul 11, 2016 5:46 pm

Thankx, theCodeCat, but that does not solve the problem.

I've finally done the trick making a function called from the Shake function:

Code: Select all

#Shake effect
init:
 python:       
        import math
        class Shaker(object):         
            anchors = {
                'top' : 0.0,
                'center' : 0.5,
                'bottom' : 1.0,
                'left' : 0.0,
                'right' : 1.0,
                }     
            def __init__(self, start, child, dist):
                if start is None:
                    start = child.get_placement()
                #
                self.start = [ self.anchors.get(i, i) for i in start ]  # central position
                self.dist = dist    # maximum distance, in pixels, from the starting point
                self.child = child
            def __call__(self, t, sizes):
                # Float to integer... turns floating point numbers to
                # integers.               
                def fti(x, r):
                    if x is None:
                        x = 0
                    if isinstance(x, float):
                        return int(x * r)
                    else:
                        return x
                xpos, ypos, xanchor, yanchor = [ fti(a, b) for a, b in zip(self.start, sizes) ]
                xpos = xpos - xanchor
                ypos = ypos - yanchor               
                nx = xpos + (1.0-t) * self.dist * (renpy.random.random()*2-1)
                ny = ypos + (1.0-t) * self.dist * (renpy.random.random()*2-1)
                return (int(nx), int(ny), 0, 0)
        def _Shake(start, time, child=None, dist=100.0, **properties):           
            SoundShock
            move = Shaker(start, child, dist=dist)       
            return renpy.display.layout.Motion(move,
                          time,
                          child,
                          add_sizes=True,
                          **properties)
        def SoundShock(self):
            renpy.sound.play("sound/effects/shock2.ogg")
        Shake= renpy.curry(_Shake)

Post Reply

Who is online

Users browsing this forum: Bing [Bot], enaielei