For Loop Syntax and Clear Stored Data

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

For Loop Syntax and Clear Stored Data

Postby Convivium » Tue Jun 11, 2019 4:51 pm

I found this For Loop in the the description for Printf in the documentation guide.

Code: Select all
init {
    int ch;
    for(ch=85; ch<=100; ++ch) {
       printf("ASCII: %d, Character: %c", ch, ch);
    }
}


However, when I change the For Loop criteria to display more of the ASCII characters (40 - 127), the Loop only prints 35 characters.

Code: Select all
init {
    int ch;
    for(ch=40; ch<=127; ++ch) {
       printf("ASCII: %d, Character: %c", ch, ch);
    }
}


1) Is there a limit on how many times a For Loop will run?

2) What is the correct syntax for the For Loop on the Titan 2 and GTuner 4?

Normally it's:
for (initial ; final ; increment)


But if I try:

Code: Select all
init {
    uint8 b = 100;
    int ch;
 
    for(ch>60; ch<=b; ++ch) {
       printf("ASCII: %d, Character: %c", ch, ch);
    }
}


I get really unexpected results. Results that aren't even in the given test conditions.

Any thoughts on this?

What I was hoping to achieve was to create a For Loop that will go through a predefined array and pull out numbers from the pointer referenced in the For Loop.

Should I nest a For Loop inside another For Loop?

I would appreciate any suggestions?

Lastly, is there a script word to call in the main section of my script that will clear or reset any persistent data stored in the PMEM?

For example, if I had a anti-recoil script that stored the anti-recoil values in the PMEM. After the game was over, I would press a button sequence that would set the PMEM back to default or if I had init conditions set the button sequence would run the script like when it is first loaded.

I guess I am looking for a way to restart the memory slot from the beginning.

Let me know.

THANKS!

(Repost: Accidentally posted this on the Titan One Scripting Topic. I deleted that Thread and made this one)
User avatar
Convivium
Master Sergeant
Master Sergeant
 
Posts: 26
Joined: Wed Mar 27, 2019 2:45 am
Location: Clemson, SC

Re: For Loop Syntax and Clear Stored Data

Postby Scachi » Tue Jun 11, 2019 5:44 pm

The prinft output will get skipped when outputting to many line in a single init or main run.
It is only the printf command that gets truncated to avoid GTuner IV getting "unresponsive" on your PC running it.

You can reload a slot with the mslot_load function : https://www.consoletuner.com/wiki/index ... mslot_load
When you don't write modications of variable values back to pmem you can reload the previous values by reloading the script with above function. Reloading the script will run the init section again.

I don't know of a gpc function to restore the default settings of a script when you have used pmem_write & pmem_save to write change back to a slot.
You can use a custom header file to set pmem values and reload the script via a single string: viewtopic.php?f=23&t=11288
This uses the IC configuration string that you can get by clicking on the small icons in the title bar of the Interactive Configuration of a script.
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: For Loop Syntax and Clear Stored Data

Postby Convivium » Tue Jun 11, 2019 6:44 pm

Scachi wrote:The prinft output will get skipped when outputting to many line in a single init or main run.
It is only the printf command that gets truncated to avoid GTuner IV getting "unresponsive" on your PC running it.

You can reload a slot with the mslot_load function : https://www.consoletuner.com/wiki/index ... mslot_load
When you don't write modications of variable values back to pmem you can reload the previous values by reloading the script with above function. Reloading the script will run the init section again.

I don't know of a gpc function to restore the default settings of a script when you have used pmem_write & pmem_save to write change back to a slot.
You can use a custom header file to set pmem values and reload the script via a single string: viewtopic.php?f=23&t=11288
This uses the IC configuration string that you can get by clicking on the small icons in the title bar of the Interactive Configuration of a script.


Gotcha!

Thanks for the reply.

The mslot_load is what I am using now but didn't know if there was other ways.

I will check that custom header out though.

Any thoughts on pulling certain values out of a predefined array using the For Loop.

For example, say I want to pull values 6 through 8 (65, 75, 85) from T[ ] but only when A[ ] is 0, 1 or 2 (0, 11, 15)

Code: Select all
 
 
const uint8 A[ ] = {0, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47};
const uint8 T[ ] = {0, 15, 25, 35, 45, 55, 65, 75, 85, 95, 105};
 
 
User avatar
Convivium
Master Sergeant
Master Sergeant
 
Posts: 26
Joined: Wed Mar 27, 2019 2:45 am
Location: Clemson, SC

Re: For Loop Syntax and Clear Stored Data

Postby Scachi » Tue Jun 11, 2019 7:15 pm

I don't know how the relate to each other..if you can define your array that the matching index is also the target index for the other array you can get rid of some extra lookup table.
something like this will work then:
Code: Select all
/* org 
const uint8 A[ ] = {0, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47};
const uint8 T[ ] = {0, 15, 25, 35, 45, 55, 65, 75, 85, 95, 105};
*/

 
// new
const uint8 A[ ] = {0, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47};
const uint8 T[ ] = {65, 75, 85, 0, 15, 25, 35, 45, 55, 95, 105}; // <- resort to match index in A
 
init {
    uint8 setindex;
    setindex=SearchSet(11);
    if (setindex!=255) {
        printf("setindex %d, val0: %d, val1: %d, val2: %d",setindex,T[setindex*3],T[setindex*3+1],T[setindex*3+2]);
    }
 
    setindex=SearchSet(15);
    if (setindex!=255) {
        printf("setindex %d, val0: %d, val1: %d, val2: %d",setindex,T[setindex*3],T[setindex*3+1],T[setindex*3+2]);
    }
 
    setindex=SearchSet(19);
    if (setindex!=255) {
        printf("setindex %d, val0: %d, val1: %d, val2: %d",setindex,T[setindex*3],T[setindex*3+1],T[setindex*3+2]);
    }
}
 
bool SearchSet(uint8 searchval)
{
    uint8 i;
    for (i=0;i<sizeof(A)-1;i++) {
        if (A[i]==searchval) return (i/3);
    }
    return 255;
}


Here is some code I was using for anti recoil with different values matching for the time the fire button is pressed:
Code: Select all
 
uint32 T[]= {0,400}; // time in ms the button has to be pressed down for the values to be used
int8  AX[]= {0 ,0  }// x anti recoil when time reached
int8  AY[]= {30,15}// y anti recoil when time reached
 
init {
  // some error checking, number of values have to match the number of times
  if (sizeof(T)/4 != sizeof(AX)) printf("number of times (%d) and x values (%d) have to match",sizeof(T)/4,sizeof(AX));
  if (sizeof(T)/4 != sizeof(AY)) printf("number of times (%d) and y values (%d) have to match",sizeof(T)/4,sizeof(AY));
}
 
main {
 
  if (get_val(BUTTON_5)) AROverTime(); // when fire button is active use anti recoil
}
 
void AROverTime() {
  int8 i;
  uint32 ta = time_active(BUTTON_5);
 
  static int last=0; // for information in the output panel
 
  for (i=sizeof(T)/4;i>=0;i--) {
    if (ta >= T[i]) {
 
      if (i != last) { last=i; printf("active entry %d, time %ld, applying x: %d, y: %d",i,T[i],AX[i],AY[i]); }// for information in the output panel
 
      AntiRecoil(STICK_1_X,(fix32)AX[i]); // apply the X anti recoil value
      AntiRecoil(STICK_1_Y,(fix32)AY[i]); // apply the y anti recoil value
      return;
    }
  }
}
 
 
// antithesis antirecoil
void AntiRecoil (uint8 axis, fix32 recoil)
{
  fix32 RY = get_actual(STICK_1_Y);
  fix32 RX = get_actual(STICK_1_X);
 
  if (get_val(BUTTON_5) && (sqrt(RX*RX + RY*RY)) <= abs(recoil))
  {
      if(abs(RY) <= abs(recoil))
      {
          set_val(axis,(recoil * (100.0 - abs(get_val(axis)))) / 100.0 + get_val(axis));
      }
  }
}
 
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: For Loop Syntax and Clear Stored Data

Postby Convivium » Tue Jun 11, 2019 9:05 pm

Scachi wrote:I don't know how the relate to each other..if you can define your array that the matching index is also the target index for the other array you can get rid of some extra lookup table.
something like this will work then:
Code: Select all
/* org 
const uint8 A[ ] = {0, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47};
const uint8 T[ ] = {0, 15, 25, 35, 45, 55, 65, 75, 85, 95, 105};
*/

 
// new
const uint8 A[ ] = {0, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47};
const uint8 T[ ] = {65, 75, 85, 0, 15, 25, 35, 45, 55, 95, 105}; // <- resort to match index in A
 
init {
    uint8 setindex;
    setindex=SearchSet(11);
    if (setindex!=255) {
        printf("setindex %d, val0: %d, val1: %d, val2: %d",setindex,T[setindex*3],T[setindex*3+1],T[setindex*3+2]);
    }
 
    setindex=SearchSet(15);
    if (setindex!=255) {
        printf("setindex %d, val0: %d, val1: %d, val2: %d",setindex,T[setindex*3],T[setindex*3+1],T[setindex*3+2]);
    }
 
    setindex=SearchSet(19);
    if (setindex!=255) {
        printf("setindex %d, val0: %d, val1: %d, val2: %d",setindex,T[setindex*3],T[setindex*3+1],T[setindex*3+2]);
    }
}
 
bool SearchSet(uint8 searchval)
{
    uint8 i;
    for (i=0;i<sizeof(A)-1;i++) {
        if (A[i]==searchval) return (i/3);
    }
    return 255;
}


Here is some code I was using for anti recoil with different values matching for the time the fire button is pressed:
Code: Select all
 
uint32 T[]= {0,400}; // time in ms the button has to be pressed down for the values to be used
int8  AX[]= {0 ,0  }// x anti recoil when time reached
int8  AY[]= {30,15}// y anti recoil when time reached
 
init {
  // some error checking, number of values have to match the number of times
  if (sizeof(T)/4 != sizeof(AX)) printf("number of times (%d) and x values (%d) have to match",sizeof(T)/4,sizeof(AX));
  if (sizeof(T)/4 != sizeof(AY)) printf("number of times (%d) and y values (%d) have to match",sizeof(T)/4,sizeof(AY));
}
 
main {
 
  if (get_val(BUTTON_5)) AROverTime(); // when fire button is active use anti recoil
}
 
void AROverTime() {
  int8 i;
  uint32 ta = time_active(BUTTON_5);
 
  static int last=0; // for information in the output panel
 
  for (i=sizeof(T)/4;i>=0;i--) {
    if (ta >= T[i]) {
 
      if (i != last) { last=i; printf("active entry %d, time %ld, applying x: %d, y: %d",i,T[i],AX[i],AY[i]); }// for information in the output panel
 
      AntiRecoil(STICK_1_X,(fix32)AX[i]); // apply the X anti recoil value
      AntiRecoil(STICK_1_Y,(fix32)AY[i]); // apply the y anti recoil value
      return;
    }
  }
}
 
 
// antithesis antirecoil
void AntiRecoil (uint8 axis, fix32 recoil)
{
  fix32 RY = get_actual(STICK_1_Y);
  fix32 RX = get_actual(STICK_1_X);
 
  if (get_val(BUTTON_5) && (sqrt(RX*RX + RY*RY)) <= abs(recoil))
  {
      if(abs(RY) <= abs(recoil))
      {
          set_val(axis,(recoil * (100.0 - abs(get_val(axis)))) / 100.0 + get_val(axis));
      }
  }
}
 


Cool.

Let me work with what you have here to see if I can get something to work. I've been messing around with the For Loops but they always seem to get stuck somewhere like the While Loop does sometimes if no exit conditional is added.

Will let you know.

Thanks man!
User avatar
Convivium
Master Sergeant
Master Sergeant
 
Posts: 26
Joined: Wed Mar 27, 2019 2:45 am
Location: Clemson, SC


Return to GPC2 Script Programming

Who is online

Users browsing this forum: No registered users and 99 guests