Timer?

Any good texts on game-loop-timers? I need to synchronize player movements for my game, because it’s gonna be multiplayer. Any good pointers?

If all you need is high precision timing and you are under 1.4 or ,later, use the nanotimer.

if you need to synchronize time between machines, you’ll probably need to use some form of NTP

nanotimer was introduced in 1.5. In 1.4 you can use GAGETimer or sun.misc.Perf. Some graphics libs like Java3d and lwjgl also provide their own hires timers. They all use the performance counter on windows except lwjgl wich use timeGetTime().

I don’t find the LWJGL timer to be very good. I switched my game to 1.5 recently and replaced the LWJGL timer call with nano time and wow…huge difference in accuracy at least on my windows XP - I was just messing around, not expecting anything much and was very surprised at the difference.

QueryPerformanceCounter is used by most hires timers and it is affected by change in clock speed. That is bad on laptops that change the mhz to save power. timeGetTime() has a maximum resolution of 1 ms. But in order to get that you need to use sleep(1) instead of yield in your game loop. Even then it will only get 10-15ms resolution on some computers, like mine. It just don’t always work. So there is no hires timer that works all the time, and that is a royal pain in the ass.

Perhapse I should have stated, this is for a game I’m making for the 4K competition.

Perfect accuracy isn’t required, small differences are acceptable, but not something that is noticable.

Currently, I’m doing something like this:


long startTime;
while(true) {
	startTime = System.currentTimeMillis();
	
	update();
	draw();

	do {
		Thread.yield(); 
	} while (System.currentTimeMillis()-startTime< 10);
}


But I don’t know how good this will be in the multiplayer game.

This should be BR’d. How are C games handling modern laptops?

currentTimeMillis is pretty much the worst of all options. the nanotimer was invented to deal with its short-comings.

I dont get your multi-player issue. You are goinmg to have to describe how you are doing multiplayer in a lot more detail I think.

poor.

yeah, because you’re not on a HT CPU or speedstep or whatever. Once those kick in, QPC will give you errors. (nanotime).

Take a look at the throtteling code posted in the “is this the best performace I can get from the game loop?” thread.

He can not use the nanotimer as the 4k entries have to be compatible with 1.4.

appel’s attempt at a networked 4K game is brave to say the least, because the code to cover both the server and client can sum up to no more than 4K. The only 4K game that has pulled off networking thus far is IsOlation Net