AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Stop Timer before Callback (https://forums.alliedmods.net/showthread.php?t=340486)

HSFighter 11-21-2022 15:20

Stop Timer before Callback
 
Hello,
i try to stop a timer before the callback begins.
PHP Code:

Handle g_hCTimer INVALID_HANDLE;

public 
void OnPluginStart() 
{
   
g_hCTimer CreateTimer(60.0Timer_Cooldown);
}
    
public 
Action Timer_Cooldown(Handle timer
{
   
// Do Stuff...


i try this...
PHP Code:

if(g_hCTimer != INVALID_HANDLEKillTimer(g_hCTimer); 

or this...
PHP Code:

if(g_hCTimer != INVALID_HANDLEdelete g_hCTimer

But the timer runs the callback even the HANDLE is valid or invalid.

Any Ideas?

Thanks in advance

Drixevel 11-21-2022 18:16

Re: Stop Timer before Callback
 
Code:

        if (g_hCTimer != null) {
                KillTimer(g_hCTimer);
                g_hCTimer = null;
        }

Usually doing that makes the timer stop entirely and it sets the pointer back to null. If it's not stopping then something else is up with your code.

alasfourom 11-21-2022 18:19

Re: Stop Timer before Callback
 
I dont know what you want to do.

Based on what you provided, you can do something like this:

Use: !start/!stop > to activate/delete timer respectively.

PHP Code:

#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>

Handle g_hTimer;
bool g_bTimer;
int g_iTimer;

public 
void OnPluginStart()
{
    
RegConsoleCmd("sm_start"Command_StartTimer"Start Timer"); // !start > will start timer
    
RegConsoleCmd("sm_stop"Command_StopTimer"Stop Timer"); // !stop > will delete timer
}

public 
void OnMapStart()
{
    
g_bTimer false;
    
g_iTimer 0;
}

Action Command_StartTimer(int clientint args)
{
    if(!
g_bTimer)
    {
        
PrintToChatAll("Timer: >>>>> Started <<<<<");
        
g_hTimer CreateTimer(1.0Timer_Cooldown_TIMER_FLAG_NO_MAPCHANGE|TIMER_REPEAT);
        
g_bTimer true;
    }
    else 
PrintToChat(client"Error: Timer already activated!!");
    return 
Plugin_Handled;
}

Action Timer_Cooldown(Handle timer)
{
    if (
g_iTimer == 60 1//60 = seconds
    
{
        
PrintHintTextToAll("Time: Ready!"g_iTimer);
        
        
// When cooldown is ready, do stuff...
        
        
g_bTimer false;
        
g_iTimer 0;
        return 
Plugin_Stop;
    }
    ++ 
g_iTimer;
    
PrintHintTextToAll("Time: %d / 60"g_iTimer);
    return 
Plugin_Continue;
}

Action Command_StopTimer(int clientint args)
{
    if(
g_bTimer)
    {
        
PrintHintTextToAll("Timer Stopped");
        
PrintToChatAll("Timer: >>>>> Stopped <<<<<");
        
delete g_hTimer;
        
g_bTimer false;
        
g_iTimer 0;
    }
    else 
PrintToChat(client"Error: Timer is not activated");
    return 
Plugin_Handled;



Bacardi 11-21-2022 18:24

Re: Stop Timer before Callback
 
Should work, unless... you have something wierd.
PHP Code:


Handle mytimer
;

public 
void OnPluginStart()
{
    
PrintToChatAll("Plugin loaded and timer creted");
    
mytimer CreateTimer(60.0timer_callback);

    
RegConsoleCmd("sm_test"test);
}

public 
Action test(int clientint args)
{
    if(
mytimer != null)
    {
        
delete mytimer;
        
PrintToChat(client"You stop timer.");
    }

    
PrintToChat(client"Piip!");

    return 
Plugin_Handled;
}


public 
Action timer_callback(Handle timer)
{
    
// For example, when multiple timers have accidentally created to same handle, ignore previous timers.
    // Or if Handle is null.
    
if(timer != mytimer)
    {
        return 
Plugin_Stop// If repeating timer flag in use
    
}

    
// When you know timer callback will finish, not repeating.
    // First step is clear handle.
    
mytimer null;



    
PrintToChatAll("My timer got executed");

    
//return Plugin_Continue;
    
return Plugin_Stop// If repeating timer flag in use



alasfourom 11-21-2022 18:38

Re: Stop Timer before Callback
 
Quote:

Originally Posted by Bacardi (Post 2793369)
Should work, unless... you have something wierd.
PHP Code:


Handle mytimer
;

public 
void OnPluginStart()
{
    
PrintToChatAll("Plugin loaded and timer creted");
    
mytimer CreateTimer(60.0timer_callback);

    
RegConsoleCmd("sm_test"test);
}

public 
Action test(int clientint args)
{
    if(
mytimer != null)
    {
        
delete mytimer;
        
PrintToChat(client"You stop timer.");
    }

    
PrintToChat(client"Piip!");

    return 
Plugin_Handled;
}


public 
Action timer_callback(Handle timer)
{
    
// For example, when multiple timers have accidentally created to same handle, ignore previous timers.
    // Or if Handle is null.
    
if(timer != mytimer)
    {
        return 
Plugin_Continue;
    }

    
// When you know timer callback will finish, not repeating.
    // First step is clear handle.
    
mytimer null;



    
PrintToChatAll("My timer got executed");

    return 
Plugin_Continue;



I liked that

HSFighter 11-28-2022 16:01

Re: Stop Timer before Callback
 
Thank you very much :up:

Grey83 11-29-2022 07:54

Re: Stop Timer before Callback
 
Bacardi, but to stop the timer, it must return Plugin_Stop, not Plugin_Continue.
Quote:

return
Plugin_Stop to stop a repeating timer, any other value for default behavior.

Bacardi 11-29-2022 14:59

Re: Stop Timer before Callback
 
Quote:

Originally Posted by Grey83 (Post 2793986)
Bacardi, but to stop the timer, it must return Plugin_Stop, not Plugin_Continue.

Absolutely true. Repeating timer. :3


All times are GMT -4. The time now is 15:35.

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