Server Snapshot Rate?

Started by
9 comments, last by EJH 14 years, 7 months ago
Use the C# StopWatch class. Here's a code sample of a very simple static timer that gives time elapsed in milleseconds. I used it for a 32 player shooter and it works great.

And yes, on your game server thread use Sleep(1) if you want to be nice to the CPU. But you don't have to.

using System;using System.ComponentModel;using System.Diagnostics;using System.Threading;using System.Windows.Forms;//----------------------------------------------------------------// TimeManager - gives time elapsed in ms since last server tick// How to use: - call TimeManager.Initialize() on startup//             - call TimeManager.Update() in your Update function//             - TimeManager.timeElapsed is time elapsed since last tick //----------------------------------------------------------------public static class TimeManager{    // the time elapsed since the previous server tick    public static float timeElapsed;    // the StopWatch and private variables    private static Stopwatch stopWatch;    private static float currentTime;    private static float oldTime;    //----------------------------------------------------------------    // Initialize() - call once on program start    //----------------------------------------------------------------    public static void Initialize()    {        stopWatch = new Stopwatch();        stopWatch.Reset();        stopWatch.Start();    }    //----------------------------------------------------------------    // Update() - calculates ms elapsed since last server tick    //----------------------------------------------------------------    public static void Update()    {        // save previous time         // update current time        // calculate time elapsed in ms        oldTime = currentTime;        currentTime = (float)stopWatch.ElapsedMilliseconds;        timeElapsed = currentTime - oldTime;    }    //----------------------------------------------------------------    // Reset() - reset stopwatch to 0    //----------------------------------------------------------------    public static void Reset()    {        stopWatch.Reset();        stopWatch.Start();        oldTime = (float)stopWatch.ElapsedMilliseconds;        currentTime = (float)stopWatch.ElapsedMilliseconds;    }}

This topic is closed to new replies.

Advertisement