Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Simple game timer - which method to use?


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   -  Reputation: 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

Sponsor:

#2 VReality   Members   -  Reputation: 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   -  Reputation: 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   -  Reputation: 1635

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.

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.



PARTNERS