Raised This Month: $32 Target: $400
 8% 

[CSS] Question with timer


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Amodd
Junior Member
Join Date: Dec 2012
Old 12-26-2021 , 06:29   [CSS] Question with timer
Reply With Quote #1

Hey,
Is this a proper way to kill timer repeat for every player?
PHP Code:
Handle g_NoblockTimers[MAXPLAYERS 1];

public 
void OnClientPutInServer(int client)
{
    
g_NoblockTimers[client] = CreateTimer(0.2CheckClientGetClientUserId(client), TIMER_REPEAT);
}

public 
Action CheckClient(Handle Timerint userid)
{
    
int client GetClientOfUserId(userid);
    if(
IsValidClient(client) && IsPlayerAlive(client))
    {
        
blablabla
    
}
    return 
Plugin_Continue;
}

public 
void OnMapEnd()
{
    for(
int client 1client <= MaxClientsclient++)
    {
        if(
g_NoblockTimers[client] != INVALID_HANDLE)
        {
            
KillTimer(g_NoblockTimers[client]);
        }
        
g_NoblockTimers[client] = INVALID_HANDLE;
    }
}

public 
OnClientDisconnect(client)
{
    if(
g_NoblockTimers[client] != INVALID_HANDLE)
    {
        
KillTimer(g_NoblockTimers[client]);
    }
    
g_NoblockTimers[client] = INVALID_HANDLE;

Amodd is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 12-26-2021 , 09:09   Re: [CSS] Question with timer
Reply With Quote #2

yeah, that is one way to do it.

You can also delete active timer.
- This will null Handle as well.




I'm not sure what you do with your timers.
But I throw another timer example. Not need use tough.
PHP Code:
Handle playertimers[MAXPLAYERS+1];

public 
void OnClientPutInServer(int client)
{
    
playertimers[client] = CreateTimer(1.0timerGetClientUserId(client), TIMER_REPEAT);
}


public 
Action timer(Handle timerany data)
{
    
int client GetClientOfUserId(data);

    
// Can we still get same player index by userid ?
    
if(client == 0)
    {
        
// - client have reconnected or disconnected
        
return Plugin_Stop;    // Stop this timer only
    
}


    
// We have found player.
    // Is this same old timer as stored timer in Handle playertimers[] ?
    
if(timer != playertimers[client])
    {
        
// - new timer have created in Handle playertimers[]
        
return Plugin_Stop;    // Stop this old timer only
    
}

    
// Avoid some errors
    
if(!IsClientInGame(client))
    {
        return 
Plugin_Continue;
    }


    
// Do your code
    
PrintToServer("%N timer"client);

    return 
Plugin_Continue;

__________________
Do not Private Message @me
Bacardi is offline
Amodd
Junior Member
Join Date: Dec 2012
Old 12-26-2021 , 10:33   Re: [CSS] Question with timer
Reply With Quote #3

Thanks, this is for noblock. I want to avoid OnGameFrame thats why i use timers

Ok, i added Your code ;) Will it be ok with this stock IsValidClient or i should edit it?
Do we still need killtimer OnClientDisconnect(client) if we have: if(client == 0)?

Second question: Could You tell me if this can lag the server(by using timer 0.2 for every player)?

PHP Code:
Handle g_NoblockTimers[MAXPLAYERS 1];

public 
void OnClientPutInServer(int client)
{
    
g_NoblockTimers[client] = CreateTimer(0.2timerGetClientUserId(client), TIMER_REPEAT);
}

public 
Action timer(Handle timerint userid)
{
    
int client GetClientOfUserId(userid);

    
// Can we still get same player index by userid ?
    
if(client == 0)
    {
        
// - client have reconnected or disconnected
        
return Plugin_Stop;    // Stop this timer only
    
}

    
// We have found player.
    // Is this same old timer as stored timer in Handle playertimers[] ?
    
if(timer != g_NoblockTimers[client])
    {
        
// - new timer have created in Handle playertimers[]
        
return Plugin_Stop;    // Stop this old timer only
    
}

    
// Avoid some errors
    
if(!IsClientInGame(client))
    {
        return 
Plugin_Continue;
    }

    if(
IsValidClient(client) && IsPlayerAlive(client))
    {
        
blablabla
    
}
    return 
Plugin_Continue;
}

public 
void OnMapEnd()
{
    for(
int client 1client <= MaxClientsclient++)
    {
        if(
g_NoblockTimers[client] != INVALID_HANDLE)
        {
            
KillTimer(g_NoblockTimers[client]);
        }
        
g_NoblockTimers[client] = INVALID_HANDLE;
    }
}

public 
OnClientDisconnect(client)
{
    if(
g_NoblockTimers[client] != INVALID_HANDLE)
    {
        
KillTimer(g_NoblockTimers[client]);
    }
    
g_NoblockTimers[client] = INVALID_HANDLE;
}

stock bool IsValidClient(int client
{
    if (!(
<= client <= MaxClients) || !IsClientInGame(client) || IsClientSourceTV(client))
        return 
false
    return 
true


Last edited by Amodd; 12-26-2021 at 10:42.
Amodd is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 12-26-2021 , 10:44   Re: [CSS] Question with timer
Reply With Quote #4

it was just a example.
Don't use it.

Because in that example it not clear handle, which give you a lot errors to try KillTimer which not exist anymore.
Example stop timer when some conditions match.

Stick your original code for now.

*edit
I'm thinking, do you need store timers in global handle after all.
You could just create timer, passing player userid. Then you continue timer as long as userid still exist.
*because OnClientPutInServer, this will create multiple timers then... not good.

Last edited by Bacardi; 12-26-2021 at 11:00.
Bacardi is offline
Amodd
Junior Member
Join Date: Dec 2012
Old 12-26-2021 , 11:02   Re: [CSS] Question with timer
Reply With Quote #5

Quote:
Originally Posted by Bacardi View Post
it was just a example.
Don't use it.

Because in that example it not clear handle, which give you a lot errors to try KillTimer which not exist anymore.
Example stop timer when some conditions match.

Stick your original code for now.
Ok thanks, i will be testing it now.

Quote:
Originally Posted by Bacardi View Post
*edit
I'm thinking, do you need store timers in global handle after all.
You could just create timer, passing player userid. Then you continue timer as long as userid still exist.
Any example how to do it?
Amodd is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 12-26-2021 , 11:36   Re: [CSS] Question with timer
Reply With Quote #6

Problem in my idea was that OnClientPutInServer, it is called multiple times (every map change), so there would be multiple timers on one person then.


But try this.
- You not need kill timer on other functions, just check is client in game and stop timer if not.
Important is clear handle after stopping timer.

- Passing client index instead userid, because repeating timer do fast frequenzy check... it's not efficiency to get client index every time in short time :/

PHP Code:


Handle g_NoblockTimers
[MAXPLAYERS+1];


public 
void OnPluginStart()
{
    
// When plugin reload in middle of gameplay,
    // create timers to players in server
    
for(int i 1<= MaxClientsi++)
    {
        if(
IsClientInGame(i))
        {
            
OnClientPutInServer(i);
        }
    }
}


public 
void OnClientPutInServer(int client)
{
    if(
IsClientSourceTV(client) || IsClientReplay(client))
        return;


    
delete g_NoblockTimers[client]; // Kills active timer if it exist

    
g_NoblockTimers[client] = CreateTimer(0.2noblocktimerclientTIMER_REPEAT);
}


public 
Action noblocktimer(Handle timerany client)
{
    if(!
IsClientInGame(client))
    {
        
//    client not in game
        // stop timer and clear handle
        
g_NoblockTimers[client] = null;
        return 
Plugin_Stop;
    }


    if(
timer != g_NoblockTimers[client])
    {
        
//    - Timer differ from global variable
        //    - This should not happen, but it is for backup.
        //    - There maybe new timer set in variable, so do not clear Handle
        
return Plugin_Stop;
    }



    
// Do your code
    
PrintToServer("%N timer %d"clienttimer);


    return 
Plugin_Continue;

__________________
Do not Private Message @me

Last edited by Bacardi; 12-26-2021 at 11:43. Reason: forgot //g_NoblockTimers
Bacardi is offline
Amodd
Junior Member
Join Date: Dec 2012
Old 12-26-2021 , 13:03   Re: [CSS] Question with timer
Reply With Quote #7

Wow, didn't know OnClientPutInServer, is called multiple times (every map change). Very clever with this OnClientPutInServer to call it OnPluginStart ;)
I will change GetClientUserId(client) to just 'client' in create timer.

As i understand well, this isn't needed anymore with your code?:
PHP Code:
public void OnMapEnd()
{
    for(
int client 1client <= MaxClientsclient++)
    {
        if(
g_NoblockTimers[client] != INVALID_HANDLE)
        {
            
KillTimer(g_NoblockTimers[client]);
        }
        
g_NoblockTimers[client] = INVALID_HANDLE;
    }
}

public 
OnClientDisconnect(client)
{
    if(
g_NoblockTimers[client] != INVALID_HANDLE)
    {
        
KillTimer(g_NoblockTimers[client]);
    }
    
g_NoblockTimers[client] = INVALID_HANDLE;

One more question:
PHP Code:
if(timer != g_NoblockTimers[client])
    {
        
//    - Timer differ from global variable
        //    - This should not happen, but it is for backup.
        //    - There maybe new timer set in variable, so do not clear Handle
        
return Plugin_Stop;
    } 
or
PHP Code:
if(noblocktimer != g_NoblockTimers[client])
    {
        
//    - Timer differ from global variable
        //    - This should not happen, but it is for backup.
        //    - There maybe new timer set in variable, so do not clear Handle
        
return Plugin_Stop;
    } 
I checked and when i take 2nd option, there is error compiling: error 100: function prototypes do not match

Last edited by Amodd; 12-26-2021 at 13:47.
Amodd is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 12-26-2021 , 14:12   Re: [CSS] Question with timer
Reply With Quote #8

Quote:
Originally Posted by Amodd View Post
Wow, didn't know OnClientPutInServer, is called multiple times (every map change).
Every time when player connect (connect, reconnect, map change)
- Using server event "player_connect" and "player_disconnect" will called less times, player truly disconnect and connect to server.

Quote:
Originally Posted by Amodd View Post
Very clever with this OnClientPutInServer to call it OnPluginStart ;)
Learned from Masters of the Universe
psychonic #1

Quote:
Originally Posted by Amodd View Post
I will change GetClientUserId(client) to just 'client' in create timer.

As i understand well, this isn't needed anymore with your code?:
PHP Code:
public void OnMapEnd()
{
    for(
int client 1client <= MaxClientsclient++)
    {
        if(
g_NoblockTimers[client] != INVALID_HANDLE)
        {
            
KillTimer(g_NoblockTimers[client]);
        }
        
g_NoblockTimers[client] = INVALID_HANDLE;
    }
}

public 
OnClientDisconnect(client)
{
    if(
g_NoblockTimers[client] != INVALID_HANDLE)
    {
        
KillTimer(g_NoblockTimers[client]);
    }
    
g_NoblockTimers[client] = INVALID_HANDLE;

Not need, in my second example.
Just continue coding from // Do your code

Quote:
Originally Posted by Amodd View Post
One more question:
PHP Code:
if(timer != g_NoblockTimers[client])
    {
        
//    - Timer differ from global variable
        //    - This should not happen, but it is for backup.
        //    - There maybe new timer set in variable, so do not clear Handle
        
return Plugin_Stop;
    } 
or
PHP Code:
if(noblocktimer != g_NoblockTimers[client])
    {
        
//    - Timer differ from global variable
        //    - This should not happen, but it is for backup.
        //    - There maybe new timer set in variable, so do not clear Handle
        
return Plugin_Stop;
    } 
I checked and when i take 2nd option, there is error compiling: error 100: function prototypes do not match
noblocktimer is name of timer callback.
Code:
...
g_NoblockTimers[client] = CreateTimer(0.2, noblocktimer, client, TIMER_REPEAT);
...
public Action noblocktimer(Handle timer, any client)
{
...
- Timer callback carries Handle called "timer", it is like address. You can keep track which timer callback is executed.
__________________
Do not Private Message @me
Bacardi is offline
Amodd
Junior Member
Join Date: Dec 2012
Old 12-26-2021 , 17:09   Re: [CSS] Question with timer
Reply With Quote #9

Thanks Bacardi for your advices and code. You are great!!!
Now i am going to write rest of the plugin ;)
Amodd is offline
azalty
AlliedModders Donor
Join Date: Feb 2020
Location: France
Old 12-26-2021 , 21:48   Re: [CSS] Question with timer
Reply With Quote #10

Bacardi's code but modified just a bit

No checks inside the timer function

PHP Code:
Handle g_NoblockTimers[MAXPLAYERS+1];


public 
void OnPluginStart()
{
    
// When plugin reload in middle of gameplay,
    // create timers to players in server
    
for(int i 1<= MaxClientsi++)
    {
        if(
IsClientInGame(i))
        {
            
OnClientPutInServer(i);
        }
    }
}

public 
void OnClientPutInServer(int client)
{
    if (
IsFakeClient(client)) // If the client is a bot
        
return;

    
g_NoblockTimers[client] = CreateTimer(0.2noblocktimerclientTIMER_REPEAT);
}

public 
void OnClientDisconnect(int client)
{
    
delete g_NoblockTimers[client]; // Kills active timer if it exist, and sets the Handle to null
}

Action noblocktimer(Handle timerany client)
{
    
// Do your code

    
return Plugin_Continue;

__________________
GitHub | Discord: @azalty | Steam
azalty 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 00:59.


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