Adding actions at runtime to existing map from asset triggers assertion error

Do I understand correctly that it’s not possible to add actions at runtime to existing InputActionMap (from the
InputActionAsset)?
When I try to do that I get assertion error “Action count in old and new state must be the same”
Or it is possible and I’m missing something?
In internal code I also found similar assertion about action maps: “Map count in old and new state must be the same”
Another user reported similar issue with bindings but I couldn’t find such assertion in v1.0.0

@Rene-Damm , sorry to bother, but maybe you know?

An assertion getting triggered is a clear bug. Ticket would be appreciated, if you get a chance.

Mutating InputActionMaps and InputActionAssets on the fly is meant to work as expected. Judging from the assertion, it’s the re-resolution code acting up. One workaround would be something like this

// Instead of adding directly to the asset, clone it. The clone
// will end up with no internal resolution state and will thus
// have to resolve from scratch when the first action is enabled
// or has its bound control queried.
var clone = Object.Instantiate(actionAsset);

// Add action.
clone.FindActionMap("gameplay").AddAction("action");

// Swap clone in place of actionAsset and destroy
// actionAsset.

Unfortunately, there’s probably no better workaround.

So yeah, ticket is definitely appreciated. Needs a closer look.

1 Like

I created new project for bug report and found this: assertion error happens only when there are other enabled action maps in the asset. I know that we need to disable action map before we add an action to it, but looking at this assertion error I’m wondering if we need to disable all the maps? You said that “An assertion getting triggered is a clear bug.” so I finished my bug report.
Until it is fixed a temporary fix could be disabling logging before adding action and enabling it back afterwards, because this error doesn’t affect functionality - added action still works:

Debug.unityLogger.logEnabled = false;
actions.AddAction(actionName, actionType, actionBinding);
Debug.unityLogger.logEnabled = true;

Ah yup, that makes sense. So just disabling actions before actually is an easier workaround than what I suggested (or just living with the assert as in your code snippet).

Thank you :slight_smile: Much appreciated. And yup, definitely a bug.

1 Like

So the bug was fixed (1.1.0-pre.5), but now we get InvalidOperationException saying all the InputActionMaps in the asset must be disabled before adding our InputAction, why is that? =( It worked before, just with the assertion error.
As I see it now, one way of fixing this would be to write more code and remember which maps were enabled before adding new action and reenable them after adding action…