Jump to content

View more

Image of the Day

WIP title screen for #DeathOfAPartisan #screenshotsaturday #gamedev https://t.co/qJNhfZCvd4
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Simple game timer - which method to use?

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
3 replies to this topic

#1 CdrTomalak   Members   

272
Like
0Likes
Like

Posted 02 September 2012 - 07:31 AM

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.

CheersPosted Image

#2 VReality   Members   

436
Like
0Likes
Like

Posted 02 September 2012 - 10:49 AM

Edit: Oops. My response was for C++, not C#.

Edited by VReality, 02 September 2012 - 10:51 AM.


#3 CdrTomalak   Members   

272
Like
0Likes
Like

Posted 02 September 2012 - 11:14 AM

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 Posted Image

#4 Karsten_   Members   

2411
Like
0Likes
Like

Posted 04 September 2012 - 08:50 AM

In c#, int is not thread safe.

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

http://stackoverflow...int-thread-safe

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_, 04 September 2012 - 08:56 AM.

http://tinyurl.com/shewonyay - Thanks so much for those who voted on my GF's Competition Cosplay Entry for Cosplayzine. She won! I owe you all beers :)

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.