Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Input System' started by RunninglVlan, Oct 2, 2020.

  1. RunninglVlan

    RunninglVlan

    Joined:
    Nov 6, 2018
    Posts:
    180
    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
     
  2. RunninglVlan

    RunninglVlan

    Joined:
    Nov 6, 2018
    Posts:
    180
    @Rene-Damm, sorry to bother, but maybe you know?
     
  3. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    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

    Code (CSharp):
    1. // Instead of adding directly to the asset, clone it. The clone
    2. // will end up with no internal resolution state and will thus
    3. // have to resolve from scratch when the first action is enabled
    4. // or has its bound control queried.
    5. var clone = Object.Instantiate(actionAsset);
    6.  
    7. // Add action.
    8. clone.FindActionMap("gameplay").AddAction("action");
    9.  
    10. // Swap clone in place of actionAsset and destroy
    11. // actionAsset.
    Unfortunately, there's probably no better workaround.

    So yeah, ticket is definitely appreciated. Needs a closer look.
     
    RunninglVlan likes this.
  4. RunninglVlan

    RunninglVlan

    Joined:
    Nov 6, 2018
    Posts:
    180
    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:
    Code (CSharp):
    1. Debug.unityLogger.logEnabled = false;
    2. actions.AddAction(actionName, actionType, actionBinding);
    3. Debug.unityLogger.logEnabled = true;
     
  5. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    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 :) Much appreciated. And yup, definitely a bug.
     
    RunninglVlan likes this.
  6. RunninglVlan

    RunninglVlan

    Joined:
    Nov 6, 2018
    Posts:
    180
    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...