Fixed Server Game Loop

Started by
-1 comments, last by Xanather 11 years, 10 months ago

[source lang="csharp"]using System;
using System.Timers;

namespace ServerGameLoopTest
{
class Program
{
static void Main(string[] args)
{
Server server = new Server();
server.Start();
}
}
class Server
{
Timer timer = new Timer(16);
DateTime prevtime;
public void Start()
{
prevtime = DateTime.Now;
timer.Elapsed += new ElapsedEventHandler(GameLoop);
timer.Enabled = true;
Console.ReadLine();
}
private void GameLoop(object source, ElapsedEventArgs e)
{
RunGameLogic(e.SignalTime.Subtract(prevtime));
prevtime = e.SignalTime;
}
private void RunGameLogic(TimeSpan timesincelastframe)
{
Console.WriteLine(timesincelastframe.Milliseconds); //testing purposes, should always show 16
//game logic here
}
}
}
[/source]
So i am tying to create a fixed time step loop for a server using the Timer object provided by the .net Framework 4.0.
I am going to assume doing this is the wrong way of doing it. When running this "as is" (the code above)
the printed timespan is 16 milliseconds, then 30 milliseconds, then 32 (i think), then 16 again. and it cycles
through this. The point is that this is not a fixed time step. Why is this, should I not use the timer object
to do this? If so what else? I am trying to get a fixed time step a the client (XNA) and server (no XNA at all so
i need to create a game loop). both programs will have a loop of every 16 milliseconds (62.5 FPS)

All replies are appriciated.

Thanks, Xanather.

This topic is closed to new replies.

Advertisement