Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question InputAction.ApplyBindingOverride not functional for composites

Discussion in 'Input System' started by N_Evan, Aug 9, 2023.

  1. N_Evan

    N_Evan

    Joined:
    Sep 17, 2021
    Posts:
    6
    I've been trying to implement a complete Rebinding System with the new Input System. I am aware that there is a rebinding sample package provided with the core package as well as the popular tutorials available online to use it in various cases.

    Initially using the RebindAction class as is worked pretty fine for individual key rebinds as well as composite rebinds (individual composite part or all composite parts together). However, the issue arose when I tried to implement a swap feature when a new keybind already exists as a binding for any other action in the InputActionAsset. This too was solved after toiling quite a bit but I couldn't get the swap to function as I wanted when the duplicates were found within a composite keybind.

    So, I moved on to a more "simpler" approach to rebind the keys without using InteractiveRebinding while also swapping any duplicate keybinds (if any). To achieve this, I'm using InputAction.ApplybindingOverride on the binding index and action that I'm trying to remap.

    Code (CSharp):
    1. if (!ResolveActionAndBinding(out var action, out var bindingIndex))
    2.             return;
    3.  
    4. var rebindedInput = await CheckForInputs();
    5.  
    6. action.ApplyBindingOverride(bindingIndex, rebindedInput);
    rebindedInput is a string such as "<Keyboard>/a", which I get when I press any key during the rebinding. This works as expected from singular bindings of any actions but the function does not work when it is used on an individual binding inside a composite.

    ApplyBindingOverride does apply an override on the InputAction as it can be seen in Debug mode as well as if I save the BindingOverridesAsJson. However, when inside the actual gameplay scene, only the composite binding works only with the defaults keys instead of the rebinded ones.

    I tried to mention as much as I possibly could, if I left out anything noteworthy, will add them up promptly. Any pointers would be highly appreciated! Thanks.
     
  2. N_Evan

    N_Evan

    Joined:
    Sep 17, 2021
    Posts:
    6
    ***Update***

    For anyone stumbling across this post with somewhat similar situation. The issue occurred due to using generated C# class to read composite inputs.

    Previously, I was reading movement input like this,

    Code (CSharp):
    1. PlayerControls.GroundMovement.HorizontalMovement.ReadValue<Vector2>();
    PlayerControls is the generated C# class here. Updated this to cache InputAction from InputActionAsset through string search (which I still feel like can be improved somehow).

    Code (CSharp):
    1. playerInput.actions["HorizontalMovement"].ReadValue<Vector2>();
    Changing this makes the entire system work as intended.

    Note.
    As for why the singular bindings were working, those button presses were read through InputActionContext.