working example of persistent variables for titan2?

GPC script programming for Titan Two device. Code examples, how to, questions, requests.

Moderators: The_Rabid_Taco, pablosscripts, bonefisher

working example of persistent variables for titan2?

Postby mpalpha » Fri Jul 13, 2018 1:52 pm

can anyone working example of persistent variables for a titan2? (not a t1 converted script)

for example, adjusting and saving a tuned value in game.

I would like add this feature to the script below

Code: Select all
#pragma METAINFO("antithesis Antirecoil", 1, 01, "antithesis")
 
fix32 RECOIL_V = 30.0;
fix32 RECOIL_H = 0.0;
fix32 RY;
fix32 RX;
 
fix32 StickNoise = 4.32;
 
main {
 
 
// DEADZONE REMOVER
    if(abs(get_actual(STICK_1_X)) < StickNoise) { set_val(STICK_1_X, 0.0); }
    if(abs(get_actual(STICK_1_Y)) < StickNoise) { set_val(STICK_1_Y, 0.0); }
    if(abs(get_actual(STICK_2_X)) < StickNoise) { set_val(STICK_2_X, 0.0); }
    if(abs(get_actual(STICK_2_Y)) < StickNoise) { set_val(STICK_2_Y, 0.0); }
 
// ANTI-RECOIL
    if (get_val (BUTTON_5))
    {   
        AntiRecoil(STICK_1_Y,RECOIL_V);
        AntiRecoil(STICK_1_X,RECOIL_H);
    }
 
}
 
void AntiRecoil (uint8 axis, fix32 recoil)
{
 
     RY = get_actual(STICK_1_Y);
     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));
        }
    }
}


Thanks.
User avatar
mpalpha
Staff Sergeant
Staff Sergeant
 
Posts: 11
Joined: Mon Jun 11, 2018 11:02 pm

Re: working example of persistent variables for titan2?

Postby Scachi » Fri Jul 13, 2018 2:15 pm

I did something like that a while ago: viewtopic.php?f=20&t=8699#p62983

Not for tuning in-game with button combinations, but via Interactive Configuration and loading the settings.
User avatar
Scachi
Major
Major
 
Posts: 990
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: working example of persistent variables for titan2?

Postby mpalpha » Fri Jul 13, 2018 2:24 pm

Scachi wrote:I did something like that a while ago: viewtopic.php?f=20&t=8699#p62983

Not for tuning in-game with button combinations, but via Interactive Configuration and loading the settings.


Thanks for the reply. In my attempts the portion I got stuck on was modifying any of the fix32 values with controller inputs and then saving them, I think maybe I had the scope wrong or something I'm not sure.
User avatar
mpalpha
Staff Sergeant
Staff Sergeant
 
Posts: 11
Joined: Mon Jun 11, 2018 11:02 pm

Re: working example of persistent variables for titan2?

Postby Scachi » Fri Jul 13, 2018 2:39 pm

modifying fix32 values you need to use numbers in fix32 style, like instead of +1 you need +1.0

Here is a working example, it will allow you to adjust the values.
While firing pressing the d-pad in a direction. Every press adds +1.0 or -1.0 to the axis (up/down v,left right h)

Code: Select all
 
 
/*** interactive configuration ***
 
<cfgdesc>
 
[Sticknoise]
shortdesc    = <<<MULTILINE
The default value 4.32 should be a good start.
If you want to tweak this, set it to the max value of your sticks resting position values.
Use the "Device Monitor" to examine them.
MULTILINE
byteoffset    = 0
bitsize        = 32
control        = spinboxf
default        = 432
minimum        = 0
maximum        = 2000
step            = 1
decimals    = 2
 
[Anti Recoil Vertical]
shortdesc    = Vertical recoil compensation
byteoffset= 4
bitsize        = 32
control        = spinboxf
default        = 300
minimum        = -1000
maximum        = 1000
step            = 1
decimals    = 1
 
[Anti Recoil Horizontal]
shortdesc    = Horizontal recoil compensation
byteoffset= 8
bitsize        = 32
control        = spinboxf
default        = 0
minimum        = -1000
maximum        = 1000
step            = 1
decimals    = 1
 
 
</cfgdesc>
 
*/

 
fix32 RECOIL_V = 30.0;
fix32 RECOIL_H = 0.0;
fix32 RY;
fix32 RX;
 
fix32 StickNoise = 4.32;
 
init {
  pmem_load();
  pmem_read(0,&StickNoise);
  pmem_read(4,&RECOIL_V);
  pmem_read(8,&RECOIL_H);
}
 
main {
 
 
// DEADZONE REMOVER
    if(abs(get_actual(STICK_1_X)) < StickNoise) { set_val(STICK_1_X, 0.0); }
    if(abs(get_actual(STICK_1_Y)) < StickNoise) { set_val(STICK_1_Y, 0.0); }
    if(abs(get_actual(STICK_2_X)) < StickNoise) { set_val(STICK_2_X, 0.0); }
    if(abs(get_actual(STICK_2_Y)) < StickNoise) { set_val(STICK_2_Y, 0.0); }
 
// ANTI-RECOIL
    if (get_val (BUTTON_5))
    {
        AntiRecoil_Adjust();
        AntiRecoil(STICK_1_Y,RECOIL_V);
        AntiRecoil(STICK_1_X,RECOIL_H);
    }
 
}
 
void AntiRecoil (uint8 axis, fix32 recoil)
{
 
     RY = get_actual(STICK_1_Y);
     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));
        }
    }
}
 
void AntiRecoilAdjust() {
 
}
 
// adjusting anti recoil values
void AntiRecoil_Adjust() {
  static fix32 OLD_V = RECOIL_V;
  static fix32 OLD_H = RECOIL_H;
  if ( event_active(BUTTON_10) ) RECOIL_V--;
  if ( event_active(BUTTON_11) ) RECOIL_V++;
  RECOIL_V = clamp(RECOIL_V,-100.0,100.0);
 
  if ( event_active(BUTTON_12) ) RECOIL_H--;
  if ( event_active(BUTTON_13) ) RECOIL_H++;
  RECOIL_H = clamp(RECOIL_H,-100.0,100.0);
 
  if ( RECOIL_V != OLD_V || RECOIL_H != OLD_H ) {
    OLD_V = RECOIL_V;
    OLD_H = RECOIL_H;
    pmem_write(4,RECOIL_V);
    pmem_write(8,RECOIL_H);
    pmem_save();
    printf("GCMD:InteractiveConfiguration.Refresh"); // refresh the interactive configuration window content if open
  }
}
User avatar
Scachi
Major
Major
 
Posts: 990
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: working example of persistent variables for titan2?

Postby mpalpha » Fri Jul 13, 2018 9:12 pm

this is where I'm at so far.

in game adjustments: done :smile0517:
auto save after adjustments: done :smile0517:
interactive config: done :smile0517:
recoil Y: done :smile0517:
recoil X: done :smile0517:
toggle ads: 95% :thumbsdownsmileyanim:

all I have left is that I can't get the checkbox/boolean to toggle ADS.

Code: Select all
#pragma METAINFO("ANTIRECOIL (V+H), adjust on the fly, interactive config", 1, 00, "ETB")
 
// Frist of all, thanks to Layman, Scachi, and Antithesis
 
// Anti Recoil
// Hold Both Triggers & Press DPAD UP, DOWN, LEFT, RIGHT to adjust.
fix32 RECOIL_V           = 30.0; // Vertical Recoil  range -100.0 to 100.0
fix32 RECOIL_H           = 0.0; // Horizontal Recoil  range -100.0 to 100.0
fix32 RY; // reserved
fix32 RX; // reserved
 
// ADS Toggle
// Hold Both Triggers & Press Right Stick
bool TOGGLE_ADS          = TRUE; // Toggle ADS Default
 
fix32 StickNoise         = 4.32; // Change this value to adjust the size of the Deadzone to ignore when using a Xim Apex.
// Go to Device Monitor in Gtuner IV, flick the right stick and take note of the largest value.
 
bool ICUpdate            = FALSE; // To update the interactive configuration window set this flag to TRUE in the main loop.
bool PMUpdate            = FALSE; // To save/write down the value to pmem set this flag to TRUE in the main loop.
 
// assign pmem locations (remember to match interactive config byteoffsets)
uint8 StickNoise_PMEM    = 0; // pmem slot
uint8 RECOIL_V_PMEM      = 4; // pmem slot
uint8 RECOIL_H_PMEM      = 8; // pmem slot
uint8 TOGGLE_ADS_PMEM    = 12; // pmem slot
 
init {
    pmem_load(); // load the ui pmem
    pmem_read(StickNoise_PMEM, &StickNoise); // read the value to change from the ui
    pmem_read(RECOIL_V_PMEM, &RECOIL_V); // read the value to change from the ui
    pmem_read(RECOIL_H_PMEM, &RECOIL_H); // read the value to change from the ui
    pmem_read(TOGGLE_ADS_PMEM, &TOGGLE_ADS); // read the value to change from the ui
    StickNoise = clamp(StickNoise,0.0,255.0); // just to make sure the value is in the correct value range
    RECOIL_V = clamp(RECOIL_V,-100.0,100.0); // just to make sure the value is in the correct value range
    RECOIL_H = clamp(RECOIL_H,-100.0,100.0); // just to make sure the value is in the correct value range
}
 
main {
    if ( event_active(BUTTON_10) && is_active(BUTTON_5) && is_active(BUTTON_8) ) { // Hold Both Triggers & Press DPAD_UP
        if ( RECOIL_V > -100.0) {
            (RECOIL_V = RECOIL_V - 1.0);
        }
        pmem_write(RECOIL_V_PMEM,(fix32) RECOIL_V); // update the pmem value
        PMUpdate=TRUE; // let the value be saved to pmem
        ICUpdate=TRUE; // show the updated value in the gui without needing to close and reopen it
    }
 
    if ( event_active(BUTTON_11) && is_active(BUTTON_5) && is_active(BUTTON_8) ) { // Hold Both Triggers & Press DPAD_DOWN
        if ( RECOIL_V < 100.0) {
            (RECOIL_V = RECOIL_V + 1.0);
        }
        pmem_write(RECOIL_V_PMEM,(fix32) RECOIL_V); // update the pmem value
        PMUpdate=TRUE; // let the value be saved to pmem
        ICUpdate=TRUE; // show the updated value in the gui without needing to close and reopen it
    }
    if ( event_active(BUTTON_12) && is_active(BUTTON_5) && is_active(BUTTON_8) ) { // Hold Both Triggers & Press DPAD_LEFT
        if ( RECOIL_H > -100.0) {
            (RECOIL_H = RECOIL_H - 1.0);
        }
        pmem_write(RECOIL_H_PMEM,(fix32) RECOIL_H); // update the pmem value
        PMUpdate=TRUE; // let the value be saved to pmem
        ICUpdate=TRUE; // show the updated value in the gui without needing to close and reopen it
    }
 
    if ( event_active(BUTTON_13) && is_active(BUTTON_5) && is_active(BUTTON_8) ) { // Hold Both Triggers & Press DPAD_RIGHT
        if ( RECOIL_H < 100.0) {
            (RECOIL_H = RECOIL_H + 1.0);
        }
        pmem_write(RECOIL_H_PMEM,(fix32) RECOIL_H); // update the pmem value
        PMUpdate=TRUE; // let the value be saved to pmem
        ICUpdate=TRUE; // show the updated value in the gui without needing to close and reopen it
    }
 
    if ( event_active(BUTTON_6) && is_active(BUTTON_5) && is_active(BUTTON_8) ) { // Hold Both Triggers & Press Right Stick
        (TOGGLE_ADS = !TOGGLE_ADS);
        pmem_write(TOGGLE_ADS_PMEM,(uint8) TOGGLE_ADS); // update the pmem value
        PMUpdate=TRUE; // let the value be saved to pmem
        ICUpdate=TRUE; // show the updated value in the gui without needing to close and reopen it
    }
 
    ICRefresh();    // This functions should be called on every iteration
        PMSave();         // This functions should be called on every iteration
 
// DEADZONE REMOVER
    if(abs(get_actual(STICK_1_X)) < StickNoise) { set_val(STICK_1_X, 0.0); }
    if(abs(get_actual(STICK_1_Y)) < StickNoise) { set_val(STICK_1_Y, 0.0); }
    if(abs(get_actual(STICK_2_X)) < StickNoise) { set_val(STICK_2_X, 0.0); }
    if(abs(get_actual(STICK_2_Y)) < StickNoise) { set_val(STICK_2_Y, 0.0); }
 
// ANTI-RECOIL
    if (!TOGGLE_ADS || get_val (BUTTON_5))
    {
        AntiRecoil(STICK_1_Y,RECOIL_V);
        AntiRecoil(STICK_1_X,RECOIL_H);
    }
}
 
// update the UI, limit the update time to 1000ms (1 second) for safety
void ICRefresh() {
    static uint32 ICULast = 0;
    if (ICUpdate && ICULast < system_time())
    {
        printf("ic update");
        printf("GCMD:InteractiveConfiguration.Refresh");
        ICUpdate = FALSE;
        ICULast = system_time() + 1000;
    }
}
 
// Update the persistent memory, but not faster than 500ms (1/2 second)
void PMSave() {
    static uint32 PMSLast = 0;
    if (PMUpdate && PMSLast < system_time())
    {
        printf("pmem saved: StickNoise: %f",StickNoise);
        printf("pmem saved: RECOIL_V: %f",RECOIL_V);
        printf("pmem saved: RECOIL_H: %f",RECOIL_H);
        printf("pmem saved: TOGGLE_ADS: %f",TOGGLE_ADS);
        PMUpdate = FALSE;
        PMSLast = system_time() + 500;
        pmem_save();
    }
}
 
void AntiRecoil (uint8 axis, fix32 recoil)
{
     RY = get_actual(STICK_1_Y);
     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));
        }
    }
}
 
/***************
<cfgdesc>
[Example]
shortdesc       = <<<MULTILINE
UI updateexample
with automatic UI refresh
MULTILINE
control         = info
 
[Sticknoise]
shortdesc       = <<<MULTILINE
The default value 4.32 should be a good start.
If you want to tweak this, set it to the max value of your sticks resting position values.
Use the "Device Monitor" to examine them.
MULTILINE
byteoffset      = 0
bitsize         = 32
control         = spinboxf
default         = 432
minimum         = 0
maximum         = 2000
step            = 1
decimals        = 2
 
[Anti Recoil Vertical]
shortdesc       = Vertical recoil compensation
byteoffset      = 4
bitsize         = 32
control         = spinboxf
default         = 300
minimum         = -1000
maximum         = 1000
step            = 1
decimals        = 1
 
[Anti Recoil Horizontal]
shortdesc       = Horizontal recoil compensation
byteoffset      = 8
bitsize         = 32
control         = spinboxf
default         = 0
minimum         = -1000
maximum         = 1000
step            = 1
decimals        = 1
 
[Toggle ADS]
shortdesc       = Enable this if you would like Anti Recoil enabled while aiming down sight or during hip fire.
byteoffset      = 12
bitsize         = 1
bitoffset       = 1
control         = checkbox
default         = 1
item            = Enable
 
</cfgdesc>
 
***************/

 
User avatar
mpalpha
Staff Sergeant
Staff Sergeant
 
Posts: 11
Joined: Mon Jun 11, 2018 11:02 pm

Re: working example of persistent variables for titan2?

Postby bonefisher » Sat Jul 14, 2018 1:41 am

Code: Select all
 
#pragma METAINFO("ANTIRECOIL (V+H), adjust on the fly, interactive config", 1, 00, "ETB")
 
// Frist of all, thanks to Layman, Scachi, and Antithesis
 
 
bool ICUpdate            = FALSE; // To update the interactive configuration window set this flag to TRUE in the main loop.
bool PMUpdate            = FALSE; // To save/write down the value to pmem set this flag to TRUE in the main loop.
bool  TOGGLE_ADS_PMEM;
fix32 StickNoise_PMEM;
fix32 RECOIL_V_PMEM;
fix32 RECOIL_H_PMEM;
fix32 RY;
fix32 RX;
 
init {
    pmem_load(); // load the ui pmem
    pmem_read(0, &StickNoise_PMEM);
    pmem_read(4, &RECOIL_V_PMEM);
    pmem_read(8, &RECOIL_H_PMEM);
    TOGGLE_ADS_PMEM = (pmem_read(12) >> 7) & 0b1;
    printf("StickNoise_PMEM: %.04f", StickNoise_PMEM);
    printf("RECOIL_V_PMEM: %.04f", RECOIL_V_PMEM);
    printf("RECOIL_H_PMEM: %.04f", RECOIL_H_PMEM);
    printf("TOGGLE_ADS_PMEM: %d", TOGGLE_ADS_PMEM);
}
 
main {
    if ( event_active(BUTTON_10) && is_active(BUTTON_5) && is_active(BUTTON_8) ) { // Hold Both Triggers & Press DPAD_UP
        if ( RECOIL_V_PMEM > -100.0) {
            (RECOIL_V_PMEM = RECOIL_V_PMEM - 1.0);
        }
        pmem_write(4,RECOIL_V_PMEM); // update the pmem value
        PMUpdate=TRUE; // let the value be saved to pmem
        ICUpdate=TRUE; // show the updated value in the gui without needing to close and reopen it
    }
 
    if ( event_active(BUTTON_11) && is_active(BUTTON_5) && is_active(BUTTON_8) ) { // Hold Both Triggers & Press DPAD_DOWN
        if ( RECOIL_V_PMEM < 100.0) {
            (RECOIL_V_PMEM = RECOIL_V_PMEM + 1.0);
        }
        pmem_write(4,RECOIL_V_PMEM); // update the pmem value
        PMUpdate=TRUE; // let the value be saved to pmem
        ICUpdate=TRUE; // show the updated value in the gui without needing to close and reopen it
    }
    if ( event_active(BUTTON_12) && is_active(BUTTON_5) && is_active(BUTTON_8) ) { // Hold Both Triggers & Press DPAD_LEFT
        if ( RECOIL_H_PMEM > -100.0) {
            (RECOIL_H_PMEM = RECOIL_H_PMEM - 1.0);
        }
        pmem_write(8,RECOIL_H_PMEM); // update the pmem value
        PMUpdate=TRUE; // let the value be saved to pmem
        ICUpdate=TRUE; // show the updated value in the gui without needing to close and reopen it
    }
 
    if ( event_active(BUTTON_13) && is_active(BUTTON_5) && is_active(BUTTON_8) ) { // Hold Both Triggers & Press DPAD_RIGHT
        if ( RECOIL_H_PMEM < 100.0) {
            (RECOIL_H_PMEM = RECOIL_H_PMEM + 1.0);
        }
        pmem_write(8,RECOIL_H_PMEM); // update the pmem value
        PMUpdate=TRUE; // let the value be saved to pmem
        ICUpdate=TRUE; // show the updated value in the gui without needing to close and reopen it
    }
 
    if ( event_active(BUTTON_6) && is_active(BUTTON_5) && is_active(BUTTON_8) ) { // Hold Both Triggers & Press Right Stick
        (TOGGLE_ADS_PMEM = !TOGGLE_ADS_PMEM);
        pmem_write(12,TOGGLE_ADS_PMEM); // update the pmem value
        PMUpdate=TRUE; // let the value be saved to pmem
        ICUpdate=TRUE; // show the updated value in the gui without needing to close and reopen it
    }
 
    ICRefresh();    // This functions should be called on every iteration
        PMSave();         // This functions should be called on every iteration
 
// DEADZONE REMOVER
    if(abs(get_actual(STICK_1_X)) < StickNoise_PMEM) { set_val(STICK_1_X, 0.0); }
    if(abs(get_actual(STICK_1_Y)) < StickNoise_PMEM) { set_val(STICK_1_Y, 0.0); }
    if(abs(get_actual(STICK_2_X)) < StickNoise_PMEM) { set_val(STICK_2_X, 0.0); }
    if(abs(get_actual(STICK_2_Y)) < StickNoise_PMEM) { set_val(STICK_2_Y, 0.0); }
 
// ANTI-RECOIL
    if (!TOGGLE_ADS_PMEM || get_val (BUTTON_5))
    {
        AntiRecoil(STICK_1_Y,RECOIL_V_PMEM);
        AntiRecoil(STICK_1_X,RECOIL_H_PMEM);
    }
}
 
// update the UI, limit the update time to 1000ms (1 second) for safety
void ICRefresh() {
    static uint32 ICULast = 0;
    if (ICUpdate && ICULast < system_time())
    {
        printf("ic update");
        printf("GCMD:InteractiveConfiguration.Refresh");
        ICUpdate = FALSE;
        ICULast = system_time() + 1000;
    }
}
 
// Update the persistent memory, but not faster than 500ms (1/2 second)
void PMSave() {
    static uint32 PMSLast = 0;
    if (PMUpdate && PMSLast < system_time())
    {
        printf("StickNoise_PMEM: %.04f", StickNoise_PMEM);
        printf("RECOIL_V_PMEM: %.04f", RECOIL_V_PMEM);
        printf("RECOIL_H_PMEM: %.04f", RECOIL_H_PMEM);
        printf("TOGGLE_ADS_PMEM: %d", TOGGLE_ADS_PMEM);
        PMUpdate = FALSE;
        PMSLast = system_time() + 500;
        pmem_save();
    }
}
 
void AntiRecoil (uint8 axis, fix32 recoil)
{
     RY = get_actual(STICK_1_Y);
     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));
        }
    }
}
 
/* *****************************************************************************
<cfgdesc>
[Example]
shortdesc       = <<<MULTILINE
UI updateexample
with automatic UI refresh
MULTILINE
control         = info
 
[Sticknoise]
shortdesc       = The default value 4.32 should be a good start. If you want to tweak this, set it to the max value of your sticks resting position values. Use the "Device Monitor" to examine them.
byteoffset      = 0
bitsize         = 32
control            = spinboxf
default            = 4320000
minimum            = -100000000
maximum            = 100000000
step            = 500000
 
[Anti Recoil Vertical]
shortdesc       = Vertical recoil compensation
byteoffset      = 4
bitsize         = 32
control            = spinboxf
default            = 30000000
minimum            = -100000000
maximum            = 100000000
step            = 500000
 
[Anti Recoil Horizontal]
shortdesc       = Horizontal recoil compensation
byteoffset      = 8
bitsize         = 32
control            = spinboxf
default            = 00000000
minimum            = -100000000
maximum            = 100000000
step            = 500000
 
[Toggle ADS]
shortdesc       = Enable this if you would like Anti Recoil enabled while aiming down sight or during hip fire.
byteoffset      = 12
bitsize         = 1
bitoffset       = 7
control         = checkbox
default         = 1
item            = Enable
</cfgdesc>
***************************************************************************** */

 

See if this works for you!
bonefisher
Major General
Major General
 
Posts: 4258
Joined: Thu Jan 29, 2015 10:49 am

Re: working example of persistent variables for titan2?

Postby mpalpha » Sat Jul 14, 2018 3:18 am

it compiles, but it doesn't toggle ads
User avatar
mpalpha
Staff Sergeant
Staff Sergeant
 
Posts: 11
Joined: Mon Jun 11, 2018 11:02 pm

Re: working example of persistent variables for titan2?

Postby bonefisher » Sat Jul 14, 2018 6:09 am

Code: Select all
 
// Frist of all, thanks to Layman, Scachi, and Antithesis
 
 
bool ICUpdate            = FALSE; // To update the interactive configuration window set this flag to TRUE in the main loop.
bool PMUpdate            = FALSE; // To save/write down the value to pmem set this flag to TRUE in the main loop.
bool  TOGGLE_ADS_PMEM;
fix32 StickNoise_PMEM;
fix32 RECOIL_V_PMEM;
fix32 RECOIL_H_PMEM;
fix32 RY;
fix32 RX;
 
init {
    pmem_load(); // load the ui pmem
    pmem_read(0, &StickNoise_PMEM);
    pmem_read(4, &RECOIL_V_PMEM);
    pmem_read(8, &RECOIL_H_PMEM);
    TOGGLE_ADS_PMEM = (pmem_read(12) >> 7) & 0b1;
    printf("StickNoise_PMEM: %.04f", StickNoise_PMEM);
    printf("RECOIL_V_PMEM: %.04f", RECOIL_V_PMEM);
    printf("RECOIL_H_PMEM: %.04f", RECOIL_H_PMEM);
    printf("TOGGLE_ADS_PMEM: %d", TOGGLE_ADS_PMEM);
}
 
main {
    if ( event_active(BUTTON_10) && is_active(BUTTON_5) && is_active(BUTTON_8) ) { // Hold Both Triggers & Press DPAD_UP
        if ( RECOIL_V_PMEM > -100.0) {
            (RECOIL_V_PMEM = RECOIL_V_PMEM - 1.0);
        }
        pmem_write(4,RECOIL_V_PMEM); // update the pmem value
        PMUpdate=TRUE; // let the value be saved to pmem
        ICUpdate=TRUE; // show the updated value in the gui without needing to close and reopen it
    }
 
    if ( event_active(BUTTON_11) && is_active(BUTTON_5) && is_active(BUTTON_8) ) { // Hold Both Triggers & Press DPAD_DOWN
        if ( RECOIL_V_PMEM < 100.0) {
            (RECOIL_V_PMEM = RECOIL_V_PMEM + 1.0);
        }
        pmem_write(4,RECOIL_V_PMEM); // update the pmem value
        PMUpdate=TRUE; // let the value be saved to pmem
        ICUpdate=TRUE; // show the updated value in the gui without needing to close and reopen it
    }
    if ( event_active(BUTTON_12) && is_active(BUTTON_5) && is_active(BUTTON_8) ) { // Hold Both Triggers & Press DPAD_LEFT
        if ( RECOIL_H_PMEM > -100.0) {
            (RECOIL_H_PMEM = RECOIL_H_PMEM - 1.0);
        }
        pmem_write(8,RECOIL_H_PMEM); // update the pmem value
        PMUpdate=TRUE; // let the value be saved to pmem
        ICUpdate=TRUE; // show the updated value in the gui without needing to close and reopen it
    }
 
    if ( event_active(BUTTON_13) && is_active(BUTTON_5) && is_active(BUTTON_8) ) { // Hold Both Triggers & Press DPAD_RIGHT
        if ( RECOIL_H_PMEM < 100.0) {
            (RECOIL_H_PMEM = RECOIL_H_PMEM + 1.0);
        }
        pmem_write(8,RECOIL_H_PMEM); // update the pmem value
        PMUpdate=TRUE; // let the value be saved to pmem
        ICUpdate=TRUE; // show the updated value in the gui without needing to close and reopen it
    }
 
    if ( event_active(BUTTON_6) && is_active(BUTTON_5) && is_active(BUTTON_8) ) { // Hold Both Triggers & Press Right Stick
        if(TOGGLE_ADS_PMEM == 0) {
        TOGGLE_ADS_PMEM = 1;
    }else if(TOGGLE_ADS_PMEM == 1) {
        TOGGLE_ADS_PMEM = 0;
    }
        pmem_write(12,TOGGLE_ADS_PMEM); // update the pmem value
        PMUpdate=TRUE; // let the value be saved to pmem
        ICUpdate=TRUE; // show the updated value in the gui without needing to close and reopen it
    }
 
    ICRefresh();    // This functions should be called on every iteration
        PMSave();         // This functions should be called on every iteration
 
// DEADZONE REMOVER
    if(abs(get_actual(STICK_1_X)) < StickNoise_PMEM) { set_val(STICK_1_X, 0.0); }
    if(abs(get_actual(STICK_1_Y)) < StickNoise_PMEM) { set_val(STICK_1_Y, 0.0); }
    if(abs(get_actual(STICK_2_X)) < StickNoise_PMEM) { set_val(STICK_2_X, 0.0); }
    if(abs(get_actual(STICK_2_Y)) < StickNoise_PMEM) { set_val(STICK_2_Y, 0.0); }
 
// ANTI-RECOIL
    if(TOGGLE_ADS_PMEM == 0){
    if (get_actual(BUTTON_8) && get_actual(BUTTON_5))
    {
        AntiRecoil(STICK_1_Y,RECOIL_V_PMEM);
        AntiRecoil(STICK_1_X,RECOIL_H_PMEM);
    }
    }
    if(TOGGLE_ADS_PMEM == 1){
    if (get_actual(BUTTON_5))
    {
        AntiRecoil(STICK_1_Y,RECOIL_V_PMEM);
        AntiRecoil(STICK_1_X,RECOIL_H_PMEM);
    }
    }
}
 
// update the UI, limit the update time to 1000ms (1 second) for safety
void ICRefresh() {
    static uint32 ICULast = 0;
    if (ICUpdate && ICULast < system_time())
    {
        printf("ic update");
        printf("GCMD:InteractiveConfiguration.Refresh");
        ICUpdate = FALSE;
        ICULast = system_time() + 1000;
    }
}
 
// Update the persistent memory, but not faster than 500ms (1/2 second)
void PMSave() {
    static uint32 PMSLast = 0;
    if (PMUpdate && PMSLast < system_time())
    {
        printf("StickNoise_PMEM: %.04f", StickNoise_PMEM);
        printf("RECOIL_V_PMEM: %.04f", RECOIL_V_PMEM);
        printf("RECOIL_H_PMEM: %.04f", RECOIL_H_PMEM);
        printf("TOGGLE_ADS_PMEM: %d", TOGGLE_ADS_PMEM);
        PMUpdate = FALSE;
        PMSLast = system_time() + 500;
        pmem_save();
    }
}
 
void AntiRecoil (uint8 axis, fix32 recoil)
{
     RY = get_actual(STICK_1_Y);
     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));
        }
    }
}
 
/* *****************************************************************************
<cfgdesc>
[Example]
shortdesc       = <<<MULTILINE
UI updateexample
with automatic UI refresh
MULTILINE
control         = info
 
[Sticknoise]
shortdesc       = The default value 4.32 should be a good start. If you want to tweak this, set it to the max value of your sticks resting position values. Use the "Device Monitor" to examine them.
byteoffset      = 0
bitsize         = 32
control            = spinboxf
default            = 4320000
minimum            = -100000000
maximum            = 100000000
step            = 500000
 
[Anti Recoil Vertical]
shortdesc       = Vertical recoil compensation
byteoffset      = 4
bitsize         = 32
control            = spinboxf
default            = 30000000
minimum            = -100000000
maximum            = 100000000
step            = 500000
 
[Anti Recoil Horizontal]
shortdesc       = Horizontal recoil compensation
byteoffset      = 8
bitsize         = 32
control            = spinboxf
default            = 00000000
minimum            = -100000000
maximum            = 100000000
step            = 500000
 
[Toggle ADS]
shortdesc       = Enable this if you would like Anti Recoil enabled while aiming down sight or during hip fire.
byteoffset      = 12
bitsize         = 1
bitoffset       = 7
control         = checkbox
default         = 1
item            = Enable
</cfgdesc>
***************************************************************************** */

 
bonefisher
Major General
Major General
 
Posts: 4258
Joined: Thu Jan 29, 2015 10:49 am

Re: working example of persistent variables for titan2?

Postby mpalpha » Sat Jul 14, 2018 4:20 pm

this one fixed the action toggle, but broke the ui checkbox toggle, but I managed to identify the issue between the two and fix it.


after rewrites, misc tweaks and adding led functions I ended up with this complete and now, working script.
Code: Select all
#pragma METAINFO("ANTIRECOIL, horizontal, vertical, adjust in-game, interactive config, autosave", 1, 02, "ETB")
 
// Frist of all, thanks to RedbeardCrunch, J2Kbr, Layman, Scachi, and Antithesis
 
// Anti Recoil
// Hold Both Triggers & Press DPAD UP, DOWN, LEFT, RIGHT to adjust.
fix32 RECOIL_V           = 30.0;    // Vertical Recoil  range -100.0 to 100.0
fix32 RECOIL_H           = 0.0;     // Horizontal Recoil  range -100.0 to 100.0
fix32 ADJUSTMENT_STEP    = 1.0;     // increment to adjust recoil per interation range 1.0 to 10.0
fix32 RY;                           // reserved
fix32 RX;                           // reserved
 
// ADS Toggle
// Hold Both Triggers & Press Right Stick (button)
uint8 TOGGLE_ADS         = TRUE;    // Toggle ADS Default
 
fix32 STICK_NOISE        = 4.32;    // Change this value to adjust the size of the Deadzone to ignore when using a Xim Apex.
                                    // in Gtuner IV > Device Monitor, flick the right stick and take note of the largest value.
 
bool PMUpdate            = FALSE;   // To save/write down the value to pmem set this flag to TRUE in the main loop.
 
// assign pmem locations (remember to match interactive config byteoffsets)
uint8 STICK_NOISE_PMEM   = 0;       // pmem slot
uint8 RECOIL_V_PMEM      = 4;       // pmem slot
uint8 RECOIL_H_PMEM      = 8;       // pmem slot
uint8 TOGGLE_ADS_PMEM    = 12;      // pmem slot
 
init {
    pmem_load(); // load the ui pmem
    pmem_read(STICK_NOISE_PMEM, &STICK_NOISE); // read the value to change from the ui
    pmem_read(RECOIL_V_PMEM, &RECOIL_V); // read the value to change from the ui
    pmem_read(RECOIL_H_PMEM, &RECOIL_H); // read the value to change from the ui
    pmem_read(TOGGLE_ADS_PMEM, &TOGGLE_ADS); // read the value to change from the ui
    STICK_NOISE = clamp(STICK_NOISE,0.0,255.0); // just to make sure the value is in the correct value range
    RECOIL_V = clamp(RECOIL_V,-100.0,100.0); // just to make sure the value is in the correct value range
    RECOIL_H = clamp(RECOIL_H,-100.0,100.0); // just to make sure the value is in the correct value range
    ADJUSTMENT_STEP = clamp(ADJUSTMENT_STEP,1.0,10.0); // just to make sure the value is in the correct value range
 
    printf("STICK_NOISE: %.04f", STICK_NOISE);
    printf("RECOIL_V: %.04f", RECOIL_V);
    printf("RECOIL_H: %.04f", RECOIL_H);
    printf("ADJUSTMENT_STEP: %.04f", ADJUSTMENT_STEP);
    printf("TOGGLE_ADS: %d", TOGGLE_ADS);
 
    // Light Blue led = ADS enabled, White led = ADS disabled
        set_light(TOGGLE_ADS? 'W': 'S');
}
 
main {
    // adjust anti-recoil
    if (is_active(BUTTON_5) && is_active(BUTTON_8)) { // Hold Both Triggers
        if ( event_active(BUTTON_10) ) { // ...and Press DPAD_UP
            if ( RECOIL_V > -100.0) {
                RECOIL_V = (RECOIL_V - ADJUSTMENT_STEP);
                pmem_write(RECOIL_V_PMEM,(fix32) RECOIL_V); // update the pmem value
                PMUpdate=TRUE; // pmem and gui value throttle
            }
        }
        if ( event_active(BUTTON_11) ) { // ...and Press DPAD_DOWN
            if ( RECOIL_V < 100.0) {
                RECOIL_V = (RECOIL_V + ADJUSTMENT_STEP);
                pmem_write(RECOIL_V_PMEM,(fix32) RECOIL_V); // update the pmem value
                PMUpdate=TRUE; // pmem and gui value throttle
            }
        }
        if ( event_active(BUTTON_12) ) { // ...Press DPAD_LEFT
            if ( RECOIL_H > -100.0) {
                RECOIL_H = (RECOIL_H - ADJUSTMENT_STEP);
                pmem_write(RECOIL_H_PMEM,(fix32) RECOIL_H); // update the pmem value
                PMUpdate=TRUE; // pmem and gui value throttle
            }
        }
        if ( event_active(BUTTON_13) ) { // ...and Press DPAD_RIGHT
            if ( RECOIL_H < 100.0) {
                RECOIL_H = (RECOIL_H + ADJUSTMENT_STEP);
                pmem_write(RECOIL_H_PMEM,(fix32) RECOIL_H); // update the pmem value
                PMUpdate=TRUE; // pmem and gui value throttle
            }
        }
        if ( event_active(BUTTON_6) ) { // ...and Press Right Stick
            TOGGLE_ADS = (!TOGGLE_ADS);
            pmem_write(TOGGLE_ADS_PMEM, TOGGLE_ADS); // toggle and update the pmem value
            PMUpdate=TRUE; // pmem and gui value throttle
        }
    }
    // save values when changed
    if (PMUpdate) {
        PMSave();         // This function should be called on every iteration if changes are detected.
    }
 
    // DEADZONE REMOVER
    if(abs(get_actual(STICK_1_X)) < STICK_NOISE) { set_val(STICK_1_X, 0.0); }
    if(abs(get_actual(STICK_1_Y)) < STICK_NOISE) { set_val(STICK_1_Y, 0.0); }
    if(abs(get_actual(STICK_2_X)) < STICK_NOISE) { set_val(STICK_2_X, 0.0); }
    if(abs(get_actual(STICK_2_Y)) < STICK_NOISE) { set_val(STICK_2_Y, 0.0); }
 
    // ANTI-RECOIL
    if(get_val(BUTTON_5) && (!TOGGLE_ADS || get_val(BUTTON_8)))
    {
        AntiRecoil(STICK_1_Y,RECOIL_V);
        AntiRecoil(STICK_1_X,RECOIL_H);
    }
}
 
// Update the persistent memory, but not faster than 1000ms (1 second)
void PMSave() {
    static uint32 PMSLast = 0;
    if (PMUpdate && PMSLast < system_time())
    {
        printf("STICK_NOISE: %.04f", STICK_NOISE);
        printf("RECOIL_V: %.04f", RECOIL_V);
        printf("RECOIL_H: %.04f", RECOIL_H);
        printf("ADJUSTMENT_STEP: %.04f", ADJUSTMENT_STEP);
        printf("TOGGLE_ADS: %d", TOGGLE_ADS);
        PMUpdate = FALSE;
        printf("ic update");
        printf("GCMD:InteractiveConfiguration.Refresh");
        PMSLast = system_time() + 1000;
        pmem_save();
 
        // Light Blue led = ADS enabled, White led = ADS disabled
        set_light(TOGGLE_ADS? 'W': 'S');
    }
}
 
void AntiRecoil (uint8 axis, fix32 recoil)
{
     RY = get_actual(STICK_1_Y);
     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));
        }
    }
}
 
void set_light(unsigned char new_light) {
  static unsigned char old_light;
  static uint32 timestamp;
  if(new_light != old_light && (system_time() - timestamp > 50)) {
    switch(new_light) {
      timestamp = system_time(), old_light = new_light;
      // PINK LED
      case 'P' :led_set(LED_1, 0.0, 0), led_set(LED_2, 0.0, 0), led_set(LED_3, 0.0, 0),led_set(LED_4, 5.0, 0); return;
      // BLUE LED
      case 'B': led_set(LED_1, 25.0, 0),led_set(LED_2, 0.0, 0), led_set(LED_3, 0.0, 0),led_set(LED_4, 0.0, 0); return;
      // SKY BLUE LED
      case 'S': led_set(LED_1, 50.0, 0),led_set(LED_2, 0.0, 0), led_set(LED_3, 50.0, 0),led_set(LED_4, 0.0, 0); return;
      // RED LED
      case 'R': led_set(LED_1, 0.0, 0),led_set(LED_2, 25.0, 0), led_set(LED_3, 0.0, 0),led_set(LED_4, 0.0, 0); return;
      // WHITE LED
      case 'W': led_set(LED_1, 25.0, 0), led_set(LED_2, 25.0, 0), led_set(LED_3, 25.0, 0), led_set(LED_4, 25.0, 0); return;
      // YELLOW LED
      case 'Y': led_set(LED_1, 0.0, 0),led_set(LED_2, 30.0, 0), led_set(LED_3, 10.0, 0), led_set(LED_4, 0.0, 0); return;
      // GREEN LED
      case 'G': led_set(LED_1, 0.0, 0), led_set(LED_2, 0.0, 0), led_set(LED_3, 25.0, 0), led_set(LED_4, 0.0, 0); return;
      // NO LED
      case 'N':led_set(LED_1, 0.0, 0),led_set(LED_2, 0.0, 0), led_set(LED_3, 0.0, 0), led_set(LED_4, 0.0, 0);
      }
  }
  return;
}
 
/***************
<cfgdesc>
[Universal Anti-Recoil]
shortdesc       = <<<MULTILINE
Horizontal: Hold both triggers + press left or right dpad
Vertical: Hold both triggers + press up or down dpad
Toggle ADS: Hold both triggers + press right stick button
 
Adjust in-game.
Interactive configuration.
Autosave.
MULTILINE
control         = info
 
[Sticknoise]
shortdesc       = <<<MULTILINE
The default value 4.32 should be a good start.
If you want to tweak this, set it to the max value of your sticks resting position values.
Use the "Device Monitor" to examine them.
MULTILINE
byteoffset      = 0
bitsize         = 32
control         = spinboxf
default         = 432
minimum         = 0
maximum         = 2000
step            = 1
decimals        = 2
 
[Anti Recoil Vertical]
shortdesc       = Vertical recoil compensation
byteoffset      = 4
bitsize         = 32
control         = spinboxf
default         = 300
minimum         = -1000
maximum         = 1000
step            = 1
decimals        = 1
 
[Anti Recoil Horizontal]
shortdesc       = Horizontal recoil compensation
byteoffset      = 8
bitsize         = 32
control         = spinboxf
default         = 0
minimum         = -1000
maximum         = 1000
step            = 1
decimals        = 1
 
[Toggle ADS]
shortdesc       = Enable this if you would like Anti Recoil enabled while aiming down sight or during hip fire.
byteoffset      = 12
bitsize         = 8
control         = checkbox
default         = 1
item            = Enable
 
</cfgdesc>
 
***************/

 
User avatar
mpalpha
Staff Sergeant
Staff Sergeant
 
Posts: 11
Joined: Mon Jun 11, 2018 11:02 pm


Return to GPC Script Programming (Titan Two)

Who is online

Users browsing this forum: Psebcool, zanvog and 6 guests