Jump to content
  • Advertisement
Sign in to follow this  
Lorian14

Keeping track of time

This topic is 4043 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 am writing a text game and I need some way of representing the game time. I essentailly need a timer, but one that can be paused and sped up/slowed down. I have looked around but haven't been able to find anything useful. Any help would be appreciated, cheers.

Share this post


Link to post
Share on other sites
Advertisement
What language are you using? In C++ you can use the sleep(int milliseconds) function, I guess.

In Java, you can create a new thread and use Thread.sleep(int milliseconds) and/or put it in a for loop. Something like:

boolean tick = false;
for(int i=0;i<10;i++)
{
tick = false;
if(i==9)
{
tick = true;
i = 0;
}
Thread.sleep(100);
}

This should set tick = true every 1000 milliseconds. Another object can check this tick to see if a second has passed. Or better, you can cause a function to be called when (i==9), that is, every second.

Of course, there might be better ways to do it and the experienced coders on this forum will correct me if I'm wrong.

Share this post


Link to post
Share on other sites
My bad, I'm using C++.

I'm not sure how using sleep() will help me though as that would freeze up the whole program until it's finished. :\

Share this post


Link to post
Share on other sites
I am not sure what API's you are using, but there should be some way of hooking onto the timer interrupt... thinggy. You can define different routines to increment a tick-variable at fixed rates. I use a set of 'tick-vars' that are incremented on a ms/sec/and min basis... It's just a matter of which API you are using...

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!