Search Unity

Unrelated struct affecting class when struct funcs called before class funcs in IF statement

Discussion in 'Scripting' started by scerial, Aug 2, 2022.

  1. scerial

    scerial

    Joined:
    Apr 9, 2013
    Posts:
    8
    Hello all,
    I have a very peculiar issue when calling a structs functions before a classes functions only in if statements.

    The debug statement of GetNeutralStick should be the value of the x axis of the control stick 60 frames ago, which most often should be zero, however, when accessed after any of the mStick functions in an if statement it always returns the current input direction, 1 for right, -1 for left.

    I could call GetNeutralStick first to workaround this issue but the meat of the function is iterative and id rather not check that every frame, also curious on how this is happening.

    To note the BufferFrame and the mStick values are both assigned from a separate input handler. The BufferFrame is created then the controlstick values are updated. The program runs sequentially so I don't there is any multiple access occurring.

    Thanks>.

    The offending if statement:

    Code (CSharp):
    1. if ((character.mStick.IsValid(0.5f, 1f, 135, 225) || character.mStick.IsValid(0.5f, 1f, 45, -45)) && character.pip.buffer.GetNeutralStick(1, InputContainer.Axis.LEFT_HORIZONTAL, InputContainer.Axis.LEFT_VERTICAL))
    2.         {
    3.             character.SwitchState(character.stateDash);
    4.             return;
    5.         }
    The struct controlStick (mStick)::

    Code (CSharp):
    1. public struct controlStick
    2. {
    3.     public Vector2 values;
    4.     public float Magnitude()
    5.     {
    6.         return Mathf.Sqrt(values.x * values.x + values.y * values.y);
    7.     }
    8.     public float Angle()
    9.     {
    10.         if(values.y >= 0)
    11.         {
    12.             return Mathf.Atan2(values.y, values.x) * Mathf.Rad2Deg;
    13.         }
    14.         else
    15.         {
    16.             return 360 + (Mathf.Atan2(values.y, values.x) * Mathf.Rad2Deg);
    17.         }
    18.     }
    19.     public bool IsBetween(int angleL, int angleH)
    20.     {
    21.         if(angleH < 0)
    22.         {
    23.             return (Angle() <= angleL || Angle() >= (360 + angleH));
    24.         }
    25.         else
    26.         {
    27.             return (Angle() >= angleL && Angle() <= angleH);
    28.         }
    29.        
    30.     }
    31.     public bool IsValid(float magnitudeL, float magnitudeH, int angleL, int angleH)
    32.     {
    33.         return (IsBetween(angleL, angleH) && (Magnitude() <= magnitudeH && Magnitude() >= magnitudeL));
    34.     }
    35.  
    36. }
    And lastly the function being called in the separate class as well as the relevant variable:
    Code (CSharp):
    1. public class InputBuffer
    2. {
    3.     private int maxBufferLength = 60;
    4.     private List<BufferFrame> Buffer;
    5.     //Last frame in list is most recent;
    6.     public InputBuffer()
    7.     {
    8.        Buffer = new List<BufferFrame>();
    9.     }
    10.     public bool GetNeutralStick(int depth, InputContainer.Axis axis1, InputContainer.Axis axis2)
    11.     {
    12.         Debug.Log("Axis Value:: " + Buffer[0].GetAxisValue(InputContainer.Axis.LEFT_HORIZONTAL));
    13.         return true;
    14.     }
    15. }
    16.  
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,856
    Probably because your if statement is short circuiting if true at the beginning, and not actually evaluating any of the following methods.
     
  3. scerial

    scerial

    Joined:
    Apr 9, 2013
    Posts:
    8
    It is evaluating the function otherwise the debug message would not show up, however I think I am suffering a reference problem where the specific BufferFrame is always referencing the current state instead of a stored value.

    Still unsure why the mStick reference would be changing that value from 0 to 1/-1.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,856
    I'm going to go out on a limb and say it's probably not. There's probably something else going on here you haven't tracked down.
     
  5. scerial

    scerial

    Joined:
    Apr 9, 2013
    Posts:
    8
    Figured it out, turns out my use of a class to store the axis data instead of a simpler structure was a bad choice. The bufferFrame contains an array of InputAxis where each inputaxis was being copied as a reference from the input handler when the BufferFrame was created.
    Thank you! Sorry for the misleading question lol.