Measuring seconds in C++?
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 :)
Use time():
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
#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
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.
#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.
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.
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:
Alternatively and more accurately:
Hope that helps,
Dave
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
Popular Topics
Advertisement