Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
M1 Oversampling
by jcl. 04/26/24 11:12
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
MT5 bridge not working on MT5 v. 5 build 4160
by EternallyCurious. 04/25/24 20:49
Trading Journey
by howardR. 04/24/24 20:04
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Scripts not found
by juergen_wue. 04/20/24 18:51
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (AndrewAMD, wandaluciaia, 1 invisible), 798 guests, and 6 spiders.
Key: Admin, Global Mod, Mod
Newest Members
wandaluciaia, Mega_Rod, EternallyCurious, howardR, 11honza11
19049 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Stopwatch ? #257303
03/22/09 14:35
03/22/09 14:35
Joined: Mar 2008
Posts: 104
theDust Offline OP
Member
theDust  Offline OP
Member

Joined: Mar 2008
Posts: 104
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 ?

Re: Stopwatch ? [Re: theDust] #257310
03/22/09 15:32
03/22/09 15:32
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
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
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...


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: Stopwatch ? [Re: EvilSOB] #257318
03/22/09 16:28
03/22/09 16:28
Joined: Mar 2008
Posts: 104
theDust Offline OP
Member
theDust  Offline OP
Member

Joined: Mar 2008
Posts: 104
Wonderfull, big thx. It's also a nice code to learn a bit more about lite-c smile

Re: Stopwatch ? [Re: theDust] #257319
03/22/09 16:35
03/22/09 16:35
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
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 Offline
Expert
Xarthor  Offline
Expert

Joined: Jul 2002
Posts: 4,436
Germany, Luebeck
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.

Re: Stopwatch ? [Re: Xarthor] #257360
03/22/09 21:41
03/22/09 21:41
Joined: Oct 2007
Posts: 5,210
İstanbul, Turkey
Quad Offline
Senior Expert
Quad  Offline
Senior Expert

Joined: Oct 2007
Posts: 5,210
İstanbul, Turkey
to be more preciese you can use wait(-0.1); too.


3333333333
Re: Stopwatch ? [Re: EvilSOB] #257577
03/24/09 02:08
03/24/09 02:08
Joined: Mar 2009
Posts: 5
Orlando, FL
J
JMichalc Offline
Newbie
JMichalc  Offline
Newbie
J

Joined: Mar 2009
Posts: 5
Orlando, FL
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!

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 Offline
Expert
EvilSOB  Offline
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
J
JMichalc Offline
Newbie
JMichalc  Offline
Newbie
J

Joined: Mar 2009
Posts: 5
Orlando, FL
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);


Last edited by JMichalc; 03/24/09 02:20.
Re: Stopwatch ? [Re: JMichalc] #411401
11/16/12 11:03
11/16/12 11:03
Joined: Nov 2012
Posts: 18
Germany
E
elevationmind Offline
Newbie
elevationmind  Offline
Newbie
E

Joined: Nov 2012
Posts: 18
Germany
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?

Page 1 of 2 1 2

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1