Search Unity

Hoe to check what methods have been added to .performed?

Discussion in 'Input System' started by Zaine7673, Apr 17, 2021.

  1. Zaine7673

    Zaine7673

    Joined:
    Feb 15, 2018
    Posts:
    238
    I seem to be having an issue with a script I've got and need to check if a method is successfully being added to .performed of an InputAction but all my googling has lead me nowhere. It has to be possible somehow right?

    Specifically, my issue is that I've set up a sort of quick time event thingy. Player holds button and after a set interval of time the button the player has to press changes. This all works fine but every now and then the button that has been assigned/set to be pressed doesn't work. I've set up a few Debug.Logs and it seems that the method is just not being assigned to the InputAction. I can't consistently re-create the issue so it's becoming a little difficult to figure out.

    Upon failure, the last log in the console is "ended" so it seems that run() is never called again when the correct button has been pressed although the method has been added to the button. I'm now trying to check if unsetButtons() is being called somewhere unexpectedly but i highly doubt it as it requires the slider value to be at 1 or above.

    Left the code below in-case someone can see something that I can't :)

    Code (CSharp):
    1.  
    2.         split = 1 / (float)numOfChange;
    3.         turns = 1f;
    4.  
    5.         void run(InputAction.CallbackContext ctx)
    6.         {
    7.             Debug.Log("started");
    8.            
    9.             if (runningQTE == false || currentStopped == true)
    10.             {
    11.                 Coroutine cor2 = StartCoroutine(_run(ctx));
    12.  
    13.                 if (cor != null)
    14.                 {
    15.                     StopCoroutine(cor);
    16.                 }
    17.  
    18.                 void unsetButtons()
    19.                 {
    20.                     button.performed -= run;
    21.                     turns = 1f;
    22.                     StopCoroutine(cor2);
    23.                 }
    24.  
    25.                 cor = StartCoroutine(decrease(decrement, interval, unsetButtons));
    26.  
    27.                 currentStopped = false;
    28.             }
    29.         }
    30.  
    31.         IEnumerator _run(InputAction.CallbackContext ctx)
    32.         {
    33.             while (slider.value < 1f)
    34.             {
    35.                 if (ctx.performed == true)
    36.                 {
    37.                     slider.value += increment;
    38.  
    39.                     if (slider.value >= split * turns)
    40.                     {
    41.                         button.performed -= run;
    42.                         InputAction tmp = button;
    43.                         buttons.Remove(button);
    44.                         button = buttons[UnityEngine.Random.Range(0, 2)];
    45.                         buttonIcon.sprite = userInput.getIcon(button);
    46.                         buttons.Add(tmp);
    47.                         button.performed += run;
    48.                         turns++;
    49.                         currentStopped = true;
    50.  
    51.                         Debug.Log("ended");
    52.  
    53.                         yield break;
    54.                     }
    55.                 }
    56.                 yield return new WaitForSeconds(interval);
    57.             }
    58.         }
     
  2. Zaine7673

    Zaine7673

    Joined:
    Feb 15, 2018
    Posts:
    238
    UPDATE: So I've noticed that it only seems to happen when Keyboard and Controller are both plugged in at the same time.
     
  3. Zaine7673

    Zaine7673

    Joined:
    Feb 15, 2018
    Posts:
    238
    UPDATE:

    Another pattern I have found is that when a button is repeated that's when it stops working. So if the player is asked to press E, SPACE, C, E it will stop working at the second E - even if I start the method again the E still does not work. Very strange behaviour.

    I thought maybe the buttons were somehow being removed from the list and for some reason it was not throwing an error but that wasn't the case as the list count always returns the correct number. and the button always returns a name after being assigned.

    Tested to see if unsetButtons() was being called randomly but it wasn't. Only called right at the end.

    I have tried changing a few things and running the code below with a "button.started += test;" right at the start of the method and I never remove it from that button. So the expected behaviour would be that the test method is executed every time I press that button regardless. I even put it in started instead of performed to further separate it from everything else. And yet, when the buttons stop working this also stops working. It's never removed so I'm slightly lost as to why it is behaving this way. iI's almost as if the entire action is being reset somehow.

    Would truly appreciate some input on this or even some suggestions as to what other tests I can try to narrow it down a little.

    Code (CSharp):
    1.  
    2.     Coroutine cor;
    3.     bool currentStopped = false;
    4.  
    5.     public void holdChange(type type, int numOfChange, float increment, float decrement, float interval, Action successMethod, Action failMethod, float width = 500f)
    6.     {
    7.         this.successMethod = successMethod;
    8.         this.failMethod = failMethod;
    9.         globalType = type;
    10.         playAudio(0);
    11.  
    12.         startQTE(0f, width);
    13.         List <InputAction> buttons = new List <InputAction> { Dreemlabs.UserInput.northHold, Dreemlabs.UserInput.southHold, Dreemlabs.UserInput.eastHold, Dreemlabs.UserInput.westHold };
    14.         InputAction button = buttons[UnityEngine.Random.Range(0, 3)];
    15.         button.performed += run;
    16.         buttonIcon.sprite = userInput.getIcon(button);
    17.  
    18.         split = 1 / (float)numOfChange;
    19.         turns = 1f;
    20.  
    21.         button.started += test;
    22.  
    23.         void run(InputAction.CallbackContext ctx)
    24.         {
    25.             if (runningQTE == false || currentStopped == true)
    26.             {
    27.                 StartCoroutine(_run(ctx));
    28.  
    29.                 if (cor != null)
    30.                 {
    31.                     StopCoroutine(cor);
    32.                 }
    33.  
    34.                 void unsetButtons()
    35.                 {
    36.                     button.performed -= run;
    37.                     turns = 1f;
    38.                 }
    39.  
    40.                 cor = StartCoroutine(decrease(decrement, interval, unsetButtons));
    41.  
    42.                 currentStopped = false;
    43.             }
    44.         }
    45.  
    46.         IEnumerator _run(InputAction.CallbackContext ctx)
    47.         {
    48.             while (slider.value < 1f)
    49.             {
    50.                 if (ctx.performed == true)
    51.                 {
    52.                     slider.value += increment;
    53.  
    54.                     if (slider.value >= split * turns)
    55.                     {
    56.                         button.performed -= run;
    57.  
    58.                         while (button.name == ctx.action.name)
    59.                         {
    60.                             button = buttons[UnityEngine.Random.Range(0, 3)];
    61.                         }
    62.  
    63.                         buttonIcon.sprite = userInput.getIcon(button);
    64.                         button.performed += run;
    65.  
    66.                         turns++;
    67.                         currentStopped = true;
    68.                         yield break;
    69.                     }
    70.                 }
    71.                 yield return new WaitForSeconds(interval);
    72.             }
    73.         }
    74.  
    75.         void test(InputAction.CallbackContext ctx)
    76.         {
    77.             Debug.Log(Time.deltaTime);
    78.         }
    79.     }