Search Unity

Question Proper way to clear InputSystem binding?

Discussion in 'Editor & General Support' started by Punfish, Aug 2, 2022.

  1. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    401
    The only method I see to clear an InputBinding is `inputAction.ChangeBinding(x).Erase()`.
    I'm calling Erase on the IsComposite, and IsPartOfComposite parts. Then I am re-adding empty ones with

    Code (CSharp):
    1.             void AddCompositeBinding()
    2.             {
    3.                 InputBinding ib = new InputBinding() { isComposite = true, isPartOfComposite = false };
    4.                 ia.AddBinding(ib);
    5.             }
    6.             void AddPartOfCompositeBinding()
    7.             {
    8.                 InputBinding ib = new InputBinding() { isComposite = false, isPartOfComposite = true };
    9.                 ia.AddBinding(ib);
    10.             }
    11.  
    This however throws
    Code (CSharp):
    1. ArgumentNullException: Value cannot be null.
    2. Parameter name: text
    3. UnityEngine.InputSystem.Utilities.NameAndParameters.Parse (System.String text) (at Library/PackageCache/com.unity.inputsystem@1.4.1/InputSystem/Utilities/NameAndParameters.cs:71)
    when attempting to call `inputAction.PerformInteractiveRebinding(x)`.

    If the default modifier + binding is 'no binding' in the inputMap it works fine. But the moment you clear a binding you cannot rebind it.
     
    Last edited: Aug 2, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,741
    Are you sure this isn't just a nullref?

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null
    - Identify why it is null
    - Fix that

    If it's not, then I would start with the docs for binding... if there is unbinding I imagine that would be linked pretty nearby in the docs.
     
  3. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    401
    This is the wrong answer. Please no one take this advice.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,741
    I'm sure that YOU are now going to post the correct answer, right?

    We've all been waiting for you to actually add value to the forum.
     
  5. WinterboltGames

    WinterboltGames

    Joined:
    Jul 27, 2016
    Posts:
    259
    It is quite simple. Here is an example:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3.  
    4. public sealed class InputRebindingExample : MonoBehaviour
    5. {
    6.     [SerializeField]
    7.     private InputAction inputAction;
    8.  
    9.     private void Start()
    10.     {
    11.         inputAction.Enable();
    12.  
    13.         inputAction.performed += callbackContext =>
    14.         {
    15.             Debug.Log("Hello, World!");
    16.         };
    17.     }
    18.  
    19.     private void Update()
    20.     {
    21.         if (Keyboard.current.eKey.wasPressedThisFrame)
    22.         {
    23.             Debug.Log("Erasing...");
    24.  
    25.             inputAction.ApplyBindingOverride(1, "");
    26.             inputAction.ApplyBindingOverride(2, "");
    27.         }
    28.  
    29.         if (Keyboard.current.bKey.wasPressedThisFrame)
    30.         {
    31.             Debug.Log("Binding...");
    32.  
    33.             inputAction.ApplyBindingOverride(1, Keyboard.current.ctrlKey.path);
    34.             inputAction.ApplyBindingOverride(2, Keyboard.current.spaceKey.path);
    35.         }
    36.  
    37.         if (Keyboard.current.rKey.wasPressedThisFrame)
    38.         {
    39.             Debug.Log("Rebinding...");
    40.  
    41.             inputAction.Disable();
    42.  
    43.             InputActionRebindingExtensions.RebindingOperation rebindingOperation = inputAction.PerformInteractiveRebinding(2);
    44.  
    45.             rebindingOperation.OnComplete(rebindingOperation =>
    46.             {
    47.                 inputAction.Enable();
    48.  
    49.                 rebindingOperation.Dispose();
    50.             });
    51.  
    52.             rebindingOperation.Start();
    53.         }
    54.     }
    55. }
     
  6. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    401
    baize97 and WinterboltGames like this.
  7. Sake906

    Sake906

    Joined:
    Mar 27, 2014
    Posts:
    45
    This whole InputSystem is sincerely so bad at the most basic things I'm starting to consider just going back to the old one and programming my own binding system. Something as trivial as clearing a key binding should NOT force me through dozens of google search entries leading to hazy articles like this one.

    To top it with more nonsense, I just found out that using ".WithControlsExcluding("<keyboard>/escape")" also affects the "e" key, and we are yet to receive an update for that.

    Horrendously abysmal. This ...THING of system should've never come out of alpha in this state.
     
  8. baize97

    baize97

    Joined:
    Jun 23, 2020
    Posts:
    30
  9. valentin56610

    valentin56610

    Joined:
    Jan 22, 2019
    Posts:
    156
    What if I want to reset all rebinds at once?
     
  10. Then you use the proper method depending on you want to reset an action's bindings or the action map's bindings. Either way, the RemoveAllBindingOverrides method is in the documentation. As always. Also please next time open your own threads and post your questions separately.
     
    valentin56610 and Kurt-Dekker like this.