Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different values of Stopwatch.Frequency on the same PC

I'm working on online game and have client and server. For client I use Unity3D and C#, server is written in C#. For synchronization I use timers, and as we know timers depends from ticks. Ticks counter in C# is class Stopwatch, and count of ticks in 1 second equals Stopwatch.Frequency, but on server and client, values of Stopwatch.Frequency are different, and it kills my synchronization because the timer on server too slow unlike a timer on the client. Stopwatch.Frequency on the server equals 3.124.980, and Stopwatch.Frequency on the client equals 10.000.000. So why??? How i can change the value of Stopwatch.Frequency for timers synchronization? Thanks and sorry for my bad English

like image 309
user2686299 Avatar asked Nov 01 '22 03:11

user2686299


1 Answers

Stopwatch can be unreliable on a PC with multiple processors, or processors that do not have a constant clock speed (processors that can reduce clock to conserve energy), so you simply can't use it in a game (because you want it to work in every computer).

Many games uses a global watch, and I've seen that even the simplest global watch algorithm can be good enough to synchronize clients with a server for a game. Take a look at the Cristian's algorithm.

Having a global watch, you can simply use DateTime.UtcNow to measure time.

like image 162
Roberto Avatar answered Nov 13 '22 16:11

Roberto