function works twice

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
Wertous
Regular
Posts: 27
Joined: Fri Aug 09, 2019 8:47 am
Contact:

function works twice

#1 Post by Wertous »

Hello everyone, I had one unforeseen problem

In my opinion, in the window I need, when the value of a variable of a certain value, an audio signal should be played

I decided that it would be best if the sound I call the function and it will work if the desired value of the variable
But the whole problem is that for some reason the function works twice

Code: Select all

init python:
    def play_1():
            global udar_1
            global test_1
            
            
            
            if udar_1==True:
            
                renpy.sound.play( "music/wave/udar_lico.mp3")
                test_1+=1
            if udar_1==False:
                renpy.sound.stop()
                test_1-=1        
            return()
variable test introduced to verify this fact

Code: Select all

screen fight_2():

    
    
        
                    
    if udar_1==True: 
        
        add  "images/fight/bez_or/L_udarv_gol_1.png"xpos 500 ypos 200
        add Animation ("images/fight/bez_or/r_poz_baze_1.png",1.0,"images/fight/bez_or/r_poz_baze_2.png",1.0)xpos 700 ypos 200
        $play_1()
        timer fast repeat True action SetVariable("udar_1",False),SetVariable("xz",True)

Code: Select all

default udar_1=False
default test_1=5
If it’s not difficult, tell me what my error is and why the function works twice

DragoonHP
Miko-Class Veteran
Posts: 758
Joined: Tue Jun 22, 2010 12:54 am
Completed: Christmas
IRC Nick: DragoonHP
Location: Zion Island, Solario
Contact:

Re: function works twice

#2 Post by DragoonHP »

Try this

Code: Select all

screen fight_2():
                    
    if udar_1==True:
        on "show":
            action Function(play_1)
        add  "images/fight/bez_or/L_udarv_gol_1.png" xpos 500 ypos 200
        add Animation ("images/fight/bez_or/r_poz_baze_1.png",1.0,"images/fight/bez_or/r_poz_baze_2.png",1.0) xpos 700 ypos 200
        timer fast repeat True action SetVariable("udar_1",False), SetVariable("xz",True)
Screens are evaluated multiple times, so the play_1 was being run multiple times. So we use an on statement to ensure that the function is run only once, when the screen is first loaded.

Wertous
Regular
Posts: 27
Joined: Fri Aug 09, 2019 8:47 am
Contact:

Re: function works twice

#3 Post by Wertous »

Thank you very much for the advice, but for some reason it doesn’t work for me as you wrote

In principle, I also realized that the problem is that the udar_1 variable is checked twice

I found a way out by declaring the declaration of the play_1 function before checking the value of the udar_1 variable

Code: Select all

    if xz==True:
        
        add Animation ("images/fight/bez_or/f_baze_1L.png",1.0,"images/fight/bez_or/f_baze_2L.png",1.0)xpos 300 ypos 200
        add Animation ("images/fight/bez_or/r_poz_baze_1.png",1.0,"images/fight/bez_or/r_poz_baze_2.png",1.0)xpos 700 ypos 200
        $play_1(a="music/wave/udar_lico.mp3")
        
        
        
        
        
                    
    if udar_1==True: 
        
        
        
        add  "images/fight/bez_or/L_udarv_gol_1.png"xpos 525 ypos 250
        add "images/fight/bez_or/R_uron_gol.png"xpos 650 ypos 250
      
        
        
        timer fast repeat True action SetVariable("udar_1",False),SetVariable("xz",True)
So everything works while

DragoonHP
Miko-Class Veteran
Posts: 758
Joined: Tue Jun 22, 2010 12:54 am
Completed: Christmas
IRC Nick: DragoonHP
Location: Zion Island, Solario
Contact:

Re: function works twice

#4 Post by DragoonHP »

$play_1(a="music/wave/udar_lico.mp3")
You have defined it above to. Please replace it with the on "show" block.

Post Reply

Who is online

Users browsing this forum: No registered users