SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Help Archive

Thread Tools Display Modes
Old 30/11/2009, 11:22 PM   #1
Little Clucker
Join Date: Sep 2008
Posts: 2
Reputation: 0
Default Avoiding the lag in SetTimer interval

I was just making my own racescript, which included 60-seconds countdown before race starts.

Here's the part:

RaceTimerId is the global variable.

if(strcmp(cmd, "/race", true) == 0)
CountdownTime = 61;
RaceTimerId = SetTimer("FinalCountdown", 1000, 1);
  return 1;
public FinalCountdown()
CountdownTime -= 1;
if(CountdownTime == 0)
SendClientMessageToAll(0xFF0000FF,"The race is starting !");
Everything worked fine, except that countdown, instead of taking 60 (well, 61) seconds, took about 70-71 seconds.
So I made a few changes, tested, tested, tested, nothing.
Then I discovered "timertest" gamemode and modified it a little bit.

The changed version:

#include <a_samp>

forward OneSecTimer();
new previous;



public OnGameModeInit()
	SetTimer("OneSecTimer", 1000, 1);
	return 1;

public OneSecTimer() {
	new sText[10];
	if(previous != 0){
	format(sText,sizeof(sText),"%d ms",GetTickCount()-previous);
	previous = GetTickCount();
	previous = GetTickCount();
It prints the time difference between subsequent OneSecTimer execution.
So, for me it returns ~1170 ms (interval set to 1000ms), which leads to conclusion that there is about 170ms lag.
Then it's easy to calculate than if we make 60 seconds countdown in the way I did, it's going to take 60000 ms + 60 * 170 ms = 70,2 seconds.
My question : is there any way to avoid the lag?
I have an idea of taking the value of time we want for interval, divide it by 1.17 and set the result as interval.
1000 ms : 1.17 = 854 ms.
I changed SetTimer("OneSecTimer", 1000, 1); to SetTimer("OneSecTimer", 854, 1); and results are 996ms-1002ms.
If you could compile the code and run the server, we would know if the 170ms lag is constant for different computers

And by the way. The same code executed on samp server v.0.2x returns ~1075ms.

[post edit]

Well, this may be confusing. I wrote that there is about 170ms lag, but it's only for 1000ms interval. 170ms is 0.17 of 1000ms, so the "lag multiplier" is 0.17. If I set interval to 5000ms, I get about 5850ms (5000ms * 1.17 = 5850ms).
Also, I tested it under windows seven (earlier - windows xp) and lag is lower.

Sorry for messing this
adamulator is offline  
Old 30/11/2009, 11:40 PM   #2
yezizhu's Avatar
Join Date: Nov 2007
Location: Shenzhen,China
Posts: 659
Reputation: 2
Default Re: Avoiding the lag in SetTimer interval

This is interesting..., specially 1160 in 0.3a,1072ms in 0.2x server
SetTmer("Countdown",(CdTime = 60)*1000,false);
also, this terrible lag will can be easily ignored when using stable hosted server cuz it has faster execute time.
yezizhu is offline  

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do i use settimer jonas4334 Help Archive 2 09/04/2010 08:50 PM
[SetTimer] i need help KDlivest954 Help Archive 8 01/04/2010 06:28 PM
[Help]Settimer Caio_Cartaxo PortuguÍs/Portuguese 9 28/03/2010 03:37 PM
Settimer witha settimer in it 02manchestera Help Archive 2 07/03/2010 01:01 PM
SetTimer(PLAYERID,"name",interval,0); This possible?? introzen Help Archive 3 11/03/2009 05:11 PM

All times are GMT. The time now is 12:55 AM.

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