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

How to limit only 1 timer work for every player?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
gilmon
Senior Member
Join Date: Feb 2011
Location: China
Old 06-10-2019 , 01:47   How to limit only 1 timer work for every player?
Reply With Quote #1

For example, when player join the server ever few seconds show a message(e.g. "welcome"), you created a repeat timer for every 60s, but after few minutes this become crazy, you will see the message just for 3-5 seconds(even 1-2 second) once like:

Code:
22:15:33 msg:welcome
22:15:34 msg:welcome
22:15:39 msg:welcome
22:15:44 msg:welcome
22:15:48 msg:welcome
...
any help about this?

P.S:the problem is not about PrintToChat or PrintToChatAll, the only problem is timer will be more faster and faster and create a lot of copy itself.

Last edited by gilmon; 06-10-2019 at 01:53. Reason: more desc
gilmon is offline
Send a message via MSN to gilmon
8guawong
AlliedModders Donor
Join Date: Dec 2013
Location: BlackMarke7
Old 06-10-2019 , 03:31   Re: How to limit only 1 timer work for every player?
Reply With Quote #2

Quote:
Originally Posted by gilmon View Post
For example, when player join the server ever few seconds show a message(e.g. "welcome"), you created a repeat timer for every 60s, but after few minutes this become crazy, you will see the message just for 3-5 seconds(even 1-2 second) once like:

Code:
22:15:33 msg:welcome
22:15:34 msg:welcome
22:15:39 msg:welcome
22:15:44 msg:welcome
22:15:48 msg:welcome
...
any help about this?

P.S:the problem is not about PrintToChat or PrintToChatAll, the only problem is timer will be more faster and faster and create a lot of copy itself.
show us the code you have now
__________________
8guawong is offline
lugui
Senior Member
Join Date: Feb 2016
Location: GetClientAbsOrigin();
Old 06-10-2019 , 08:32   Re: How to limit only 1 timer work for every player?
Reply With Quote #3

I'm assuming you are doing something like:

PHP Code:
public void OnClientPutInServer(int client){
    
WelcomeTimers[client] = CreateTimer(15.0WelcomePlayerclientTIMER_REPEAT);
}

public 
Action WelcomePlayer(Handle timerany client){
    
PrintToConsole(client"Welcome to the server!");

(edited copy paste from: https://wiki.alliedmods.net/Timers_(SourceMod_Scripting))

If you are doing this you problem is that you are not clearing the timers.

think about this situation:

a player joins and is assigned the player index 1
you create a timer for player index 1
player index 1 leaves the server
other player joins the server and is assigned player index 1
another timer for player index 1 is created

Now you have two timers for the same player. since you are not clearing the timer handles, ths process will continue until your server stops.

what you sould have done is:

PHP Code:
Handle WelcomeTimers[MAXPLAYERS+1];
 
public 
void OnClientPutInServer(int client)
{
    
WelcomeTimers[client] = CreateTimer(15.0WelcomePlayerclient);
}
 
public 
void OnClientDisconnect(int client)
{
    if (
WelcomeTimers[client] != null)
    {
        
KillTimer(WelcomeTimers[client]);
        
WelcomeTimers[client] = null;
    }
}
 
public 
Action WelcomePlayer(Handle timerany client)
{
    
PrintToConsole(client"Welcome to the server!");
    
WelcomeTimers[client] = null;

(copy pasted from the wiki)
Note the variable WelcomeTimers that stores the timer handles and how it is cleared by running KillTimer() and then seting it to null.

In this example, the timer handle is set to null on its action WelcomePlayer but in this example, it is not a repeating timer so it is safe to just set the handle null as it is already done. The KillTimer is still needed in case the player leaves the server before the timer runs.
__________________
Add me for commissions!
Steam: [U:1:88621772]
Discord: lugui#0889
My Plugins
lugui is offline
Silvers
SourceMod Plugin Approver
Join Date: Aug 2010
Location: SpaceX
Old 06-10-2019 , 09:35   Re: How to limit only 1 timer work for every player?
Reply With Quote #4

I think
PHP Code:
KillTimer(WelcomeTimers[client]);
WelcomeTimers[client] = null
can be replaced with:

PHP Code:
delete WelcomeTimers[client]; 
This will kill the timer and set the handle to null.
__________________

Last edited by Silvers; 06-10-2019 at 09:35.
Silvers is offline
Reply


Thread Tools
Display Modes

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 14:36.


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