# Measuring seconds in C++?

## 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 on other sites
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 on other sites
It tells me time function does not take 0 arguments.

Try clock().

##### Share on other sites
Can someone tell me what the difference between time() and clock() is?

##### 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 on other sites
Quote:
 Original post by Emper0rIt tells me time function does not take 0 arguments.

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

##### 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 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 on other sites
Using [boost]:

#include <boost/timer.hpp>#include <iostream>int main () {    boost::timer t;    while ( t.elapsed() < 5.0 ) { /* do nothing */ }    std::cout << "5 seconds have now passed." << std::endl;}

##### Share on other sites
When I use that code the function executes right when I start up the program. I should have been more specific, I'm looking to use the function only when exactly 10 seconds have passed by, then stop using it. The reason I'm doing this is because I'm wanting to spawn the boss character for my game.

if (( GetTickCount() - start ) == 10000)

Nothing happened, is this because the program doesn't have enough time to do what's in the function? Thanks again for the help Dave.

##### Share on other sites
Quote:
 Original post by Emper0rWhen I use that code the function executes right when I start up the program. I should have been more specific, I'm looking to use the function only when exactly 10 seconds have passed by, then stop using it. The reason I'm doing this is because I'm wanting to spawn the boss character for my game.When I made it this instead:if (( GetTickCount() - start ) == 10000)Nothing happened, is this because the program doesn't have enough time to do what's in the function? Thanks again for the help Dave.

If you test for the exact time, you may miss the exact moment.

Use something like this:

bool triggered = false;

if( !triggered && GetTickCount() - start >= 10000 ) {
triggered = true;

// ..do something..

}

##### Share on other sites
Well, if you want to spawn a boss 10 seconds into the execution of your program you should have some sort of timer class that is polled every frame of the program.

Something like this might suffice:
class  BossTimer{   public:      BossTimer()      {          m_startTime = GetTickCount();      }      ~BossTimer(){};      void AssessAndPollBoss()      {         if (GetTickCount()-m_startTime) >= 10000)         {            SpawnBoss();         }      }   private:      void SpawnBoss()      {         ... Somehow spawn some bosses ...      }};

This should be called every frame in your application loop.

Possibly like this (with some instance of BossTimer declared globally or something):
            while( msg.message!=WM_QUIT )            {                if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )                {                    TranslateMessage( &msg );                    DispatchMessage( &msg );                }                else                {                    myBossTimer.AssessAndPollBoss();                    Render();                }            }

Hope that helps,

Dave

##### Share on other sites
Quote:
 Original post by DaveA 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.

That's not true: time() returns the number of seconds elapsed since (Unix) epoch (01 Jan. 1970), not a number of milliseconds.

For the record,
Quote:
 HistoryThe earliest versions of Unix time had a 32-bit integer incrementing at a rate of 60 Hz, which was the rate of the system clock on the hardware of the early Unix systems. The value 60 Hz still appears in some software interfaces as a result. The epoch also differed from the current value. The first edition Unix Programmer's Manual dated November 3, 1971 defines the Unix time as "the time since 00:00:00, Jan. 1, 1971, measured in sixtieths of a second". It also comments that "the chronologically-minded user will note that 232 sixtieths of a second is only about 2.5 years". Because of this limited range, the epoch was redefined more than once, before the rate was changed to 1 Hz and the epoch was set to its present value. This yielded a range in excess of 130 years, though with more than half the range in the past (see discussion of signedness above).

Regards,

##### Share on other sites
Quote:
Original post by Emmanuel Deloget
Quote:
 Original post by DaveA 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.

That's not true: time() returns the number of seconds elapsed since (Unix) epoch (01 Jan. 1970), not a number of milliseconds.

For the record,
Quote:
 HistoryThe earliest versions of Unix time had a 32-bit integer incrementing at a rate of 60 Hz, which was the rate of the system clock on the hardware of the early Unix systems. The value 60 Hz still appears in some software interfaces as a result. The epoch also differed from the current value. The first edition Unix Programmer's Manual dated November 3, 1971 defines the Unix time as "the time since 00:00:00, Jan. 1, 1971, measured in sixtieths of a second". It also comments that "the chronologically-minded user will note that 232 sixtieths of a second is only about 2.5 years". Because of this limited range, the epoch was redefined more than once, before the rate was changed to 1 Hz and the epoch was set to its present value. This yielded a range in excess of 130 years, though with more than half the range in the past (see discussion of signedness above).

Regards,

Yeah i realised this when the OP next replied, i was typing with GetTickCount in mind. [smile]

Dave

##### Share on other sites
How are you doing your graphics? If you're using Allegro it has timer functions. If not, well, I can't really help. I'm struggling with a similar problem, trying to delay my enemy update routine so it's slow enough for a human to keep up.