Search Unity

Resolved Example code for InputAction.GetBindingDisplayString that returns controlPath doesn't compile

Discussion in 'Input System' started by RunninglVlan, Oct 16, 2020.

  1. RunninglVlan

    RunninglVlan

    Joined:
    Nov 6, 2018
    Posts:
    182
    I want to know how to get controlPath of an InputAction, but example code from documentation doesn't work.
    Code (CSharp):
    1. var action = new InputAction();
    2.  
    3. action.AddBinding("<Gamepad>/dpad/up", groups: "Gamepad");
    4.  
    5. // Prints "A", then "Gamepad", then "dpad/up".
    6. Debug.Log(action.GetBindingDisplayString(InputBinding.MaskByGroup("Gamepad", out var deviceLayoutNameA, out var controlPathA));
    7. Debug.Log(deviceLayoutNameA);
    8. Debug.Log(controlPathA);
    First of all there's missing parenthesis, second - MaskByGroup returns InputBinding, but we need int bindingIndex here instead.
     
  2. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Indeed. Will push a fix for the docs.

    You can look up the binding index using
    GetBindingIndex
    and then pass it to
    GetBindingDisplayString
    .

    Code (CSharp):
    1. var bindingIndex = action.GetBindingIndex(group: "Gamepad");
    2. var displayString = action.GetBindingDisplayString(bindingIndex, out var _, out var controlPath);
     
    RunninglVlan likes this.
  3. EMOTION-THEORY

    EMOTION-THEORY

    Joined:
    Jul 16, 2013
    Posts:
    83
    action.GetBindingDisplayString(); 

    gives the correct text, it works well. As I switch between keyboard and gamepad, I get the correct text. (for eg. SPACE / A)

    However, I cannot use
    action.GetBindingDisplayString(out var deviceLayoutName, out var controlPath);
    .
    This override seems not to exist.

    This one gives strange results
    action.GetBindingDisplayString(action.GetBindingIndex(), out var deviceLayoutName, out var controlPath);

    Rather than switching from keyboard to gamepad dynamically, it just switches from the short name to the long name (eg. Shift becomes Left Shift instead of LB for gamepad).

    How do I figure out what device/path my current action is on based on the current scheme?
     
  4. David_Faulkner

    David_Faulkner

    Joined:
    Oct 5, 2013
    Posts:
    33