Jump to content
  • Advertisement
Sign in to follow this  
Emper0r

Measuring seconds in C++?

This topic is 4234 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

Hey guys, I'm trying to make it so certain functions in my game are triggered after a certain amount of time. How would someone do this? I'm trying to make it for example: if(seconds == 60) { level2(); } Thank you to anyone for help :)

Share this post


Link to post
Share on other sites
Advertisement
Use time():



#include <time.h>

int startTime = time();

while( (time() - startTime) < 1000 )
{
// Waste some time or whatever
}


A crude horrible example, but you get the idea. time() reports a system clock in milliseconds. There are higher resolution timers available but this should serve your purpose.

Hope that helps,

Dave

Share this post


Link to post
Share on other sites
Here's what I have right now and nothing's happening:

#include <time.h>

clock_t startTime = clock () * CLK_TCK;

if (startTime == 10)
{
function body
}

Can someone please just give me a straight-forward method that measures 10 seconds passing after program execution and executing a function as a result? Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by Emper0r
It tells me time function does not take 0 arguments.


Did you google for time() to see why it does not take 0 parameters?

Share this post


Link to post
Share on other sites
Ok here's what I have so far:

time_t seconds = time(&seconds);

if (seconds == 10000)
{
function body
}

So this means the function should execute in 10 seconds correct? Please let me know if you see anything wrong here and thanks a lot for the help guys.

Share this post


Link to post
Share on other sites
No.

If you want to wait 10 seconds you do this:

int start = time( NULL );

while ( ( time( NULL ) - start ) < 10 )
{
}

Alternatively and more accurately:

#include <windows.h>
DWORD start = GetTickCount();

while ( ( GetTickCount() - start ) < 10000 )
{
}


Hope that helps,

Dave

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!