Sign in to follow this  
Khaiy

C# events at timed intervals

Recommended Posts

I'm fiddling with a small console program, and I'm trying to set the movement of characters through a 2D array to be assessed each second so that the simulation can be real-time (rather than turn based) and the characters' speed attributes will matter.

Since it's a console simulation I don't need to tie this to framerate (though maybe I do need to do something similar). I've been trying to use a system timer to fire an event that will prompt my navigation function each second, but I'm having issues getting an elapsed time event to do anything more than write to the console, and all the examples of timers I've found do the same.

I'm starting to consider writing a new event handler with a new delegate that can work with my navigation function, but that seems like it might be more work than is necessary.

Is this the right approach conceptually? Or is there a better way to do something like this that I've missed?

Share this post


Link to post
Share on other sites
It depends on the design of your game.

For many games, the better design is to have some globally-accessible timing information.

Common timers include:
* simulator step. A single unsigned integer for how much game time has passed. One tick may equal 1/4 second, or 1/10 second, or some other value. This stops when the game is paused.
* frame time. Often a high-performance counter or floating point number that shows how much time has passed since the last redraw
* wall time since program was started.


The game is then run "as fast as possible", and does not wait around for system timers. Rendering waits only until the display buffers have been flipped, the simulation ticks away on its own, sometimes running multiple ticks in a single visible frame.

It is generally unwise to base much of your game on the general purpose timers the system provides. While those timers are useful for many tasks, and they can be used for form games and turn games and such, they are not really designed for interactive animated simulators. Consider that the system timer has 15.6ms resolution on Windows 7, and different resolutions on older system. Then consider most of today's panel displays have refresh rates of 75 frames per second, or 13.3ms. The fact that a single timer tick is longer than an entire display frame would, by itself, disqualify it for use in many games.

Share this post


Link to post
Share on other sites
Thanks for the reply!

Just to make sure I understand the simulator step (the other two seem pretty straightforward), I have an int which I increment based on game events (a completed loop or whatever), and any relation that this has to the passage of real time is defined elsewhere.

So I would have something like
[code]
public int timer = 0;
bool gameRunning = true;

while(gameRunning)
{
// Game logic, input, etc.
timer++;
}
[/code]

and the timer variable would be updated as fast as the program could execute a full game loop.

And if I wanted it to be throttled for whatever reason, I guess I could do something like making sure that at least X ticks had passed before running the loop again (or something). That might be important for something like game logic, which I don't want to outrun the player, but not very for flipping buffers which can just draw the same frames over and over again if need be.

Do I at least have the general idea of the concept? I've been pretty underslept lately, so I could be way off...

Share this post


Link to post
Share on other sites
Here's a C# simple static timer class I used on a game server. Uses a C# StopWatch to calculate the time elapsed since the previous frame in milliseconds.

[code]

//----------------------------------------------------------------
// TimeMgr.cs - time elapsed since previous update
//
// to use:
// - call TimeMgr.Initialize() on game start
// - at the start of every game update call TimeMgr.Update()
// - public field TimeMgr.timeElapsed is the elapsed
// milliseconds since the previous update
//----------------------------------------------------------------
using System.Diagnostics;

public static class TimeMgr
{

// stop watch and private vars
private static Stopwatch stopWatch;
private static float currentTime = 0f;
private static float oldTime = 0f;


// time elapsed since the previous tick
public static float timeElapsed = 0f;


//----------------------------------------------------------------
// Initialize() - init stopwatch
//----------------------------------------------------------------
public static void Initialize()
{
stopWatch = new Stopwatch();
stopWatch.Reset();
stopWatch.Start();
}


//----------------------------------------------------------------
// Update() - save previous time elapsed
// - update current time elapsed
//----------------------------------------------------------------
public static void Update()
{
oldTime = currentTime;
currentTime = (float)stopWatch.ElapsedMilliseconds;
timeElapsed = currentTime - oldTime;
}


}
[/code]

Share this post


Link to post
Share on other sites
[quote name='Khaiy' timestamp='1312943830' post='4846999']
[code]
while(gameRunning)
{
// Game logic, input, etc.
timer++;
[/code]

Do I at least have the general idea of the concept? I've been pretty underslept lately, so I could be way off...
[/quote]

Basically, but I'd call it something like "simulatorTick".

The simulator is independent of everything else. It is independent of wall clock, allowing you to pause the simulator or fast-forward to 2x or 4x or other speeds. It is independent of rendering, or else you couldn't draw anything while paused or it would stutter if the CPU is busy with spyware or other tasks, or for some other reason frame rate changes. The simulator timing is independent of networking, independent of input hardware like keyboard and mouse, independent of window events, and so on. The simulator should be isolated from reality as much as possible.

Many systems, like physics, animations, and IA, should operate in terms of that simulator tick, not the real clock time.



All of that said, there is still a place for the C# timer objects if you choose to use them. They just don't belong as part of the simulator for most games.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this