Measuring seconds in C++?

Started by
15 comments, last by Possumdude0 17 years, 4 months ago
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 :)
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
It tells me time function does not take 0 arguments.
Try clock().
Can someone tell me what the difference between time() and clock() is?
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.
possibly.
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?
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.
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

This topic is closed to new replies.

Advertisement