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.

PlaySound spamming defined sound

Postby cinnagoat_ » Wed Sep 08, 2021 11:01 pm

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

Code: Select allExpand view
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!
User avatar
cinnagoat_
 
Joined: 07 Dec 2020

Re: PlaySound spamming defined sound

Postby Jarewill » Thu Sep 09, 2021 10:46 am

Without seeing the rest of the script, there's not much we can do.
Please post the rest of your script.
Jarewill
 
 
 
Joined: 21 Jul 2019

Re: PlaySound spamming defined sound

Postby cinnagoat_ » Thu Sep 09, 2021 9:26 pm

Code: Select allExpand view
#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
cinnagoat_
 
Joined: 07 Dec 2020

Re: PlaySound spamming defined sound

Postby MFG38 » Fri Sep 10, 2021 7:27 am

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 allExpand view
If(combokills==2){combomessage="x2"; PlaySound(0,"combosnd",CHAN_BODY,1.0,FALSE,0);}
User avatar
MFG38
 
Joined: 14 Apr 2019
Location: Finland
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia (Modern GZDoom)

Re: PlaySound spamming defined sound

Postby Jarewill » Fri Sep 10, 2021 9:46 am

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 allExpand view
    Script "ComboGet" (void)
    {
        
combokills++; //Add to the counter
        
combotimer=130;
        
PlaySound(0,"combosnd",CHAN_BODY,1.0,FALSE,0);
    } 
Jarewill
 
 
 
Joined: 21 Jul 2019


Return to Scripting

Who is online

Users browsing this forum: Apeirogon, Hey Doomer and 1 guest