Raised This Month: $51 Target: $400
 12% 

Make sound not play so often


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
HowToRuski
Senior Member
Join Date: Feb 2019
Location: Hungary
Old 04-19-2023 , 05:57   Make sound not play so often
Reply With Quote #1

Hello, so basically i have a script which plays a sound when player leaves/joins the server, i wanted to ask if there is a way i can make it not play so often? Because if more players start joining it overlays more sounds and make a very bad sounding itself. Like make a check if sound was played like in 30 sec interval then not play another?
HowToRuski is offline
Hakim Azizov
Member
Join Date: Mar 2023
Old 04-19-2023 , 08:02   Re: Make sound not play so often
Reply With Quote #2

Quote:
Originally Posted by HowToRuski View Post
Hello, so basically i have a script which plays a sound when player leaves/joins the server, i wanted to ask if there is a way i can make it not play so often? Because if more players start joining it overlays more sounds and make a very bad sounding itself. Like make a check if sound was played like in 30 sec interval then not play another?
Hi. I can help you but you need sound(s) for playing it. Please wait me
Hakim Azizov is offline
Hakim Azizov
Member
Join Date: Mar 2023
Old 04-19-2023 , 08:26   Re: Make sound not play so often
Reply With Quote #3

Quote:
Originally Posted by HowToRuski View Post
Hello, so basically i have a script which plays a sound when player leaves/joins the server, i wanted to ask if there is a way i can make it not play so often? Because if more players start joining it overlays more sounds and make a very bad sounding itself. Like make a check if sound was played like in 30 sec interval then not play another?
Attached Files
File Type: sma Get Plugin or Get Source (join-drop sound.sma - 67 views - 2.1 KB)
Hakim Azizov is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-20-2023 , 00:43   Re: Make sound not play so often
Reply With Quote #4

You can add a type of timer that will prevent replaying the sound within X seconds. You can implement this using something like this:
  • Create a global boolean variable to control if the sound can be played (default value should be true)
  • If the variable is true, play the sound and set the variable to false (otherwise, do nothing)
  • Set a timer (e.g. with set_task()) for X seconds that will then reset the the variable back to true.

Try to implement this on your own (this is the Scripting Help section after all) and see if you can get it working. If you have issues, post the code that you tested and we can help you understand how to fix it.
__________________
fysiks is offline
HowToRuski
Senior Member
Join Date: Feb 2019
Location: Hungary
Old 04-20-2023 , 05:48   Re: Make sound not play so often
Reply With Quote #5

Quote:
Originally Posted by fysiks View Post
You can add a type of timer that will prevent replaying the sound within X seconds. You can implement this using something like this:
  • Create a global boolean variable to control if the sound can be played (default value should be true)
  • If the variable is true, play the sound and set the variable to false (otherwise, do nothing)
  • Set a timer (e.g. with set_task()) for X seconds that will then reset the the variable back to true.

Try to implement this on your own (this is the Scripting Help section after all) and see if you can get it working. If you have issues, post the code that you tested and we can help you understand how to fix it.
Got something like this

PHP Code:
new g_bPlaySound true;

public 
PlaySoundDelay(id)
{
    if (
g_bPlaySound)
    {
        
// Play the sound here

        // Set the variable to false
        
g_bPlaySound false;

        
set_task(30.0"ResetSoundVariable"id);
    }
}

public 
ResetSoundVariable(id)
{
    
g_bPlaySound true;

Would this work?
HowToRuski is offline
mlibre
Veteran Member
Join Date: Nov 2015
Location: return PLUGIN_CONTINUE
Old 04-20-2023 , 11:29   Re: Make sound not play so often
Reply With Quote #6

or

PHP Code:
public client_putinserver(id)
{
    static 
iTimestampiWaitNext
            
    iTimestamp 
get_systime()
            
    if(
iWaitNext iTimestamp) return
            
    
iWaitNext iTimestamp 30
    
    client_cmd
(0"spk buttons/bell1")

__________________
mlibre is online now
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-20-2023 , 23:16   Re: Make sound not play so often
Reply With Quote #7

Quote:
Originally Posted by mlibre View Post
or

PHP Code:
public client_putinserver(id)
{
    static 
iTimestampiWaitNext
            
    iTimestamp 
get_systime()
            
    if(
iWaitNext iTimestamp) return
            
    
iWaitNext iTimestamp 30
    
    client_cmd
(0"spk buttons/bell1")

Yeah, this method is better (except having the return in the middle of a function for no good reason, just use the if statement to conditionally execute the code, it'll be much more readable and easier to integrate into existing code). I do this type of thing all the time so I'm not sure why I didn't remember this method when responding the first time.
__________________
fysiks is offline
mlibre
Veteran Member
Join Date: Nov 2015
Location: return PLUGIN_CONTINUE
Old 04-25-2023 , 11:49   Re: Make sound not play so often
Reply With Quote #8

Quote:
Originally Posted by fysiks View Post
Yeah, this method is better (except having the return in the middle of a function for no good reason, just use the if statement to conditionally execute the code, it'll be much more readable and easier to integrate into existing code). I do this type of thing all the time so I'm not sure why I didn't remember this method when responding the first time.
how would you optimize it because it is not a good practice to return after the condition
__________________
mlibre is online now
Jhob94
AMX Mod X Donor
Join Date: Jul 2012
Old 04-25-2023 , 14:00   Re: Make sound not play so often
Reply With Quote #9

Just switch ‘>’ with ‘<‘ and remove the return
It’s cleaner that way. Like he said it’s easier to read and to add to an existing code. Imagine he wants to do something else on client_putinserver, that return would stop it. It’s a good coding habit to avoid returns, only do it when it’s necessary, in this case you don’t need it at all
__________________
Jhob94 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-26-2023 , 00:44   Re: Make sound not play so often
Reply With Quote #10

I assume you already know but just in case anyone is reading this in the future who is less experienced, in addition to changing the condition you need to make sure to wrap the conditional code in braces { }.
__________________
fysiks is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:16.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode