PlaySound spamming defined sound

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
User avatar
cinnagoat_
Posts: 9
Joined: Mon Dec 07, 2020 3:35 pm

PlaySound spamming defined sound

Post by cinnagoat_ »

I have a script that runs when a condition is met like this.

Code: Select all

If(combokills==1){combomessage="x1";}  
If(combokills==2){combomessage="x2";} PlaySound(0,"combo2sound",CHAN_BODY,1.0,FALSE,0);
The sound indeed plays when tested with playsound in console, but when combokills==2, it spams the sound over and over until the script stops.

I understand that it has to do with it constantly activating it since it is an IF, but how can I stifle it so that it only plays once.

I've read that somebody used GiveInventory to play the sound prompt, then remove it from the activator, but I know theres most likely a simple solution to this.

Thank you!
Jarewill
Posts: 1334
Joined: Sun Jul 21, 2019 8:54 am

Re: PlaySound spamming defined sound

Post by Jarewill »

Without seeing the rest of the script, there's not much we can do.
Please post the rest of your script.
User avatar
cinnagoat_
Posts: 9
Joined: Mon Dec 07, 2020 3:35 pm

Re: PlaySound spamming defined sound

Post by cinnagoat_ »

Code: Select all

#library "SCORE"
#include "zcommon.acs"

int combokills; //Amount of kills
int combotimer; //Timer before combo ends

Script "ComboSystem" ENTER
{
    str combomessage;
    While(true)
    {
        If(combokills>0)
        {
		    SetFont("COMBO01");
            HudMessage(s:"A"; HUDMSG_PLAIN, 0, CR_UNTRANSLATED, 1.5, 0.1, 1873);
		//last value was 3.7
            If(combokills==1){combomessage="x1";}  
            If(combokills==2){combomessage="x2";} PlaySound(0,"combosnd",CHAN_BODY,1.0,FALSE,0); 
            If(combokills==3){combomessage="x3";}
            If(combokills==4){combomessage="x4";}
            If(combokills==5){combomessage="x5";}
            If(combokills==6){combomessage="x6";}
            If(combokills==7){combomessage="x7";}
            If(combokills==8){combomessage="x8";}
            If(combokills==9){combomessage="x9";}
			If(combokills==10){combomessage="x10";}
			If(combokills==11){combomessage="x11";}
			If(combokills==12){combomessage="x12";}
			If(combokills==13){combomessage="x13";}
			If(combokills==14){combomessage="x14";}
			If(combokills==15){combomessage="x15";}
			If(combokills==16){combomessage="x16";}
			If(combokills==17){combomessage="x17";}
			If(combokills==18){combomessage="x18";}
			If(combokills==19){combomessage="x19";}
			If(combokills==20){combomessage="x20";}
            If(combokills>=21){combomessage="WHAT!?";}
			
			
			
			
			//Combo Counter HUD Element (MAIN ONE)
            SetFont("BIGFONT");
            HudMessage(s:combomessage; HUDMSG_PLAIN, 100, 0, 0.1, 0.2, 0);
            int time = Ceil(FixedDiv(combotimer<<16,35.0));
			//Val after PLAIN is 100
            
			
			
			
			
			
            If(combotimer<=0){combokills=0;} //When timer runs out, combo ends
            Else{combotimer--;}
        }
        Else
        {
		    SetFont("NULLHUD");
            HudMessage(s:" "; HUDMSG_PLAIN, 100, CR_UNTRANSLATED, 0.1, 0.8, 3.7);
			HudMessage(s:" "; HUDMSG_PLAIN, 100, 0, 0.5, 0.2, 0);
            HudMessage(s:" "; HUDMSG_PLAIN, 101, 0, 0.5, 0.2, 0);
        }
        Delay(1);
    }
}

Script "ComboGet" (void)
{
    combokills++; //Add to the counter
    combotimer=130; 
} 
Funny enough is that you helped me out with this exact script months ago, Jarewill! :D
User avatar
MFG38
Posts: 392
Joined: Sun Apr 14, 2019 8:26 am
Graphics Processor: nVidia (Modern GZDoom)
Location: Finland

Re: PlaySound spamming defined sound

Post by MFG38 »

I believe your sound is playing in an endless loop because your call to the PlaySound function is placed outside the brackets for the respective if statement. I don't know if the fix is going to be as simple as this, but moving the right bracket of the if statement on that line to the right of the PlaySound function is a start at the very least. Like so:

Code: Select all

If(combokills==2){combomessage="x2"; PlaySound(0,"combosnd",CHAN_BODY,1.0,FALSE,0);}
Jarewill
Posts: 1334
Joined: Sun Jul 21, 2019 8:54 am

Re: PlaySound spamming defined sound

Post by Jarewill »

As MFG said, your PlaySound call is outside any brackets, so it will play no matter the combokills value.
Now I have two ideas for fixes, assuming you only want your sound to play once.
You can do a lot of checks with a custom variable to make sure a sound only plays once:
Spoiler:
Or an even easier way would be to just add the sound to the ComboGet script:

Code: Select all

    Script "ComboGet" (void)
    {
        combokills++; //Add to the counter
        combotimer=130;
        PlaySound(0,"combosnd",CHAN_BODY,1.0,FALSE,0);
    } 

Return to “Scripting”