Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Saving and loading created Actions

Discussion in 'Input System' started by P3YC1, Apr 5, 2021.

  1. P3YC1

    P3YC1

    Joined:
    Mar 11, 2020
    Posts:
    6
    Hello everyone.
    So I've noticed that it is possible to create actions using the script, but I have not found a way of saving and preserving them.
    I know that I can save changed Bindings using "SaveBindingOverridesAsJson()" but so far I have not found a way of saving the actions and assigned bindings I've created during runtime.
    I know that I can make Maps and Assets to and from Json, but if I do so, I seem to be unable to use them with the Auto Generated C# Class and Input System I've created.
     
  2. P3YC1

    P3YC1

    Joined:
    Mar 11, 2020
    Posts:
    6
    So when I try to use the
    Code (CSharp):
    1. (Input System's generated C# Class object).asset.LoadFromJson(myFile)
    function, then I get the following error when enabling the ActionMap:
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. UnityEngine.InputSystem.InputActionMap.ResolveBindings () (at Library/PackageCache/com.unity.inputsystem@1.1.0-preview.3/InputSystem/Actions/InputActionMap.cs:1146)
    3. UnityEngine.InputSystem.InputActionMap.ResolveBindingsIfNecessary () (at Library/PackageCache/com.unity.inputsystem@1.1.0-preview.3/InputSystem/Actions/InputActionMap.cs:1026)
    4. UnityEngine.InputSystem.InputActionMap.Enable () (at Library/PackageCache/com.unity.inputsystem@1.1.0-preview.3/InputSystem/Actions/InputActionMap.cs:546)
    5. InputControler+InGameScreenActions.Enable () (at Assets/InputControler.cs:287)
    6. Assets.InputHandler.OnEnable () (at Assets/InputHandler.cs:40)
     
  3. P3YC1

    P3YC1

    Joined:
    Mar 11, 2020
    Posts:
    6
    Okay, so on my way of finding a solution I tried to abandon the generated Input System Class, and just use a created Action map for what I need. This seems to somewhat work, but I feel like I'm encountering a strange bug. It seems like that
    myActionMap.FindAction(skillName).bindings[0]
    and
    _skillMap.ToJson()
    have different values stored for the bindings after the remapping. The following code is part of the function that gets called in
    .OnComplete(...)


    Code (CSharp):
    1. Debug.Log("Actual Binding for " + skillName + ": " + _skillMap.FindAction(skillName).bindings[0]);
    2. Debug.Log("Amount of Bindings for " + skillName + ": " + _skillMap.FindAction(skillName).bindings.Count);
    3. Debug.Log("Action Map that contains " + skillName + ": " + _skillMap.ToJson());
    The output unity gives me is the following:
    Code (CSharp):
    1. Actual Binding for Force of Fire: Force of Fire:<Keyboard>/b
    Code (CSharp):
    1. Amount of Bindings for Force of Fire: 1
    Code (CSharp):
    1. {
    2.     "maps": [
    3.         {
    4.             "name": "Skill Map",
    5.             "id": "...",
    6.             "actions": [
    7.                 ...,
    8.                 {
    9.                     "name": "Force of Fire",
    10.                     "type": "Value",
    11.                     "id": "88dc203e-9346-4015-af53-48406102af70",
    12.                     "expectedControlType": "",
    13.                     "processors": "",
    14.                     "interactions": ""
    15.                 },
    16.                 ...
    17.             ],
    18.             "bindings": [
    19.                 ...,
    20.                 {
    21.                     "name": "",
    22.                     "id": "709796b4-c10b-4e04-a2df-5dd1f8c44fe8",
    23.                     "path": "<Keyboard>/k",
    24.                     "interactions": "",
    25.                     "processors": "",
    26.                     "groups": "",
    27.                     "action": "Force of Fire",
    28.                     "isComposite": false,
    29.                     "isPartOfComposite": false
    30.                 },
    31.                 ...
    32.             ]
    33.         }
    34.     ]
    35. }
    (I've replaced part's that should be unimportant with "...")
    Is this supposed to behave that way? And if so, what am I doing wrong/what should I change?