Avoiding the lag in SetTimer interval
#1

Hi
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 !");
KillTimer(RaceTimerId);
}
}
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;

main()
{
	print("Test");

}

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

public OneSecTimer() {
	new sText[10];
	if(previous != 0){
	format(sText,sizeof(sText),"%d ms",GetTickCount()-previous);
	print(sText);
	previous = GetTickCount();
	}
	else{
	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
Reply
#2

This is interesting..., specially 1160 in 0.3a,1072ms in 0.2x server
Try
pawn Код:
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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)