Need Help With Syntax and Array Iteration

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

Need Help With Syntax and Array Iteration

Postby pele'sghost » Wed Sep 21, 2022 3:48 pm

Hello,
I have some questions about scripting.

I am wanting to assign a number for each button like

#define 0 = PS4_OPTIONS
#define 1 = PS4_L2
#define 2 = PS4_SQUARE
#define 3 = PS4_R2
#define 4 = PS4_L1
#define 5 = PS4_TRIANGLE
#define 6 = PS4_R1
#define 7 = PS4_L3
#define 8 = PS4_TOUCH
#define 9 = PS4_R3

Thus assigning a button to each number 0 thru 9.
I then want to set an array of four-digit numbers.
const curated_list[] = { 1234, 1111, 5678 }
I want to have an activation button that begins to iterate the list.

what is the syntax to use a for loop?
for(i=0; i < curated_list - 1; i++) I am unsure.

I also am unsure how it will equate each number in the list to a button press.
[UPDATE]
So I can take the i and split it to read it?
1,2,3,4
then press each button
and move on
So when it grabs i it would have to do a combo that could be dynamic based on each number of the element in the array.

Does anyone know how to do these things using the Titan Two syntax???
User avatar
pele'sghost
Master Sergeant
Master Sergeant
 
Posts: 41
Joined: Tue Aug 15, 2017 12:04 am

Re: Need Help With Syntax and Array Iteration

Postby Scachi » Thu Sep 22, 2022 11:47 pm

why not create an array with each button in its own index ?
uint8 btnlist[] {
1,2,3,4,
1,1,1,1,
5,6,7,8
};

i= 0;
then use a combo that presses i+0, i+1, i+2, i+3
and at the end of the combo increase the i with +4 to run the next line of 4 buttons to press
when i has reached size of btnlist either do not restart the combo or restart at (i)ndex 0 or wherever you want again.

something like that:
Code: Select all
uint8 i = 0;
uint8 btnlist[] = {
1,2,3,4,
1,1,1,1,
5,6,7,8,
};
 
 
main {
    if (i < sizeof(btnlist)) combo_run(cBtns);
}
 
combo cBtns {
    set_val(btnlist[i+0], 100);
    set_val(btnlist[i+1], 100);
    set_val(btnlist[i+2], 100);
    set_val(btnlist[i+3], 100);
    wait(1000);
    i+= 4;
    printf("index: %d", i);
}
 
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: Need Help With Syntax and Array Iteration

Postby pele'sghost » Sat Sep 24, 2022 12:57 am

Hello,

These are my first two attempts.

I am playing around with your idea as well.

This code stops iterating the array before the end.

maybe you can see what I am doing wrong that makes it stop before end.

It iterates the whole array when conditionals are removed, i.e if count == 4, etc.

The first one with just the if statements, stops looping after 14 numbers out of 40.

The second one goes 18 and stops.

To print sizeof A, it shows 40 but with the for loop stopping and that being the condition at which it stops maybe this is it. I don't know.

UPDATE!!!!


I Removed the print statements and left only the one on 9 since 6969 is at the end of the array and now it is making it to the end.
I was only using them to debug, and it was the very thing causing it to stop.


Code: Select all
 
#pragma METAINFO("code_lock", 1, 0, "ThaGhost")
 
#include <ps4.gph>
 
uint8 A[] = {
    1, 2, 3, 4,
    1, 1, 1, 1,
    0, 0, 0, 0,
    1, 2, 1, 2,
    7, 7, 7, 7,
    1, 0, 0, 4,
    2, 0, 0, 0,
    4, 4, 4, 4,
    2, 2, 2, 2,
        6, 9, 6, 9       
};
 
int count = 0;
 
int i;
 
main {
 
    if(event_release(PS4_TRIANGLE)) {
 
        for(i = 0; i < sizeof(A); ++i) {
 
            if(A[i] == 0) {
                set_val(PS4_OPTIONS, 100);
                combo_run(pause);
                printf("The Number: %d", A[i]);
                count++;
                printf("The Count: %d", count);
            }
            if(A[i] == 1) {
                set_val(PS4_L2, 100);
                combo_run(pause);
                printf("The Number: %d", A[i]);
                count++;
                printf("The Count: %d", count);
            }
            if(A[i] == 2) {
                set_val(PS4_SQUARE, 100);
                combo_run(pause);
                printf("The Number: %d", A[i]);
                count++;
                printf("The Count: %d", count);
            }
            if(A[i] == 3) {
                set_val(PS4_R2, 100);
                combo_run(pause);
                printf("The Number: %d", A[i]);
                count++;
                printf("The Count: %d", count);
            }
            if(A[i] == 4) {
                set_val(PS4_L1, 100);
                combo_run(pause);
                printf("The Number: %d", A[i]);
                count++;
                printf("The Count: %d", count);
            }
            if(A[i] == 5) {
                set_val(PS4_TRIANGLE, 100);
                combo_run(pause);
                printf("The Number: %d", A[i]);
                count++;
                printf("The Count: %d", count);
            }
            if(A[i] == 6) {
                set_val(PS4_R1, 100);
                combo_run(pause);
                printf("The Number: %d", A[i]);
                count++;
                printf("The Count: %d", count);
            }
            if(A[i] == 7) {
                set_val(PS4_L3, 100);
                combo_run(pause);
                printf("The Number: %d", A[i]);
                count++;
                printf("The Count: %d", count);
            }
            if(A[i] == 8) {
                set_val(PS4_TOUCH, 100);
                combo_run(pause);
                printf("The Number: %d", A[i]);
                count++;
                printf("The Count: %d", count);
            }
            if(A[i] == 9) {
                set_val(PS4_R3, 100);
                combo_run(pause);
                printf("The Number: %d", A[i]);
                count++;
                printf("The Count: %d", count);
            }
        }
    }
}
combo pause {
    wait(1000);
}
 



This one also stops iterating the array before the end as well.

Code: Select all
 
#pragma METAINFO("code_lock", 1, 0, "ThaGhost")
 
#include <ps4.gph>
 
uint8 A[] = {
    1, 2, 3, 4,
    1, 1, 1, 1,
    0, 0, 0, 0,
    1, 2, 1, 2,
    7, 7, 7, 7,
    1, 0, 0, 4,
    2, 0, 0, 0,
    4, 4, 4, 4,
        2, 2, 2, 2,
    6, 9, 6, 9            
};
 
int count = 0;
 
int i;
 
main {
 
    if(event_release(PS4_TRIANGLE)) {
 
        for(i = 0; i < sizeof(A); i++) {
 
            if(count == 4) {
                combo_run(pause);
                count = 0;
                combo_run(pause);
                set_val(PS4_TRIANGLE, 100);
                combo_run(pause);
            }
 
            //if(count < 4) {
 
                switch(A[i]) {
                    case 0:
                        set_val(PS4_OPTIONS, 100);
                        combo_run(pause);
                        count++;
                        printf("The Count: %d", count);   
                        printf("SWITCH: %d", A[i]);
                        break;
                    case 1:
                        set_val(PS4_L2, 100);
                        combo_run(pause);
                        count++;
                        printf("The Count: %d", count);
                        printf("SWITCH: %d", A[i]);
                        break;
                    case 2:
                        //combo_run(press_2);
                        set_val(PS4_SQUARE, 100);
                        combo_run(pause);
                        count++;
                        printf("The Count: %d", count);
                        printf("SWITCH: %d", A[i]);
                        break;
                    case 3:
                        set_val(PS4_R2, 100);
                        combo_run(pause);
                        count++;
                        printf("The Count: %d", count);
                        printf("SWITCH: %d", A[i]);
                        break;
                    case 4:
                        set_val(PS4_L1, 100);
                        combo_run(pause);
                        count++;
                        printf("The Count: %d", count);
                        printf("SWITCH: %d", A[i]);
                        break;
                    case 5:
                        set_val(PS4_TRIANGLE, 100);
                        combo_run(pause);
                        count++;
                        printf("The Count: %d", count);
                        printf("SWITCH: %d", A[i]);
                        break;
                    case 6:
                        set_val(PS4_R1, 100);
                        combo_run(pause);
                        count++;
                        printf("The Count: %d", count);
                        printf("SWITCH: %d", A[i]);
                        break;
                    case 7:
                        set_val(PS4_L3, 100);
                        combo_run(pause);
                        count++;
                        printf("The Count: %d", count);
                        printf("SWITCH: %d", A[i]);
                        break;
                    case 8:
                        set_val(PS4_TOUCH, 100);
                        combo_run(pause);
                        count++;
                        printf("The Count: %d", count);
                        printf("SWITCH: %d", A[i]);
                        break;
                    case 9:
                        set_val(PS4_R3, 100);
                        combo_run(pause);
                        count++;
                        printf("The Count: %d", count);
                        printf("SWITCH: %d", A[i]);
                        break;
 
                    default:
                        printf("All numbers have ran\n");
                }
            //}
        }
    }
}
 
combo pause {
    wait(1000);
}
 


Thanks in advance!
User avatar
pele'sghost
Master Sergeant
Master Sergeant
 
Posts: 41
Joined: Tue Aug 15, 2017 12:04 am

Re: Need Help With Syntax and Array Iteration

Postby Scachi » Sat Sep 24, 2022 6:01 pm

You may want to read the wiki scripting section and code examples.

Don't use a for loop like that. On the T2 the main {} gets called each 1 ms or faster.
When the whole code isn't able to return fast enough the T2 resets as the T2 thinks it got locked up.

Your code just runs all the stuff in 1ms, or it tries to. Your "pause" combo has no effect.
You can think of it is like it gets spawned and it runs in parallel to the main.

The reason I put the increase of the counter into the combo itself in my code is to define how fast it progresses through the button list.

You almost always want to use a combo all the time when you want to define any output, like pressing or releasing a button as a button press requires:
one or more buttons to press
the time to press
the time to release

if you don't define the time to press or release some buttons might not be registered by the game at all as it may miss that 1ms short button press or simply ignore it.

Perhaps this will help you:
Code: Select all
#include <ps4.gph>
 
// buttons to press
uint8 A[] = {
    1, 2, 3, 4,
    1, 1, 1, 1,
    0, 0, 0, 0,
    1, 2, 1, 2,
    7, 7, 7, 7,
    1, 0, 0, 4,
    2, 0, 0, 0,
    4, 4, 4, 4,
    2, 2, 2, 2,
    6, 9, 6, 9       
};
 
// mapping of nr to button index
uint8 BTN[] = {PS4_OPTIONS, PS4_L2, PS4_SQUARE, PS4_R2, PS4_L1, PS4_TRIANGLE, PS4_R1, PS4_L3, PS4_TOUCH, PS4_R3};
 
bool run = FALSE; // script run flag, set to TRUE by release TRIANGLE
uint8 i = 0; // keep track of current A index to press
 
main {
    if (event_release(PS4_TRIANGLE)) run = TRUE;
 
    if (run) combo_run(cBtns);
}
 
combo cBtns {
    set_val(BTN[A[i]], 100); // pressing button
    wait(100); // repeat line above for time: 100
    set_val(BTN[A[i]], 0); // releasing button
    wait(100); // repeat line above for time: 100
    i++;
    printf("index: %d", i);
    if  (i >= sizeof(A)) {
        run = FALSE;
        i = 0;
        printf("completed");
    }
}
 


Run the code and take a look at the "Device Monitor" when you press Triangle.
If you can't see the button to be pressed lighting up at all in the Device Monitor it is likely that you did do something wrong with your code as it may press the buttons to fast to recognize with your eyes.
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: Need Help With Syntax and Array Iteration

Postby pele'sghost » Sun Sep 25, 2022 6:26 pm

Thanks so much for getting me lined out. I didn't realize that was what Main was doing.
User avatar
pele'sghost
Master Sergeant
Master Sergeant
 
Posts: 41
Joined: Tue Aug 15, 2017 12:04 am


Return to GPC2 Script Programming

Who is online

Users browsing this forum: kubawaz and 126 guests