Scripting / Coding Question

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

Scripting / Coding Question

Postby jelly44 » Wed Aug 21, 2019 1:41 pm

Hi folks, I'm working on a script and I have the following question / issue:

I would like to replace the numerical values below 3.5, 5.5, 48.5 and 50.5 respectively for something with increment / decrements for variables like FFB1_WP_01--, FFB1_WP_01++, FFB2_WP_01-- and FFB2_WP_01++

Code: Select all
if(ffb_get(FFB_1, NULL) > 3.5 && ffb_get(FFB_1, NULL) < 5.5
&& ffb_get(FFB_2, NULL) > 48.5 && ffb_get(FFB_2, NULL) < 50.5)


I'm not sure what I am missing there. It just won't compile.
- My recommendation for learning C programming: 'C Programming Absolute Beginner's Guide'
- Create your own scripts with GPC2 Scripting
User avatar
jelly44
Sergeant Major of the Army
Sergeant Major of the Army
 
Posts: 220
Joined: Tue Feb 05, 2019 3:49 pm

Re: Scripting / Coding Question

Postby Scachi » Wed Aug 21, 2019 1:46 pm

jelly44 wrote:Hi folks, I'm working on a script and I have the following question / issue:

I would like to replace the numerical values below 3.5, 5.5, 48.5 and 50.5 respectively for something with increment / decrements for variables like FFB1_WP_01--, FFB1_WP_01++, FFB2_WP_01-- and FFB2_WP_01++

Code: Select all
if(ffb_get(FFB_1, NULL) > 3.5 && ffb_get(FFB_1, NULL) < 5.5
&& ffb_get(FFB_2, NULL) > 48.5 && ffb_get(FFB_2, NULL) < 50.5)


I'm not sure what I am missing there. It just won't compile.

You need to use fix32 as datatype for those variable, or cast the return value of ffb_get to the datatype your variables uses or cast your variables to fix32 in the if statement.
Code: Select all
 
fix32 a,b,c,d;
 
main {
    if(ffb_get(FFB_1, NULL) > a && ffb_get(FFB_1, NULL) < b
    && ffb_get(FFB_2, NULL) > c && ffb_get(FFB_2, NULL) < d)
     printf("123");
}

or
Code: Select all
uint32 a,b,c,d;
 
main {
    if(ffb_get(FFB_1, NULL) > (fix32)a && ffb_get(FFB_1, NULL) < (fix32)b
    && ffb_get(FFB_2, NULL) > (fix32)c && ffb_get(FFB_2, NULL) < (fix32)d)
     printf("123");
}
Last edited by Scachi on Wed Aug 21, 2019 1:51 pm, edited 1 time in total.
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: Scripting / Coding Question

Postby jelly44 » Wed Aug 21, 2019 1:50 pm

Thank you, Scachi!

On your last statement, if I recast the variable in the if statement, will that increase its size? Right now I'm using uint8.
- My recommendation for learning C programming: 'C Programming Absolute Beginner's Guide'
- Create your own scripts with GPC2 Scripting
User avatar
jelly44
Sergeant Major of the Army
Sergeant Major of the Army
 
Posts: 220
Joined: Tue Feb 05, 2019 3:49 pm

Re: Scripting / Coding Question

Postby Scachi » Wed Aug 21, 2019 1:55 pm

Cast isn't increasing the value. It just makes it possible to match the other datatype for being able to compare the values.
So you still need to use something like
yourvariable++;
to increase it at some location in your code.
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: Scripting / Coding Question

Postby jelly44 » Wed Aug 21, 2019 1:56 pm

Thank you! I'll be testing it soon. Cheers
- My recommendation for learning C programming: 'C Programming Absolute Beginner's Guide'
- Create your own scripts with GPC2 Scripting
User avatar
jelly44
Sergeant Major of the Army
Sergeant Major of the Army
 
Posts: 220
Joined: Tue Feb 05, 2019 3:49 pm

Re: Scripting / Coding Question

Postby jelly44 » Wed Aug 21, 2019 2:56 pm

Scachi wrote:So you still need to use something like
yourvariable++;
to increase it at some location in your code.


Could I apply the increment and decrement in the if statement?

Code: Select all
uint32 a,b;
 
main {
    if(ffb_get(FFB_1, NULL) > (fix32)a-- && ffb_get(FFB_1, NULL) < (fix32)a++
    && ffb_get(FFB_2, NULL) > (fix32)b-- && ffb_get(FFB_2, NULL) < (fix32)b++)
     printf("123");
}


I basically want to input the FFB_1 = a and FFB_2 = b values in the interactive configuration menu and use them as in the code above.
- My recommendation for learning C programming: 'C Programming Absolute Beginner's Guide'
- Create your own scripts with GPC2 Scripting
User avatar
jelly44
Sergeant Major of the Army
Sergeant Major of the Army
 
Posts: 220
Joined: Tue Feb 05, 2019 3:49 pm

Re: Scripting / Coding Question

Postby Scachi » Wed Aug 21, 2019 5:10 pm

I am sorry. I think I didn't understand your question about changing the size correclty.
a++ , a-- are changing the value of the variable. It is the same as "a = a +1" or "a = a -1"
I don't think that this is what you want to do in the if statement.

To compare the current ffb to the value read from the IC but with some allowed variation you have to use it like that:
Code: Select all
 
uint8 a,b;
 
main {
    if (ffb_get(FFB_1, NULL) > (fix32)(a-1) &&
        ffb_get(FFB_1, NULL) < (fix32)(a+1) &&
        ffb_get(FFB_2, NULL) > (fix32)(b-1) &&
        ffb_get(FFB_2, NULL) < (fix32)(b+1))
    {
        printf("123");
    }
}
 

or
Code: Select all
uint8 a,b;
uint8 range=1;
 
main {
    if (ffb_get(FFB_1, NULL) > (fix32)(a-range) &&
        ffb_get(FFB_1, NULL) < (fix32)(a+range) &&
        ffb_get(FFB_2, NULL) > (fix32)(b-range) &&
        ffb_get(FFB_2, NULL) < (fix32)(b+range))
    {
        printf("123");
    }
}
 
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: Scripting / Coding Question

Postby jelly44 » Thu Aug 22, 2019 2:47 pm

Scachi wrote:I am sorry. I think I didn't understand your question about changing the size correctly.


It's my fault for not explaining it correctly.

I will give it a try, not sure I fully grasp it yet.

Basically, I will add the FFB_1 and FFB_2 values for a given weapon in the interactive menu of my script and then if they fall within the logical statement values, a set of actions take place ... rapid fire, anti-recoil and so on.
- My recommendation for learning C programming: 'C Programming Absolute Beginner's Guide'
- Create your own scripts with GPC2 Scripting
User avatar
jelly44
Sergeant Major of the Army
Sergeant Major of the Army
 
Posts: 220
Joined: Tue Feb 05, 2019 3:49 pm

Re: Scripting / Coding Question

Postby jelly44 » Fri Sep 06, 2019 8:37 am

Back from holidays, back to this topic :)

I am basically out of memory space in the interactive configuration, so in order to avoid creating more variables and increasing the script size I would like to add the FFB_1 and FFB_2 values for a given weapon in the interactive configuration menu and then whenever the script captures FFB1 and FFB2 values within that +1 and -1 range, a series of anti recoil values will kick in.

I hope you can see below what I am trying to accomplish:

Code: Select all
#pragma METAINFO("Int_Conf_Test", 1, 0, "jelly44")
/* *****************************************************************************
<cfgdesc>
 
[Weapon 2:]
color       = #F79000
collapsible = 2
 
[Standing Anti Recoil Settings 2st Weapon]
group          = true
color          = #908000
collapsible    = 2
shortdesc      = <font color="blue">STANDING ANTI RECOIL: </font> <font color="green"> Anti Recoil vertical pull force compensation.</font>
byteoffset     = 39
bitsize        = 32
control        = spinboxf
default        = 600
minimum        = -10000
maximum        = 10000
step           = 1
decimals       = 2
 
[Crouch Anti Recoil Settings 2st Weapon]
group          = true
color          = #908000
collapsible    = 2
shortdesc      = <font color="blue">CROUCH ANTI RECOIL: </font> <font color="green"> Anti Recoil vertical pull force compensation.</font>
byteoffset     = 43
bitsize        = 32
control        = spinboxf
default        = 430
minimum        = -10000
maximum        = 10000
step           = 1
decimals       = 2
 
[Prone Anti Recoil Settings 2st Weapon]
group          = true
color          = #908000
collapsible    = 2
shortdesc      = <font color="blue">PRONE ANTI RECOIL: </font> <font color="green"> Anti Recoil vertical pull force compensation.</font>
byteoffset     = 47
bitsize        = 32
control        = spinboxf
default        = 400
minimum        = -10000
maximum        = 10000
step           = 1
decimals       = 2
 
[FFB_1 2st Weapon]
group          = true
color          = #908000
collapsible    = 2
shortdesc      = <font color=#FF6B00>FFB 1: </font> <font color="green"> FFB_1 value from Device Monitor.</font>
byteoffset     = 51
bitsize        = 8
control        = spinboxf
default        = 1420
minimum        = 0
maximum        = 25500
step           = 1
decimals       = 2
 
[FFB_2 2st Weapon]
group          = true
color          = #908000
collapsible    = 2
shortdesc      = <font color=#FF6B00>FFB 2: </font> <font color="green"> FFB_1 value from Device Monitor.</font>
byteoffset     = 52
bitsize        = 8
control        = spinboxf
default        = 1420
minimum        = 0
maximum        = 25500
step           = 1
decimals       = 2
 
</cfgdesc>
***************************************************************************** */

 
fix32 ARecoil_V_Standing_WP_02;
fix32 ARecoil_V_Crouch_WP_02;
fix32 ARecoil_V_Prone_WP_02;
uint8 FFB1_WP_02;
uint8 FFB2_WP_02;
 
init {
    pmem_load(); // Load permanent memory before use any pmem operation
    pmem_read(39, &ARecoil_V_Standing_WP_02); //4 bytes == 32bits
    pmem_read(43, &ARecoil_V_Crouch_WP_02); //4 bytes == 32bits
    pmem_read(47, &ARecoil_V_Prone_WP_02); //4 bytes == 32bits
    pmem_read(51, &FFB1_WP_02); //1 byte == 8bits
    pmem_read(52, &FFB2_WP_02); //1 byte == 8bits
}
 
main {
if(ffb_get(FFB_1, NULL) > (fix32)FFB1_WP_02-- && ffb_get(FFB_1, NULL) < (fix32)FFB1_WP_02++
&& ffb_get(FFB_2, NULL) > (fix32)FFB2_WP_02-- && ffb_get(FFB_2, NULL) < (fix32)FFB2_WP_02++){
    ARecoil_V_Standing_WP_02 = 6.0;
    ARecoil_V_Crouch_WP_02 = 5.0;
    ARecoil_V_Prone_WP_02 = 4.0;
}
}
 
- My recommendation for learning C programming: 'C Programming Absolute Beginner's Guide'
- Create your own scripts with GPC2 Scripting
User avatar
jelly44
Sergeant Major of the Army
Sergeant Major of the Army
 
Posts: 220
Joined: Tue Feb 05, 2019 3:49 pm

Re: Scripting / Coding Question

Postby Scachi » Fri Sep 06, 2019 11:10 am

I think using spinboxf with bitsize 8 and decimals 2 storing it into an uint8 type variable doesn't make much sense.
uint8 value range is 0..255 https://www.consoletuner.com/wiki/index ... data_types
You can simply use a "spinbox" instead in this case.
The smallest useful bitsize to store a decimal into Interactive Configuration/pmem is bitsize=16
fix32 resulting min/max -127.something/127.something ?
where real fix32 (bitsize 32) is min/max -32768.000000 to 32767.999985

So when using antirecoil values as decimals you can save pmem space by storing it in pmem with bitsize 16 as the effective anti recoil value range is -100.0/100.0

fix16 (fix32 bitsize 16) Example:
Code: Select all
 
 
/* IC
<cfgdesc>
[Sticknoise]
group     = true
shortdesc    = Deadzone
byteoffset= 1
bitsize        = 16
control        = spinboxf
default        = 432
minimum        = 0
maximum        = 2000
step            = 1
decimals    = 2
</cfgdesc>
 
*/

 
fix32  StickNoise;
 
init {
    pmem_load();
StickNoise = pmem_read_int16tofix32(1); // read "fix16" value from byteoffset 1
printf("val: %f",StickNoise);
}
 
// read fix16 from pmem position ( read int16 and return it as fix32 )
fix32 pmem_read_int16tofix32 (uint8 pmem_location) {
  int16 valInt16;
  pmem_read(pmem_location, &valInt16);
  return (fix32)valInt16 / 256.0;
}
 


For the value-- or value-1 different in the if.. run this code and take a look at the output panel:
Code: Select all
uint8 valA=100;
uint8 valB=100;
 
 
init {
    printf("valA before: %d",valA);
    valA--; // changes the real value of the variable (write it back into it), same as "valA= valA-1;"
    printf("valA: after %d",valA);
 
    printf("valB before: %d",valB);
    valB-1; // doesn't change the real value of the variable, only ch
    printf("valB: after %d",valB);
}
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Next

Return to GPC2 Script Programming

Who is online

Users browsing this forum: No registered users and 77 guests