Raised This Month: $ Target: $400
 0% 

[STOCK] Clear a timer


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
SkydiveX
Member
Join Date: Jul 2012
Location: England
Old 10-30-2012 , 10:37   Re: [STOCK] Clear a timer
Reply With Quote #1

Never used it + Never understood what It does. :/.
I guess its because i'm a bad coder .
__________________

_____________________________________________ _______________
Pastafarian and proud!
SkydiveX is offline
Malachi
Senior Member
Join Date: Jun 2010
Location: USA
Old 01-18-2013 , 13:28   Re: [STOCK] Clear a timer
Reply With Quote #2

I'm trying to test if a one-time timer has expired yet, but I'm not having any luck.

The test:
if (g_hTimer != INVALID_HANDLE)

isnt doing what I thought it should.


Here is what I've tried:
Code:
#include <sourcemod>
#pragma semicolon 1

// Globals
new Handle:g_hTimer = INVALID_HANDLE; 

...

public Action:Hook_RoundStart(Handle:event, const String:name[], bool:dontBroadcast)
{
    if (g_hTimer != INVALID_HANDLE)
    {
        KillTimer(g_hTimer);
        g_hTimer = INVALID_HANDLE;
    }
    g_hTimer = CreateTimer(30.0, ShowStatus, 0, TIMER_FLAG_NO_MAPCHANGE | TIMER_HNDL_CLOSE);
    return Plugin_Continue;
}
(I reconstructed my code as best I could)

So KillTimer (I've substituted CloseHandle - the docs suggest they're similar) causes a runtime error. Using TIMER_HNDL_CLOSE doesn't change this. The docs warn that a runtime error will happen if you pass KillTimer an invalid handle. I'm thinking this is because the test for INVALID_HANDLE isn't working.

FWIW here is the actual SM errors log:
Quote:
L 01/16/2013 - 18:46:32: [SM] Plugin "alltalkstatus.0.5.smx" encountered error 23: Native detected error
L 01/16/2013 - 18:46:32: [SM] Invalid data handle 0 (error 4) passed during timer end with TIMER_DATA_HNDL_CLOSE
L 01/16/2013 - 18:46:32: [SM] Unable to call function "ShowStatus" due to above error(s).
L 01/16/2013 - 18:46:40: [SM] Native "KillTimer" reported: Invalid timer handle 2730236 (error 1)
L 01/16/2013 - 18:46:40: [SM] Displaying call stack trace for plugin "alltalkstatus.0.5.smx":
L 01/16/2013 - 18:46:40: [SM] [0] Line 118, alltalkstatus.0.5.sp::Hook_RoundStart()
L 01/16/2013 - 18:53:48: [SM] Native "KillTimer" reported: Invalid timer handle 2730236 (error 1)
L 01/16/2013 - 18:53:48: [SM] Displaying call stack trace for plugin "alltalkstatus.0.5.smx":
L 01/16/2013 - 18:53:48: [SM] [0] Line 118, alltalkstatus.0.5.sp::Hook_RoundStart()
I saw your code and couldn't help but wonder where I might be going wrong. Maybe I'm just missing something simple here.
Malachi is offline
TnTSCS
AlliedModders Donor
Join Date: Oct 2010
Location: Undisclosed...
Old 01-18-2013 , 14:12   Re: [STOCK] Clear a timer
Reply With Quote #3

what's in your timer's function (ShowStatus)?
__________________
View my Plugins | Donate
TnTSCS is offline
SystematicMania
Senior Member
Join Date: Aug 2012
Old 01-18-2013 , 14:38   Re: [STOCK] Clear a timer
Reply With Quote #4

You're doing several dangerous things.

Firstly, you shouldn't be passing an invalid handle (data consisting of 0) while using TIMER_DATA_HNDL_CLOSE.

Secondly, you shouldn't be keeping track of the timer's status while also using TIMER_FLAG_NO_MAPCHANGE. That's dangerous because the timer will terminate without setting g_hTimer to INVALID_HANDLE.

Here's what you should be doing:

Code:
#include <sourcemod> #pragma semicolon 1 // Globals new Handle:g_hTimer = INVALID_HANDLE; ... public OnMapEnd() {     if (g_hTimer != INVALID_HANDLE)     {         KillTimer(g_hTimer);         g_hTimer = INVALID_HANDLE;     } } public Action:Hook_RoundStart(Handle:event, const String:name[], bool:dontBroadcast) {     if (g_hTimer != INVALID_HANDLE)     {         KillTimer(g_hTimer);         g_hTimer = INVALID_HANDLE;     }     g_hTimer = CreateTimer(30.0, ShowStatus);     return Plugin_Continue; } public Action:ShowStatus(Handle:timer) {     g_hTimer = INVALID_HANDLE;     // rest of your code     return Plugin_Stop; }
__________________

Last edited by SystematicMania; 01-18-2013 at 15:15.
SystematicMania is offline
Malachi
Senior Member
Join Date: Jun 2010
Location: USA
Old 01-18-2013 , 15:30   Re: [STOCK] Clear a timer
Reply With Quote #5

Quote:
Originally Posted by TnTSCS View Post
what's in your timer's function (ShowStatus)?
It just prints stuff:
Code:
public Action:ShowStatus(Handle:Timer)
{
    new String:message[64] = "";

...

    Format(message, sizeof(message), "  \x04TeamTalk is \x01ON");
    
...

    PrintToChatAll("\x04AllTalk is \x01ON%s", message);

...
    return Plugin_Continue;
}
I just want to display stuff after a time delay. But I also have my eye on learning the in's and out's so I can apply this stuff to future projects.


Quote:
Originally Posted by SystematicMania View Post
Firstly, you shouldn't be passing an invalid handle (data consisting of 0) while using TIMER_DATA_HNDL_CLOSE.
I'm using TIMER_HNDL_CLOSE? Where are you getting TIMER_DATA_HNDL_CLOSE from?

Yeah, the "0" was a copy/paste from another person's plugin. Using INVALID_HANDLE seems more appropriate.



Quote:
Originally Posted by SystematicMania View Post
Secondly, you shouldn't be keeping track of the timer's status while also using TIMER_FLAG_NO_MAPCHANGE. That's dangerous because the timer will terminate without setting g_hTimer to INVALID_HANDLE.
Even using TIMER_HNDL_CLOSE it won't clean up (set g_hTimer to INVALID_HANDLE)? Oh, duh.

Ok, I see how you're handling that with OnMapEnd. That sounds like where the runtime error is coming from.


Thanks, those were some fast replies!

Last edited by Malachi; 01-18-2013 at 15:35.
Malachi is offline
SystematicMania
Senior Member
Join Date: Aug 2012
Old 01-18-2013 , 15:51   Re: [STOCK] Clear a timer
Reply With Quote #6

Quote:
Originally Posted by Malachi View Post
I'm using TIMER_HNDL_CLOSE? Where are you getting TIMER_DATA_HNDL_CLOSE from?
They are the exact same thing: http://mxr.alliedmods.net/sourcemod-.../timers.inc#42

TIMER_HNDL_CLOSE is depreciated. TIMER_DATA_HNDL_CLOSE is its new name.

Quote:
Originally Posted by Malachi View Post
Ok, I see how you're handling that with OnMapEnd. That sounds like where the runtime error is coming from.
Did you also notice what was done in the timer's callback (in my example)?
__________________

Last edited by SystematicMania; 01-18-2013 at 15:59.
SystematicMania is offline
Malachi
Senior Member
Join Date: Jun 2010
Location: USA
Old 01-19-2013 , 01:15   Re: [STOCK] Clear a timer
Reply With Quote #7

Quote:
Originally Posted by SystematicMania View Post
They are the exact same thing
Gotcha.

Quote:
Originally Posted by SystematicMania View Post
Did you also notice what was done in the timer's callback (in my example)?
Yeah, I noticed that. Thats when it became clearer what I was missing.

Thanks for your help. The plugin is now working well and I learned a few things in the process.
Malachi 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 18:28.


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