Raised This Month: $ Target: $400
 0% 

How to check if a handle is valid


Post New Thread Reply   
 
Thread Tools Display Modes
naris
AlliedModders Donor
Join Date: Dec 2006
Old 03-16-2009 , 17:47   Re: How to check if a handle is valid
Reply With Quote #11

I use these stock functions to maintain timers for clients.

PHP Code:
stock Handle:g_ClientTimers[MAXPLAYERS+1];

stock CreateClientTimer(clientFloat:intervalTimer:funcflags=TIMER_REPEAT)
{
    if (
g_ClientTimers[client] == INVALID_HANDLE)
        
g_ClientTimers[client] = CreateTimer(interval,func,client,flags);
}

stock KillClientTimer(client)
{
    new 
Handle:timer=g_ClientTimers[client];
    if (
timer != INVALID_HANDLE)
    {
        
KillTimer(timer);
        
g_ClientTimers[client] = INVALID_HANDLE;    
    }
}

stock KillAllClientTimers()
{
    for (new 
1<= MaxClientsi++)
        
KillClientTimer(i);

Example usage:

PHP Code:
public Action:PlayerSpawnEvent(Handle:event,const String:name[],bool:dontBroadcast)
{
    new 
client GetClientOfUserId(GetEventInt(event,"userid"));
    if (
client 0)
    {
            
CreateClientTimer(client3.0DoSomethingTIMER_REPEAT);
    }
    return 
Plugin_Continue;
}

public 
OnPlayerDeathEvent(Handle:event,const String:name[],bool:dontBroadcast)
{
    new 
client GetClientOfUserId(GetEventInt(event,"userid"));
    if (
client 0)
    {
        
KillClientTimer(client);
    }


Last edited by naris; 03-16-2009 at 17:51.
naris is offline
naris
AlliedModders Donor
Join Date: Dec 2006
Old 03-16-2009 , 17:55   Re: How to check if a handle is valid
Reply With Quote #12

Quote:
Originally Posted by FeuerSturm View Post
I'm pretty sure that you can't use KillTimer or CloseHandle on a repeating
timer as the handle changes everytime it's run again.

Just stop the timer with "return Plugin_Stop" within the callback and you should be fine.
I never had a problem using KillTimer() on a repeating timer, as long as you don't try to kill it using KillTimer() AND stop it by returning Plugin_Stop. Use either one method OR the other, but not BOTH or one will kill the timer (probably the Plugin_Stop) and the other will error out (probably the KillTimer()) since the timer was already closed.
naris is offline
naris
AlliedModders Donor
Join Date: Dec 2006
Old 03-16-2009 , 18:04   Re: How to check if a handle is valid
Reply With Quote #13

Quote:
Originally Posted by paegus View Post
the main timer, that repeats is doing most of the work, and firing very frequently (every 0.1 seconds). i'd like to close those timers that i don't need anymore. for dead players or trip alarms that haven't been triggered in the past however many seconds...
The main timer (tmr_DrawSprites() I presume) might not be repeating at all. THe timers are created with TIMER_REPEAT, but it doesn't return anything. To get the timer to actually repeat, you should return Plugin_Continue.

If the timer ends, that could be causing your errors.
naris is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 03-17-2009 , 10:14   Re: How to check if a handle is valid
Reply With Quote #14

If you don't return anything in a function tagged Action, it's treated as if you returned Plugin_Continue, so his timer will repeat (though he really should return it himself for clarity).

Error 3 (from the original post) means the handle was already freed when SM went to free it. This probably means the script is calling CloseHandle on a timer during that timer's own callback (tmr_DrawSprites).
Fyren is offline
kim_perm
Member
Join Date: Sep 2007
Location: Perm, Russia
Old 02-08-2012 , 14:40   Re: How to check if a handle is valid
Reply With Quote #15

At the same time, how about plugin handle, when plugin is unloaded?
kim_perm 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 20:42.


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