Search Unity

Can a Vector2 value be read from OneModifierComposite?

Discussion in 'Input System' started by goncalo-vasconcelos, Nov 26, 2020.

  1. goncalo-vasconcelos

    goncalo-vasconcelos

    Joined:
    May 24, 2017
    Posts:
    8
    The documentation for OneModifierComposite has this example binding a look action:

    Code (CSharp):
    1. lookAction.AddCompositeBinding("OneModifier")
    2.     .With("Modifier", "<Keyboard>/alt")
    3.     .With("Binding", "<Mouse>/delta")
    However, trying to poll the action directly for a value

    Code (CSharp):
    1. var look = lookAction.ReadValue<Vector2>()
    Throws a NullReferenceException

    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. UnityEngine.InputSystem.InputActionState.ReadValue[TValue] (System.Int32 bindingIndex, System.Int32 controlIndex, System.Boolean ignoreComposites) (at Packages/com.unity.inputsystem@1.1.0-preview.2/InputSystem/Actions/InputActionState.cs:2248)
    3. UnityEngine.InputSystem.InputAction.ReadValue[TValue] () (at Packages/com.unity.inputsystem@1.1.0-preview.2/InputSystem/Actions/InputAction.cs:920)
    How then should a Vector2 value be read from the OneModifierComposite?
    Or, is the look action example misleading and should we use a custom composite instead?
    https://forum.unity.com/threads/how-to-create-this-aim-action-see-inside.751865/#post-5009159
     
    bjornsyse likes this.
  2. Nargleflex

    Nargleflex

    Joined:
    Sep 6, 2019
    Posts:
    1
    I'm running into this same issue right now, I would figure the modifier just acts as a gate & the binding is what controls the type returned. In my case I've got Position as the binding, but can't seem to read a Vector2 from it
     
  3. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Indeed a bug. Reading the value through
    InputAction.ReadValue()
    does the right thing but reading it through
    InputAction.CallbackContext.ReadValue()
    in a callback throws. Fixed here.
     
    goncalo-vasconcelos likes this.
  4. bjornsyse

    bjornsyse

    Joined:
    Mar 28, 2017
    Posts:
    102
    when will this fix be released via the package manager update? Is there a work-around to get values from a composite input in the meantime?
     
  5. bjornsyse

    bjornsyse

    Joined:
    Mar 28, 2017
    Posts:
    102
    Solved this by using ReadValueAsObject on the composite with modifier. Returned Null if no modifier was engaged and the Vector2 if it was.