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

Feedback Please consider creating a 'ControlType' property within InputAction.

Discussion in 'Input System' started by NotaNaN, Mar 10, 2021.

  1. NotaNaN

    NotaNaN

    Joined:
    Dec 14, 2018
    Posts:
    324
    After digging through documentation for hours and attempting to jury-rig a way to determine a given InputAction's value type without that InputAction being triggered, I have pretty much given up and resorted to the forums. :(

    Basically, I want to know the value type (be it a float, Vector2, Vector3, integer, etc) that would be returned from .ReadValue(), explicitly, or in enum-form (I.E. the InputActionAsset editor's 'ControlType' property), so that way I can know the value type of a given InputAction beforehand (for my own purposes).

    I tried finding a way to get this information myself... but everything having to do with .ReadValue() is internal.


    To show you what I need in psuedocode:
    Code (CSharp):
    1. // I need this:
    2. if (inputAction.GetValueType() is float)
    3. {
    4.     /// We now know that .GetValue<TValue>() requires a float!
    5. }
    6.  
    7. // Or this:
    8. if (inputAction.ControlType == ControlType.Vector2)
    9. {
    10.     /// We now know that inputAction is of Vector2 type!
    11. }

    @Rene-Damm , I realize it's probably wrong to tag you like this — but I really need this feature! :oops: (well technically I can live without it, but I want it).

    EDIT: Now mention that I need to know the value type of the InputAction regardless of the InputAction being triggered or not.
     
    Last edited: Mar 11, 2021
  2. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246

    Isn't this what you're looking for? .ReadValueAsObject().GetType()


    Code (CSharp):
    1. if(inputAction.ReadValueAsObject().GetType() == typeof(Vector2))
    2. {
    3.          Debug.Log("Yes is vector 2");
    4. }
     
    Last edited: Mar 11, 2021
    NotaNaN likes this.
  3. NotaNaN

    NotaNaN

    Joined:
    Dec 14, 2018
    Posts:
    324
    .ReadValueAsObject() returns null, rather than the type, if the InputAction in question is currently at it's default value. (I.E. if the InputAction would return Vector2, if the value of that InputAction was 0,0 — null would be returned instead of 0,0).
    (Correct me if I'm wrong here).

    I need to know the value type of the InputAction regardless of it being 'actuated' or not. :(
     
  4. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    why don't you debug it and see for yourself its doesn't return null to me



    Code (CSharp):
    1. using UnityEngine;
    2.     using UnityEngine.InputSystem;
    3.  
    4.     public class InputDebug : MonoBehaviour
    5.     {
    6.         public InputAction inputAction;
    7.  
    8.         void OnEnable()
    9.         {
    10.             inputAction.Enable();
    11.         }
    12.  
    13.         void OnDisable()
    14.         {
    15.             inputAction.Disable();
    16.         }
    17.  
    18.         private void Update()
    19.         {
    20.             if (inputAction.triggered)
    21.             {
    22.                 if (inputAction.ReadValueAsObject().GetType() == typeof(Vector2))
    23.                 {
    24.                     Debug.Log("Yes is vector 2");
    25.                     Debug.Log(inputAction.ReadValueAsObject());
    26.                 }
    27.             }
    28.         }}
     
  5. NotaNaN

    NotaNaN

    Joined:
    Dec 14, 2018
    Posts:
    324
    .ReadValueAsObject() is only safe to perform if the InputAction is triggered. If the InputAction is not triggered, .ReadValueAsObject() returns null, preventing the use of .GetType(). :(

    I need to know the value type of the InputAction regardless of it being 'triggered' or not.
     
    Putcho likes this.
  6. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    if it was me then i will fine my way around to working on that now that I know where to looking at get the type
     
    NotaNaN likes this.
  7. NotaNaN

    NotaNaN

    Joined:
    Dec 14, 2018
    Posts:
    324
    Yeah, I agree. That's probably the best course of action.

    Luckily, I am able to perform an extra authoring step (on my side) to define what type of input would be received inside the editor... but it's kinda janky and makes things unnecessarily complex. :oops:

    I can live without the feature for the time being (but I still want it as a niche QoL). :p

    Regardless, I appreciate all of your suggestions, @Putcho! ;)
     
    Last edited: Mar 11, 2021
  8. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    you know the real problem is? the Input Action only send out the value when Device is active, we can't get any type because the value be null in the first place and that is by design I thing. we only beable to read type if the vale be send from the device, that what am getting at.
     
    NotaNaN likes this.
  9. IsometricBacon

    IsometricBacon

    Joined:
    Sep 28, 2017
    Posts:
    29
    I ran into this same problem, and would love a way to interrogate the context to determine what it is coming back as in a device agnostic way.

    i.e. in my application I want to have a input map for 'zoom camera' but this could be a scroll button, holding one button down and moving a mouse, dragging a finger across a touchscreen, pushing forward on a xbox controller, etc, etc. Ideally, it could (and should) be any input or device that the developer wants to remap it to.

    I could say "if device type is this" in my code - but I thought the whole point of this InputSystem was to make code device agnostic - which is why I'd much prefer to have it based on the actual value types being passed through.

    To add to some of the above discussions - the docs warn against using ReadValueAsObject() as "it will allocate Garbage Collection heap memory and lead to frame rate instabilities."

    I managed to get around it in my project in an overlay complex way, which basically involved another InputAction setting that would trigger a boolean of whether I'm zooming using a pointer device (i.e. in which i'd want to zoom into the Vector2 position of the mouse cursor / touchscreen pressed position), or instead zoom into the centre of the screen when using keyboards, joysticks, gamepads etc that don't have a pointer). But this only works in my hyperspecific use case.
     
    NotaNaN likes this.