• Advertisement
Sign in to follow this  

Simple game timer - which method to use?

This topic is 1962 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a simple game loop for a pacman clone, and want to add a timer.

After doing some research there are a few options. For example, I have read about a Win32 timer, a WIndows forms timer, and a system threading timer. With the latter the principle appears to be embedding the game loop inside timer events: http://csharptips.wordpress.com/2010/08/24/thread-timer/

This would imply that to count for a number of seconds I could record the number of game loop iterations.

But I wonder, what do people recommend? And what have people used? It's my first look into this subject, so any comments would be useful.

Cheers[img]http://public.gamedev.net//public/style_emoticons/default/cool.png[/img]

Share this post


Link to post
Share on other sites
Advertisement
In the end I used the thread timer method. It's was very easy indeed.

secondsPassed = 0; // Reset the time
timer = new System.Threading.Timer(new System.Threading.TimerCallback(SecondHasPassed), null, 0, 1000);

private void SecondHasPassed(object obj)
{
secondsPassed++;
}

Problem solved [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Share this post


Link to post
Share on other sites
In c#, int is not thread safe.

It is actually a Nullable<> struct consisting of an integer and a boolean.

[url="http://stackoverflow.com/questions/3047280/is-int-thread-safe"]http://stackoverflow...int-thread-safe[/url]

So if you do use an integer in this way, perhaps put a mutex lock around it. However, I probably recommend using the Winforms timer (System.Windows.Forms.Timer) or the timer provided with the c# language (System.Timers.Timer) Edited by Karsten_

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement