Trouble with printf, time math & variable syntax

GPC2 script programming for Titan Two. Code examples, questions, requests.

Trouble with printf, time math & variable syntax

Postby creedkiller » Sat Aug 24, 2019 10:07 pm

Code: Select all
bool StopWatch = FALSE;
int msecs, secs, mins, hrs, rsecs, rmins, rhrs;
int tsecs = 10;
int tmins = 10;
int thrs = 1;
 
main {
    //TERMINATE
    if(event_active(BUTTON_15)) {
        combo_stop(a); combo_stop(b); combo_stop(c);
        StopWatch = FALSE;
//         msecs, secs, mins, hrs, rsecs, rmins, rhrs = 0;
        msecs = 0; secs = 0; mins = 0; hrs = 0; rsecs = 0; rmins = 0; rhrs = 0;
        printf("STOP");
    }
 
    //INPUTS
    if(event_active(BUTTON_10)) {
        combo_stop(b); combo_stop(c);
        StopWatch = FALSE;
        msecs = 0; secs = 0; mins = 0; hrs = 0; rsecs = 0; rmins = 0; rhrs = 0;
        combo_restart(a);
    }
    if(event_active(BUTTON_11)) {
        combo_stop(a); combo_stop(c);
        StopWatch = FALSE;
        msecs = 0; secs = 0; mins = 0; hrs = 0; rsecs = 0; rmins = 0; rhrs = 0;
        combo_restart(b);
    }
    if(event_active(BUTTON_12)) {
        combo_stop(a); combo_stop(b);
        StopWatch = FALSE;
        msecs = 0; secs = 0; mins = 0; hrs = 0; rsecs = 0; rmins = 0; rhrs = 0;
        combo_restart(c);
    }
    if(event_active(BUTTON_19)) { printf("%d:%d:%d Remaining",rhrs,rmins,rsecs); }
    if(event_active(BUTTON_20)) { printf("%d:%d:%d",hrs,mins,secs); }
 
    //STOPWATCH
    if(StopWatch) {
        msecs = msecs + elapsed_time(); if(msecs >= 1000) {   
            msecs = msecs - 1000; secs = secs + 1; if(secs >= 60) {   
                secs = secs - 60; mins = mins + 1; if(mins >= 60) {
                    mins = mins - 60; hrs = hrs + 1; } } }
        rsecs = tsecs - secs; rmins = tmins - mins; rhrs = thrs - hrs;
    }
 
    if(hrs == 0 && mins == 1 && secs == 30 && msecs == 500) { printf("Something"); }   
    if(hrs == 0 && mins == 2 && secs == 30 && msecs == 500) { printf("Something else"); }   
    if(hrs == 0 && mins == 3 && secs == 30 && msecs == 500) { printf("Not something else"); }
 
    if(hrs == 1 && mins == 10 && secs == 10 && msecs == 500) {
        printf("Complete");
        StopWatch = FALSE;
        msecs = 0; secs = 0; mins = 0; hrs = 0; rsecs = 0; rmins = 0; rhrs = 0;
        combo_run(c);
    }
 
}
 
combo a {
    printf("1st Sequence Part 1"); wait(0);
    wait(2000);
    printf("1st Sequence Part 2"); wait(0);
    wait(2000);
    printf("1st Sequence Part 3"); wait(0);
    wait(2000);
    combo_run(b);
}
 
combo b {
    printf("2nd Sequence Part 1"); wait(0);
    wait(2000);
    printf("2nd Sequence Part 2"); wait(0);
    wait(2000);
    printf("2nd Sequence Part 3"); wait(0);
    wait(2000);
    StopWatch = TRUE;
}
 
combo c {
    printf("3rd Sequence Part 1"); wait(0);
    wait(2000);
    printf("3rd Sequence Part 2"); wait(0);
    wait(2000);
    printf("3rd Sequence Part 3"); wait(0);
    wait(2000);
    combo_run(a);
}


1. How can I stop printf from printing 2 lines instead of 1 for code lines 48, 49 & 50. Would also like to understand why it does this. Is it because the function in the VM runs twice every ms?

2. How to elegantly code time math so that BUTTON_19 doesn't print "GVM Output: 2:-3:10 Remaining" and instead prints "1:57:10"

3. Why doesn't line 12 work instead of line 13 even though it compiles without errors?

4. Is it better to make individual post about issues like these?
creedkiller
Lieutenant
Lieutenant
 
Posts: 293
Joined: Fri Mar 17, 2017 4:08 pm

Re: Trouble with printf, time math & variable syntax

Postby J2Kbr » Mon Aug 26, 2019 6:19 pm

creedkiller wrote:1. How can I stop printf from printing 2 lines instead of 1 for code lines 48, 49 & 50. Would also like to understand why it does this. Is it because the function in the VM runs twice every ms?

Correct, the VM runs at least once every ms, and it is normal run multiple times in a ms. Every time a new report from any connected controller is received, the VM is executed.

In your code, to prevent printf() to be evaluated multiple times per ms, you will need a control variable to flag the message was already printed on that ms, and test the flag to prevent run the printf again.

creedkiller wrote:2. How to elegantly code time math so that BUTTON_19 doesn't print "GVM Output: 2:-3:10 Remaining" and instead prints "1:57:10"

Maybe this? (didn't fully understand the initialization values for tsecs, tmins, thrs).
Code: Select all
rsecs = 60 - secs; rmins = 60 - mins; rhrs = 1 - hrs;


creedkiller wrote:3. Why doesn't line 12 work instead of line 13 even though it compiles without errors?

The correct construction for line 12 is:
Code: Select all
msecs = secs = mins = hrs = rsecs = rmins = rhrs = 0;


creedkiller wrote:4. Is it better to make individual post about issues like these?

That is fine. All your question are related. :smile0517:
ConsoleTuner Support Team
User avatar
J2Kbr
General of the Army
General of the Army
 
Posts: 20323
Joined: Tue Mar 18, 2014 1:39 pm

Re: Trouble with printf, time math & variable syntax

Postby creedkiller » Mon Aug 26, 2019 11:57 pm

J2Kbr wrote:
creedkiller wrote:1. How can I stop printf from printing 2 lines instead of 1 for code lines 48, 49 & 50. Would also like to understand why it does this. Is it because the function in the VM runs twice every ms?

Correct, the VM runs at least once every ms, and it is normal run multiple times in a ms. Every time a new report from any connected controller is received, the VM is executed.

In your code, to prevent printf() to be evaluated multiple times per ms, you will need a control variable to flag the message was already printed on that ms, and test the flag to prevent run the printf again.

creedkiller wrote:2. How to elegantly code time math so that BUTTON_19 doesn't print "GVM Output: 2:-3:10 Remaining" and instead prints "1:57:10"

Maybe this? (didn't fully understand the initialization values for tsecs, tmins, thrs).
Code: Select all
rsecs = 60 - secs; rmins = 60 - mins; rhrs = 1 - hrs;


creedkiller wrote:3. Why doesn't line 12 work instead of line 13 even though it compiles without errors?

The correct construction for line 12 is:
Code: Select all
msecs = secs = mins = hrs = rsecs = rmins = rhrs = 0;


creedkiller wrote:4. Is it better to make individual post about issues like these?

That is fine. All your question are related. :smile0517:

Thank you very much J2Kbr
creedkiller
Lieutenant
Lieutenant
 
Posts: 293
Joined: Fri Mar 17, 2017 4:08 pm

Re: Trouble with printf, time math & variable syntax

Postby creedkiller » Wed Aug 28, 2019 10:56 am

J2Kbr wrote:
creedkiller wrote:In your code, to prevent printf() to be evaluated multiple times per ms, you will need a control variable to flag the message was already printed on that ms, and test the flag to prevent run the printf again.

J2Kbr, could you please elaborate a little more and if you have time, a short example of how I would achieve this.
creedkiller
Lieutenant
Lieutenant
 
Posts: 293
Joined: Fri Mar 17, 2017 4:08 pm

Re: Trouble with printf, time math & variable syntax

Postby Scachi » Wed Aug 28, 2019 11:10 am

You can try to add "&& elapsed_time()" to your if condition if this fixes your multiple printf issue.
Code: Select all
if(hrs == 0 && mins == 1 && secs == 30 && msecs == 500 && elapsed_time()) { printf("Something"); }
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: Trouble with printf, time math & variable syntax

Postby creedkiller » Wed Aug 28, 2019 7:41 pm

Scachi wrote:You can try to add "&& elapsed_time()" to your if condition if this fixes your multiple printf issue.
Code: Select all
if(hrs == 0 && mins == 1 && secs == 30 && msecs == 500 && elapsed_time()) { printf("Something"); }

Cheers mate, I will give it a shot.
creedkiller
Lieutenant
Lieutenant
 
Posts: 293
Joined: Fri Mar 17, 2017 4:08 pm

Re: Trouble with printf, time math & variable syntax

Postby J2Kbr » Wed Aug 28, 2019 8:11 pm

Scachi wrote:You can try to add "&& elapsed_time()" to your if condition if this fixes your multiple printf issue.
Code: Select all
if(hrs == 0 && mins == 1 && secs == 30 && msecs == 500 && elapsed_time()) { printf("Something"); }

Very elegant solution. I would initially do with control variables, but this works much better. :joia:
ConsoleTuner Support Team
User avatar
J2Kbr
General of the Army
General of the Army
 
Posts: 20323
Joined: Tue Mar 18, 2014 1:39 pm


Return to GPC2 Script Programming

Who is online

Users browsing this forum: No registered users and 189 guests