1 registered members (AndrewAMD),
1,248
guests, and 6
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: Stopwatch ?
[Re: theDust]
#257310
03/22/09 15:32
03/22/09 15:32
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
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 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 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...
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Stopwatch ?
[Re: theDust]
#257319
03/22/09 16:35
03/22/09 16:35
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
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......
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Stopwatch ?
[Re: EvilSOB]
#257347
03/22/09 20:14
03/22/09 20:14
|
Joined: Jul 2002
Posts: 4,436 Germany, Luebeck
Xarthor
Expert
|
Expert
Joined: Jul 2002
Posts: 4,436
Germany, Luebeck
|
You could also use total_secs: http://www.conitec.net/beta/atotal_secs.htmJust 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.
|
|
|
Re: Stopwatch ?
[Re: EvilSOB]
#257577
03/24/09 02:08
03/24/09 02:08
|
Joined: Mar 2009
Posts: 5 Orlando, FL
JMichalc
Newbie
|
Newbie
Joined: Mar 2009
Posts: 5
Orlando, FL
|
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: 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 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!
Last edited by JMichalc; 03/24/09 02:16.
|
|
|
Re: Stopwatch ?
[Re: JMichalc]
#257579
03/24/09 02:12
03/24/09 02:12
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Thats what I thought, I always forget the % operator...
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Stopwatch ?
[Re: EvilSOB]
#257580
03/24/09 02:19
03/24/09 02:19
|
Joined: Mar 2009
Posts: 5 Orlando, FL
JMichalc
Newbie
|
Newbie
Joined: Mar 2009
Posts: 5
Orlando, FL
|
yeah, its a good one to keep in mind another option was using Gamestudio's built in "cycle" function: which wraps x to be between a and b, so in this case: minutes += integer(seconds / 60);
seconds = wrap(seconds, 0, 60);
Last edited by JMichalc; 03/24/09 02:20.
|
|
|
Moderated by mk_1, Perro, rayp, Realspawn, Rei_Ayanami, rvL_eXile, Spirit, Superku, Tobias, TSG_Torsten, VeT
|