- Home /
Input System binding with one modifier composite value problem
So I'm using the Input System Package (version 1.1.0 preview 2). Here generalised bindings with one (or two) modifier have been added (from now on I'mma call it BWOM for short), and I've having problems getting the value of the action.
Using the Input Action Asset editor I've set up an Action called Peer (Vector2) and set one of the bindings to be a BWOM. The specific bindings do not really matter, but if you want to know I've set the modifier to be the gamepad right stick press, and the binding itself as the gamepad right stick.
Then, I've created a Player gameobject with the PlayerInput component, and set the behaviour to"Send Messagges" for semplicity sake. In a new script I've created to receive the inputs I have the method OnPeer that receives the input value.
If the imputs are given throug any kind of binding that's not a BWOM, everything's fine with this line of code:
Vector2 peer; //Variable
void OnPeer(InputValue val) => peer = val.Get<Vector2>(); //Method
But for some reason, when using the gamepad right stick (with BWOM binding) it throws a NullReferenceException. I've done some testing and found out that NullReferenceException is thrown whenever the TValue you give is not the actual type of the value the system is expecting.
It was wierd to me, I even tried using Debug.Log(val.Get());
and the values is gave me actually were Vector2 (in fact val.Get().GetType()
gives Vector2), except when the modifier is released, which resulted in a null value for some reason. So I tried out other value types, not really expecting any kind of results; I tried using , and and it always resulted in a NullReferenceException except when the modifier was released, wich resulted in false or a 0, which is interesting, because it means that the Get() method outputs an inconsistent value.
My question is, is it all just a bug of the package which is still in preview? Am I missing something, like maybe the fact that there is a specific TValue type I must use when creating BWOMs which act as a wrapper? Or am i forced to use another behaviour (such as "Invoke UnityEvents", which would give me a CallbackContext variable)?
I assume I could use the Get() method and then cast the value, after checking it to be non null, but I read on the documentation that it's not safe to use it as it would lead to GC spikes, so I would prefer not to use it, if not really necessary.
Answer by Nargleflex · Jan 31, 2021 at 03:47 AM
I definitely think this is a bug in the preview version, I tried just about every combination of settings to get this action to read a Vector2 from the mouse's position but kept getting this same null reference exception. Currently, my bootleg workaround seems to do the job:
void Awake()
{
Controls = new InputManager();
Controls.Player.Aim.performed += ctx => Look((Vector2)ctx.ReadValueAsObject());
Controls.Player.Aim.canceled += _ => Look(Vector2.zero);
}
private void Look(Vector2 lookVector)
{
// Mess with lookVector here
}
Thanks for your answer, but i ended up with this:
void OnPeer(InputValue val) => peer = val.Get() as Vector2? ?? Vector2.zero;
I know yours is probablu better because of GC spikes, but now that I've already done it I'm not going to change my implementation as long as it works.
Thanks anyways
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Using Input.mouseposition to instantiate 1 Answer
Input.GetTouch(0).position.x and TouchPhase.Began 1 Answer
Unity, Property Drawers & GetPropertyHeight() not working 0 Answers