SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts > Includes

Reply
 
Thread Tools Display Modes
Old 12/10/2011, 04:18 PM   #1
Slice
High-roller
 
Slice's Avatar
 
Join Date: Mar 2008
Location: Sweden
Posts: 1,859
Reputation: 1236
Default Timer Fix (seamlessly makes SetTimer/SetTimerEx very accurate)

Note: The fixes2 plugin takes care of this much better. Consider using it instead.

Hey,

I've recently been struggling with timers, and the fact that they aren't accurate by a damn sight. Here's an include that will make your timers accurate by ~5ms.

For example, it will turn this output from one 250ms timer and one 1500ms timer:
Code:
[17:54:33] Time since last call: 293ms (43 ms off)
[17:54:34] Time since last call: 292ms (42ms off)
[17:54:34] Time since last call: 292ms (42ms off)
[17:54:34] Time since last call: 292ms (42ms off)
[17:54:35] Time since last call: 292ms (42ms off)
[17:54:35] Time since last call: 1786ms (286ms off)
[17:54:37] Time since last call: 1756ms (256ms off)
[17:54:38] Time since last call: 1756ms (256ms off)
[17:54:40] Time since last call: 1756ms (256ms off)
[17:54:42] Time since last call: 1756ms (256ms off)
As you can see, the timer is roughly 25% off!

into this:
Code:
[17:55:11] Time since last call: 251ms (1ms off)
[17:55:11] Time since last call: 252ms (2ms off)
[17:55:12] Time since last call: 252ms (2ms off)
[17:55:12] Time since last call: 251ms (1ms off)
[17:55:12] Time since last call: 251ms (1ms off)
[17:55:12] Time since last call: 1498ms (2ms off)
[17:55:14] Time since last call: 1499ms (1ms off)
[17:55:15] Time since last call: 1499ms (1ms off)
[17:55:17] Time since last call: 1500ms (0ms off)
[17:55:18] Time since last call: 1499ms (1ms off)
Usage
You just have to add one single line of code after you included a_samp:
pawn Code:
#include <timerfix>

Limitations
You can, by default, have only 128 timers running at the same time. If you feel the need to increase this, define TIMER_FIX_TIMER_SLOTS before you include the script.
Something to keep in mind is this include causes PAWN to work harder; therefore, you shouldn't have loads of timers running at the same time as it will require higher CPU usage.

Compatibility
  • It works for SetTimer and SetTimerEx, though not with strings and arrays.
  • It works very well with y_timers.
Download
Here: timerfix.inc

Last edited by Slice; 21/11/2012 at 06:06 PM.
Slice is offline   Reply With Quote
Old 12/10/2011, 04:20 PM   #2
SuperMarioRol
High-roller
 
SuperMarioRol's Avatar
 
Join Date: Dec 2009
Location: Castellón, Comunidad Valenciana, España
Posts: 1,741
Reputation: 13
Default Respuesta: Timer Fix (seamlessly makes SetTimer very accurate)

WOW, its great!
__________________


¿Necesitas que tu servidor esté en la lista "Hosted"?
Mandame un MP o mail a david.soler.balado@gmail.com
SuperMarioRol is offline   Reply With Quote
Old 12/10/2011, 04:21 PM   #3
IstuntmanI
High-roller
 
IstuntmanI's Avatar
 
Join Date: Mar 2009
Location: Romania
Posts: 1,690
Reputation: 229
Default Re: Timer Fix (seamlessly makes SetTimer very accurate)

That's awesome... I think I will use this. Thanks.

/second
IstuntmanI is offline   Reply With Quote
Old 12/10/2011, 04:37 PM   #4
rbN.
Banned
 
Join Date: Jan 2010
Location: fissa
Posts: 1,638
Reputation: 289
Default Re: Timer Fix (seamlessly makes SetTimer very accurate)

I saw this like a time ago but there wasn't some positive feedback about it D:
rbN. is offline   Reply With Quote
Old 12/10/2011, 04:37 PM   #5
Killa[DGZ]
Huge Clucker
 
Killa[DGZ]'s Avatar
 
Join Date: Feb 2010
Location: Australia
Posts: 252
Reputation: 45
Default Re: Timer Fix (seamlessly makes SetTimer very accurate)

Quote:
Originally Posted by costel_nistor96 View Post
That's awesome... I think I will use this. Thanks.
Agreed, Definitely an improvement Thank you very much!
Killa[DGZ] is offline   Reply With Quote
Old 12/10/2011, 04:44 PM   #6
Stigg
High-roller
 
Stigg's Avatar
 
Join Date: Aug 2010
Location: Other side of the Universe.
Posts: 2,084
Reputation: 111
Default Re: Timer Fix (seamlessly makes SetTimer very accurate)

Great, this will come in very handy, thank's for the share.
__________________

Peace...

Stigg is offline   Reply With Quote
Old 12/10/2011, 04:57 PM   #7
OKStyle
High-roller
 
OKStyle's Avatar
 
Join Date: May 2008
Location: Russia
Posts: 2,276
Reputation: 77
Default Re: Timer Fix (seamlessly makes SetTimer very accurate)

SetTimerEx too?
__________________
O.K.Style™ Laboratory
Kalcor: "... it's makes the game look like it's not GTA. This mod is called San Andreas Multiplayer. It's not a custom game engine which you can script..."
OKStyle is online now   Reply With Quote
Old 12/10/2011, 04:58 PM   #8
Slice
High-roller
 
Slice's Avatar
 
Join Date: Mar 2008
Location: Sweden
Posts: 1,859
Reputation: 1236
Default Re: Timer Fix (seamlessly makes SetTimer very accurate)

Quote:
Originally Posted by OKStyle View Post
SetTimerEx too?
Not yet!

Quote:
This works only for SetTimer at the moment. I'm working on support for SetTimerEx.
__________________
Do you like SA-MP and DayZ? Come play both: ulclan.com:1421

Do you use any scripts I've made?
Star them on GitHub or reply in their topics so I know what I should keep working on!
Slice is offline   Reply With Quote
Old 12/10/2011, 05:27 PM   #9
Y_Less
Beta Tester
 
Y_Less's Avatar
 
Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 18,158
Reputation: 2540
Default Re: Timer Fix (seamlessly makes SetTimer very accurate)

Quote:
Originally Posted by Slice View Post
Compatibility
It works well with YSI, too. In fact, it increases the accuracy of tasks significantly.
That is very good to hear, though for full disclosure I should point out that it will ONLY work with tasks as everything else uses "SetTimerEx" (often renamed to "O@" for technical reasons (length)). I can see the issue with "SetTimerEx" though - if you don't mind could I have a go at integrating this in to y_timers directly? It may make supporting SetTimerEx easier as there is already code for storing parameters (to fix strings and arrays).

I was going to suggest using a tick time of 0 for better results, but shouldn't make any difference given that the SA:MP server has a "Sleep(5)" call in it.
Y_Less is online now   Reply With Quote
Old 12/10/2011, 05:31 PM   #10
RyDeR`
High-roller
 
RyDeR`'s Avatar
 
Join Date: Feb 2009
Location: Belgium
Posts: 3,133
Reputation: 645
Default Re: Timer Fix (seamlessly makes SetTimer very accurate)

Very useful release.

Perhaps this could help you developing "SetTimerEx":
pawn Code:
preventCrash(); public preventCrash()
{
    SetTimerEx("preventCrash", 0, 0, "");
}

stock SetTimerEx_(szFunc[], iInterval, iRepeating, szFormat[], { Float, _ }: ...)
{
    new
        iArgs = numargs() << 2,
        iStart,
        iEnd
    ;
    #emit CONST.PRI szFormat
    #emit STOR.S.PRI iStart
     
    #emit LOAD.S.PRI iArgs
    #emit ADD.C 8
    #emit STOR.S.PRI iEnd
   
    for(new i = iEnd; i >= iStart; i -= 4)
    {
        #emit LCTRL 5
        #emit LOAD.S.ALT i
        #emit ADD
        #emit LOAD.I
        #emit PUSH.PRI
    }
    #emit PUSH.S szFormat
    #emit PUSH.S iRepeating
    #emit PUSH.S iInterval
    #emit PUSH.S szFunc
    #emit PUSH.S iArgs
    #emit SYSREQ.C SetTimerEx
   
    for(iArgs = (iArgs >> 2) + 1; iArgs--; )
    {
        #emit STACK 4
    }
}
__________________

Sup?

Last edited by RyDeR`; 13/10/2011 at 01:55 PM. Reason: Fixed a small bug about clearing the stack.
RyDeR` is offline   Reply With Quote
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



All times are GMT. The time now is 04:05 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.