Stopwatch ?

Posted By: theDust

Stopwatch ? - 03/22/09 14:35

Hi, the timer()-function is working in microseconds. After 2 secs its not working anymore, the timer stays at "-2097152". I don't need such a high-precision counter, but i need a higher range of a few mins up too hours. How to ?
Posted By: EvilSOB

Re: Stopwatch ? - 03/22/09 15:32

Im usless with c-script, so I hope you after a lite-c result.

USAGE: (TESTED)
ALWAYS needs to called with VAR variables in all 3 parameters.
First call starts the timer, second call stops it and injects the elapsed time into the 3 var parameters.
IF there are values in the parameters VARs as first-call, these are used as a starting-point by the timer.
So you can do
Code:
   var HH=0, MM=0, SS=0;
   StopWatch(HH, MM, SS);  //timer started at ZEROs
   ...
   StopWatch(HH, MM, SS);  //Stop timer and HH, MM, SS = elapsed time
   StopWatch(HH, MM, SS);  //Restart timer at HH, MM, SS elapsed time (NO time lost)
   ...
   StopWatch(HH, MM, SS);  //Stop timer and HH, MM, SS = elapsed time
STOPWATCH Function itself
Code:
void StopWatch(var* Hours, var* Minutes, var* Seconds)
{
    static var hours, minutes, seconds, running=0;
    if(running==0)
    {    //START StopWatch
        running = 1;
        hours=*Hours;    minutes=*Minutes;    seconds=*Seconds;
        while(running)
        {
            wait(1);
            seconds += (timer()/1000000);
            if(seconds>=60)
            {
                minutes += integer(seconds/60);
                seconds  = cycle(seconds,0,60);
                if(minutes>=60)
                {
                    hours  += integer(minutes/60);
                    minutes = cycle(minutes,0,60);
                }
            }
        }
        return;
    }
    //STOP StopWatch
    *Hours = hours;    *Minutes = minutes;    *Seconds = seconds;
    running=0;
    return;
}
If this is needed in C-script, I'll need you to let me know, and I'll need more time...
Posted By: theDust

Re: Stopwatch ? - 03/22/09 16:28

Wonderfull, big thx. It's also a nice code to learn a bit more about lite-c smile
Posted By: EvilSOB

Re: Stopwatch ? - 03/22/09 16:35

No problems, BUT, take a look at the time-calculations flowing
from seconds into hours. It's right, but Im sure there is a better way.

It just that its 4am here and I am desperately tired, but just cant sleep.
So I code......
Posted By: Xarthor

Re: Stopwatch ? - 03/22/09 20:14

You could also use total_secs: http://www.conitec.net/beta/atotal_secs.htm
Just save it to a var when the stopwatch is started and when you stop the stopwatch you subtract the stored value from the current one and you get the passed seconds.
Which you can then translate into hours/minutes/seconds.

Other than that you can also use "wait(-1)" to wait one second.
Posted By: Quad

Re: Stopwatch ? - 03/22/09 21:41

to be more preciese you can use wait(-0.1); too.
Posted By: JMichalc

Re: Stopwatch ? - 03/24/09 02:08

Originally Posted By: EvilSOB
No problems, BUT, take a look at the time-calculations flowing
from seconds into hours. It's right, but Im sure there is a better way.


There is, and it's super simple:

Code:
void StopWatch(var* Hours, var* Minutes, var* Seconds)
{
    static var hours, minutes, seconds, running=0;
    if(running==0)
    {    //START StopWatch
        running = 1;
        hours=*Hours;    minutes=*Minutes;    seconds=*Seconds;
        while(running)
        {
            wait(1);
            seconds += (timer()/1000000);

            minutes += integer(seconds / 60);
            seconds = seconds % 60;

            hours += integer(minutes / 60);
            minutes = minutes % 60;
        }
        return;
    }
    //STOP StopWatch
    *Hours = hours;    *Minutes = minutes;    *Seconds = seconds;
    running=0;
    return;
}


No more if statements needed smile


Just so beginners understand, the mathematical operator "%" is called modulus. It calculates the remainder of a division operation.

Lets say we have a pile of 20 cookies. We want to serve the cookies at a party on plates, where each plate can hold 3 cookies. 20 / 3 equals 6.666666_, so we know there will be cookies left over, but how many? 20 % 3 equals 2, so there will be 6 plates with 3 cookies each and 1 plate with just 2 cookies. You'd better eat that serving before-hand... you don't want an upset guest!
Posted By: EvilSOB

Re: Stopwatch ? - 03/24/09 02:12

Thats what I thought, I always forget the % operator...
Posted By: JMichalc

Re: Stopwatch ? - 03/24/09 02:19

yeah, its a good one to keep in mind wink

another option was using Gamestudio's built in "cycle" function:
Code:
cycle(var x,var a,var b)

which wraps x to be between a and b, so in this case:
Code:
minutes += integer(seconds / 60);
seconds = wrap(seconds, 0, 60);

Posted By: elevationmind

Re: Stopwatch ? - 11/16/12 11:03

Hey I was just trying to implement this and wanted to ask you:

Is it possible to make more than one Stopwatches?

And would it be possible to have to funcions: one for start, one for stop?
Posted By: rayp

Re: Stopwatch ? - 11/16/12 11:12

Nearly everything is possible why not ?
Rename it to StopWatch2 and u have another one grin

Make the var "running" global. Then u can stop the running StopWatch function by setting running to zero.
Posted By: elevationmind

Re: Stopwatch ? - 11/16/12 12:06

var running;
void StopWatch(var* Seconds)
{
static var seconds;
seconds= *Seconds;
while(running)
{
wait(1);
seconds += (timer()/1000000);
}
return;
*Seconds = seconds;
return;

}

This is what I am currently using (as I only need seconds), but it is not working.

Starting it with running=1; StopWatch(SS);
doesn't work...
Posted By: Uhrwerk

Re: Stopwatch ? - 11/16/12 12:16

Here are the worst things:
1. *Seconds = seconds; is dead code.
2. There is no advante in making seconds static.
3. You cannot use timer() over two frames. Read the documentation. Besides timer() is absolutely not necesary here. time_step is totally sufficient.
Posted By: elevationmind

Re: Stopwatch ? - 11/16/12 12:40

Originally Posted By: Uhrwerk
Here are the worst things:
1. *Seconds = seconds; is dead code.
2. There is no advante in making seconds static.
3. You cannot use timer() over two frames. Read the documentation. Besides timer() is absolutely not necesary here. time_step is totally sufficient.


Could you please explain to me how I could use time_step to make a stopwatch that just counts the seconds?
Posted By: elevationmind

Re: Stopwatch ? - 11/16/12 12:43

Nevermind - I've got it. Thx anyway!
© 2024 lite-C Forums