Hey forum,
using Unity 2021.3.18f1
using Input System 1.5.1
I have an issue with rebinding composite actions. I have a custom script where I copied the interesting parts from the the rebinding sample provided by Unity.
My script has these two methods:
private void StartRebindOperation()
{
if (!ResolveActionAndBinding(out int bindingIndex))
{
return;
}
// If the binding is a composite, we need to rebind each part in turn.
if (actionToRebind.bindings[bindingIndex].isComposite)
{
int firstPartIndex = bindingIndex + 1;
if (firstPartIndex < actionToRebind.bindings.Count && actionToRebind.bindings[firstPartIndex].isPartOfComposite)
{
PerformBinding(actionToRebind, firstPartIndex, true);
}
}
else
{
PerformBinding(actionToRebind, bindingIndex);
}
}
public bool ResolveActionAndBinding(out int bindingIndex)
{
bindingIndex = -1;
if (actionToRebind == null)
{
return false;
}
if (string.IsNullOrEmpty(actionBindingId))
{
return false;
}
// Look up binding index.
Guid bindingId = new(actionBindingId);
bindingIndex = actionToRebind.bindings.IndexOf(x => x.id == bindingId);
if (bindingIndex == -1)
{
Log.Error($"Cannot find binding with ID '{bindingId}' on '{actionToRebind}'", this);
return false;
}
return true;
}
In my test case I want to rebind the “PrimaryMovement” Action, the bindings look like this:
Now the rebinding doesnt work at all, so I started the debugger and monitored some values and the issue is the binding index apparently.
When ResolveActionAndBinding() is called in StartRebindingOperation, this line here:
bindingIndex = actionToRebind.bindings.IndexOf(x => x.id == bindingId);
returns an int of 11, so the bindinIndex becomes 11.
And then StartRebindingOperation goes on with this bindingIndex and in this line here:
if (actionToRebind.bindings[bindingIndex].isComposite)
returns false, since that binding with this index is not a composite, but apparently it seems to be part of a composite.
These are all the bindings by index when looking into them via the debugger:
Now when I inspect the bindings of the actionToRebind and looked into the 11th index and to what it belongs it seems the binding index I would need is 10, since this binding’s composite bool value is true, but for some reason I do get 11:
I am not sure if this is maybe a tiny bug here? Or if my setup is maybe wrong?
And while I am here, another question would be: As you can see in the binding screenshot above I have two mappings for both the Keyboard and the Gamepad?
How would I need to adjust the code in order to be able to rebind BOTH variations, the binding with the left stick and the binding with the dpad for example?