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

Stop Timer before Callback


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
HSFighter
Veteran Member
Join Date: Aug 2007
Location: Flensburg - Germany
Old 11-21-2022 , 15:20   Stop Timer before Callback
Reply With Quote #1

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
__________________



Sorry for my very bad english
Greetings HSFighter

Last edited by HSFighter; 11-21-2022 at 15:22.
HSFighter is offline
Send a message via ICQ to HSFighter
Drixevel
AlliedModders Donor
Join Date: Sep 2009
Location: Somewhere headbangin'
Old 11-21-2022 , 18:16   Re: Stop Timer before Callback
Reply With Quote #2

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.

Last edited by Drixevel; 11-21-2022 at 18:17.
Drixevel is offline
alasfourom
Senior Member
Join Date: Feb 2022
Location: Saudi Arabia
Old 11-21-2022 , 18:19   Re: Stop Timer before Callback
Reply With Quote #3

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;

__________________

Last edited by alasfourom; 11-21-2022 at 18:34.
alasfourom is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 11-21-2022 , 18:24   Re: Stop Timer before Callback
Reply With Quote #4

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

__________________
Do not Private Message @me

Last edited by Bacardi; 11-29-2022 at 15:27. Reason: change return type. Doesn't really matter when not using repeating timer flag..
Bacardi is offline
alasfourom
Senior Member
Join Date: Feb 2022
Location: Saudi Arabia
Old 11-21-2022 , 18:38   Re: Stop Timer before Callback
Reply With Quote #5

Quote:
Originally Posted by Bacardi View Post
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
__________________
alasfourom is offline
HSFighter
Veteran Member
Join Date: Aug 2007
Location: Flensburg - Germany
Old 11-28-2022 , 16:01   Re: Stop Timer before Callback
Reply With Quote #6

Thank you very much
__________________



Sorry for my very bad english
Greetings HSFighter
HSFighter is offline
Send a message via ICQ to HSFighter
Grey83
Veteran Member
Join Date: Dec 2014
Location: Ukraine
Old 11-29-2022 , 07:54   Re: Stop Timer before Callback
Reply With Quote #7

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.
__________________

Last edited by Grey83; 11-29-2022 at 07:57.
Grey83 is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 11-29-2022 , 14:59   Re: Stop Timer before Callback
Reply With Quote #8

Quote:
Originally Posted by Grey83 View Post
Bacardi, but to stop the timer, it must return Plugin_Stop, not Plugin_Continue.
Absolutely true. Repeating timer.
__________________
Do not Private Message @me
Bacardi 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 10:43.


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