Thread: [Solved] Invalid Timer Handle
View Single Post
condolent
AlliedModders Donor
Join Date: Jan 2016
Location: gc_sLocation;
Old 10-06-2017 , 02:13   Re: Invalid Timer Handle
Reply With Quote #17

Quote:
Originally Posted by Bacardi View Post
Here my example...

Two Handle to handle two timers.

Warmup timer start on round start, and if this timer is still active while player kill another player, timer reset.
If warmuptimer still active when round start, timer reset.
When warmup timer time reach end, it create repeated smoketimer.

If smoketimer is active, it stop on round start.


PHP Code:
Handle hWarmuptimer;
Handle hSmoketimer;

public 
void OnPluginStart()
{

    
HookEventEx("round_start"round_start);
    
HookEventEx("player_death"player_death);
}


public 
void round_start(Event event, const char[] namebool dontBroadcast)
{
    
// SmokeTimer currently running, active!
    // Lets clear this repeated timer Handle, it will stop itself :D
    
if(hSmoketimer != nullhSmoketimer null;



    
// If Warmuptimer still running, active!
    // Kill timer
    
if(hWarmuptimer != nullKillTimer(hWarmuptimer);

    
hWarmuptimer CreateTimer(20.0warmuptimer_cb); // create new on round start
}

public 
void player_death(Event event, const char[] namebool dontBroadcast)
{
    
int victim GetClientOfUserId(event.GetInt("userid"));
    
int attacker GetClientOfUserId(event.GetInt("attacker"));

    
// killer not world or player suicide (no teamkill check here now)
    
if(attacker == || attacker == victim) return;


    
// Timer is currently running, active! We will reset timer.
    
if(hWarmuptimer != null)
    {
        
KillTimer(hWarmuptimer); // Remove current timer
        
hWarmuptimer CreateTimer(20.0warmuptimer_cb); // Create new timer in this same Handle
    
}
}

public 
Action warmuptimer_cb(Handle timer)
{
    
// Ok, this timer reach end and callback has triggered.
    // Lets also clear (null) this timer Handle to make sure that this timer is not active anymore. Like as message to othe code blocks.
    
hWarmuptimer null;

    
    
// There is no way, smoketimer supposed to runnig in this point. But double check.
    
if(hSmoketimer != nullKillTimer(hSmoketimer);

    
// Create repeated smoke timer
    
hSmoketimer CreateTimer(0.5smoketimer_cb_TIMER_REPEAT);

    return 
Plugin_Continue;
}

public 
Action smoketimer_cb(Handle timer)
{
    
// Check Handle is it null, stop timer if it is.
    
if(hSmoketimer == null) return Plugin_Stop


    
// Do your smoke exec() here!
    // for example, print chat message smoke alive players
    
for(int i 1<= MaxClientsi++)
    {
        if(!
IsClientInGame(i) || !IsPlayerAlive(i)) continue;

        
PrintToChat(i"smoke!");
    }




    
// Let timer continue
    
return Plugin_Continue;

*edit
I skipped this part. I made example to loop all alive players and print chat message "smoke" :/
Thank you for this! I will try it all tonight after work
__________________
condolent is offline