Snapback protection for platform fighters on Nintendo Switch

Script for Xbox One controllers to prevent snapback on the left stick. Useful if you're encountering unintentional dashing or B-reverses on the Xbone controller.
Version1.0
AuthorRobo_Leader
Publish DateSun, 6 Jan 2019 - 00:06
Last UpdateSun, 6 Jan 2019 - 00:06
Downloads2
RATE


1

0

Release Notes: Initial Upload
Code: Select all
//Craig! Twitter: @ROBO_LEADER
//Snapback protection for platform fighters
//1/5/2019
//version 1
 
//two updates back
int wasRight2 = FALSE;
int wasLeft2 = FALSE;
int wasAtRest2 = FALSE;
//one update back
int wasRight1 = FALSE;
int wasLeft1 = FALSE;
int wasAtRest = FALSE;
//now
int isRight = FALSE;
int isLeft = FALSE;
int currentPosition = 0;
int atRest = FALSE;
//Deadzones so that slow stick movement won't be dropped
int deadzoneRight = 32;
int deadzoneLeft = -32;
 
//if past this point, do nothing
int unsignedCurrentPosition = 0;
int intentional = 75;
 
//main is called every time the console polls the device
//for Switch, this is every 8ms, so twice a frame.
main {
 
    //Find out where we are now
    currentPosition = get_val(XB1_LX);
    unsignedCurrentPosition = abs(currentPosition);
 
    //check if the stick is in the deadzone
    if (currentPosition < deadzoneRight && currentPosition > deadzoneLeft){
            atRest = TRUE;
            isRight = FALSE;
            isLeft = FALSE;
            set_val(TRACE_5, 0);
            set_val(TRACE_6, 0)
    }
    //Stick isn't in the deadzone! What direction is it in?
    else {
    atRest = FALSE;
        if (currentPosition > deadzoneRight){
            isRight = TRUE;
            set_val(TRACE_6, 100);
        }
        else{
            isLeft = TRUE;
            set_val(TRACE_5, 100);
        }
    }
 
    if (!atRest){
       set_val(TRACE_1, 0);
       //did the stick return to neutral?
       if (wasAtRest || wasAtRest2){
        //are we now holding in the opposite direction?
        if (wasRight2 || wasRight1){
            if (isLeft){
                //snapback?
                if (unsignedCurrentPosition < intentional) set_val(XB1_LX, 0);
                }
            }
 
        if (wasLeft2 || wasLeft1){
            if (isRight){
                //snapback?
                if (unsignedCurrentPosition < intentional) set_val(XB1_LX, 0);
                }
            }
        }
    }
    //for when the stick actually passed a neutral position between updates
    if (isRight && wasLeft1 && unsignedCurrentPosition < intentional) set_val(XB1_LX, 0);
    if (isLeft && wasRight1 && unsignedCurrentPosition < intentional) set_val(XB1_LX, 0);
 
    //Lastly, since this update is about to complete, record the previous position
    set_val(TRACE_1, currentPosition);
    wasAtRest2 = wasAtRest;
    wasAtRest = atRest;
    wasLeft2 = wasLeft1;
    wasRight2 = wasRight1;
    wasLeft1 = isLeft;
    wasRight1 = isRight;
}