1

Say I was making a game and let's pretend I was using a Console Application rather than using something like Unity etc etc.

And imagine I wanted to implement a game loop, the same concept would apply to a dedicated network server for the game.

This is usually how I would have done it, but is this a good option?

public void Start()
{
    _isRunning = true;
    
    var sw = new Stopwatch();
    while (_isRunning)
    {
        sw.Start();
        /* Update Game Logic */
        sw.Stop();
        
        var sleepDelta = Constants.Tickrate - (int)sw.ElapsedMilliseconds;
        if (sleepDelta > 0)
            Thread.Sleep(sleepDelta);
        else
            Console.WriteLine(
                $"Server can't keep up!\nElapsed: {(int)sw.ElapsedMilliseconds}\nElapsed: {sleepDelta}");
        sw.Reset();
    }
}

What's the difference between doing that vs something like the Timer and using the OnElapsed event

TylerH
  • 20,799
  • 66
  • 75
  • 101
JohnA
  • 564
  • 1
  • 5
  • 20
  • That's fine, certainly better than that awful Timer class. You may find out that the sleep is not very accurate or too granular, pinvoke timeBeginPeriod() to fix that. – Hans Passant Jan 31 '23 at 14:37
  • 1
    Can I interest you in the [PeriodicTimer](https://learn.microsoft.com/en-us/dotnet/api/system.threading.periodictimer?view=net-7.0) ? – Fildor Jan 31 '23 at 14:43
  • To get a good answer, more requirements for the loop should be specified. E.g., Some game loops spin as fast as they can and simply pass the elapsed time to the physics, input, and rending logic. These parts can then decide if the act or skip. The Sleep is a waste of resources in away. Most game servers would not slaap but wait for messages to appear in a queue. But this all depends on the required timing of the game. – Emond Jan 31 '23 at 15:09
  • Consider asking this on https://gamedev.stackexchange.com instead – TylerH Jan 31 '23 at 15:27

1 Answers1

2

This is usually how I would have done it, but is this a good option?

Something like this might very well be good enough. I would not expect a high accuracy from the Thread.Sleep, so it might depend on how tight timing requirements you have. For a console based game I would suspect this is not super critical. If you do need high accuracy timers you might want to read this question.

What's the difference between doing that vs something like the Timer and using the OnElapsed event

A timer would do more or less the same thing, but the Threading.Timer/Timers.Timer classes allow ticks to overlap each other if you are not careful, so you either need to be careful with how you are using it, or use something like PeriodicTimer.

You also need a bit careful since the console process will exit when the main method returns, at least for normal non-async main methods.

Timers are in some sense more useful for UI programs where you cannot have a 'main loop', and have specialized timers that run on the UI thread.

But if your method works, then leave it be. If you do not have any problems, chances are that changes will introduce new issues without actually improving anything.

JonasH
  • 28,608
  • 2
  • 10
  • 23