Search Unity

New input system: differentiate action coming from composite and normal

Discussion in 'Input System' started by jrmgx, Feb 9, 2021.

  1. jrmgx

    jrmgx

    Joined:
    Oct 21, 2016
    Posts:
    41
    Hello, I have a question about the new input system 1.1.0-prev3

    Given two actions ActionOne and ActionTwo
    • ActionOne is a button type with a bidding to Keyboard Y
    • ActionTwo is also a button type with a composite "One Modifier"
      binding to Keyboard Y + modifier Keybaord Shift
    I use Unity Events in my code,
    I have two unity events:

     - public void ActionOne(CallbackContext context)
    - public void ActionTwo(CallbackContext context)


    In play mode, when I push the Y key, I get a call to ActionOne method,
    but when I push Shift + Y keys, I get a call to both ActionOne and ActionTwo

    How can I get only the ActionTwo call for Shift + Y

    Am I missing something?
     
    bjornsyse likes this.
  2. jrmgx

    jrmgx

    Joined:
    Oct 21, 2016
    Posts:
    41
    That should be easy? anyone?

    Of course the goal is not to add a condition in ActionOne asking if ! shift pressed
     
  3. jrmgx

    jrmgx

    Joined:
    Oct 21, 2016
    Posts:
    41
    US based timezone up
     
  4. bjornsyse

    bjornsyse

    Joined:
    Mar 28, 2017
    Posts:
    102
    Also struggling with this question.
     
  5. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    ATM there's no support in there for having actions "preempt" each other's input (described here in docs). It's high on the wishlist of things to add.
     
  6. bjornsyse

    bjornsyse

    Joined:
    Mar 28, 2017
    Posts:
    102
    I see, thanks for prompt answer. In the meantime I ended up using the script provided by @_s_e_r_ in this thread (
    AxisModifierComposite 
    ) but modified it by adding this boolean flag to check for an Inverted state of the modifier key. That is, whether the state of the modifier key NOT being pressed should gate the action in question.

    Code (CSharp):
    1. public bool invertModifier;
    2.    
    3.     // This method computes the resulting input value of the composite based
    4.     // on the input from its part bindings but gated by the modifier key.
    5.     public override float ReadValue(ref InputBindingCompositeContext context)
    6.     {
    7.         float baseValue = base.ReadValue(ref context);
    8.        
    9.         if (!invertModifier)
    10.         {
    11.             if (context.ReadValueAsButton(modifier))
    12.                 return base.ReadValue(ref context);
    13.             return default;
    14.         }
    15.  
    16.         if (!context.ReadValueAsButton(modifier))
    17.             return base.ReadValue(ref context);
    18.         return default;
    19.     }
    20.