Please consider creating a 'ControlType' property within InputAction.

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. :frowning:

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:

// I need this:
if (inputAction.GetValueType() is float)
{
    /// We now know that .GetValue<TValue>() requires a float!
}

// Or this:
if (inputAction.ControlType == ControlType.Vector2)
{
    /// We now know that inputAction is of Vector2 type!
}

@Rene-Damm , I realize it’s probably wrong to tag you like this — but I really need this feature! :sweat_smile: (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.

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

if(inputAction.ReadValueAsObject().GetType() == typeof(Vector2))
{
         Debug.Log("Yes is vector 2");
}
1 Like

.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. :frowning:

why don’t you debug it and see for yourself its doesn’t return null to me

using UnityEngine;
    using UnityEngine.InputSystem;

    public class InputDebug : MonoBehaviour
    {
        public InputAction inputAction;

        void OnEnable()
        {
            inputAction.Enable();
        }

        void OnDisable()
        {
            inputAction.Disable();
        }

        private void Update()
        {
            if (inputAction.triggered)
            {
                if (inputAction.ReadValueAsObject().GetType() == typeof(Vector2))
                {
                    Debug.Log("Yes is vector 2");
                    Debug.Log(inputAction.ReadValueAsObject());
                }
            }
        }}

.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(). :frowning:

I need to know the value type of the InputAction regardless of it being ‘triggered’ or not.

1 Like

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

1 Like

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. :sweat_smile:

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

Regardless, I appreciate all of your suggestions, @Putcho ! :wink:

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.

1 Like

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.

1 Like