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

How to reset action states?

Discussion in 'Input System' started by philippelabalette, Dec 8, 2019.

  1. philippelabalette

    philippelabalette

    Joined:
    Mar 23, 2019
    Posts:
    14
    Hi,

    I encounter the following problem in my project.
    Depending on the states of the games, action maps should be activated/deactivated. Everything works fine but If an action triggers the deactivation of its own map and this map is reactivated later in the fame, the action is like stuck in an undefined state, so I need to push 2 times the button in order to trigger the action.

    I made a simple example to illustrate this. It's a very simple 2 states management system. The states can be switched by pushing buttons on the gamepad.

    1) I first created a new action map asset with 2 maps (map1 and map2)
    upload_2019-12-8_7-19-16.png

    upload_2019-12-8_7-20-26.png

    2) Then I use this only script to manage the states in the game:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3.  
    4. public class stateManagement : MonoBehaviour, NewControls.IMap1Actions, NewControls.IMap2Actions
    5. {
    6.     private NewControls controls;
    7.     public int state;
    8.  
    9.     public void OnToState1(InputAction.CallbackContext context)
    10.     {
    11.         if(context.performed)
    12.         {
    13.             state = 1;
    14.             controls.map1.Enable();
    15.             controls.map2.Disable();
    16.         }
    17.     }
    18.  
    19.     public void OnToState2(InputAction.CallbackContext context)
    20.     {
    21.         if(context.performed)
    22.         {
    23.             state = 2;
    24.             controls.map2.Enable();
    25.             controls.map1.Disable();
    26.         }
    27.     }
    28.  
    29.     void Start()
    30.     {
    31.         controls = new NewControls();
    32.         controls.map1.SetCallbacks(this);
    33.         controls.map2.SetCallbacks(this);
    34.         state = 1;
    35.         controls.map1.Enable();
    36.         controls.map2.Disable();
    37.     }
    38. }

    3) The game starts in state 1. Then I trigger the "ToState2" (button A) action, the game switchs to state 2. Everything is fine.

    4) Then I trigger the "ToState1" (button X) action, the game switchs to state 1. Still fine.

    5) Then, if I push the button A again, the game doesnt switch to state 2. I need to push the button A a second time in order to achieve this.

    Am I doing something wrong?
    I'm using the Input System 1.0.0 preview 3. But I had this problem already in older versions
     
  2. philippelabalette

    philippelabalette

    Joined:
    Mar 23, 2019
    Posts:
    14
    I tried to use "release only" interaction instead of "press only" and the problem of double taping is solved. But this is not the functionality I want. I want to be able to use "press only".
    It seems to be a bug. Some developers here to try to reproduce the error. (see my first post. It can be reproduced in 5min). Thanks for the support
     
  3. philippelabalette

    philippelabalette

    Joined:
    Mar 23, 2019
    Posts:
    14
    OK, I made an official bug report. If I get information from the developer team I will add it there.
     
  4. philippelabalette

    philippelabalette

    Joined:
    Mar 23, 2019
    Posts:
    14
  5. NanushTol

    NanushTol

    Joined:
    Jan 9, 2018
    Posts:
    121
    I'm using a state machine in my game to handle different states in the enter function of each state i call the action map enable, and on the exit i call the disable function, I didn't encounter this problem.
    from going quickly over your code I would try Disabling the previous map before enabling the new one.
    Code (CSharp):
    1. public void OnToState1(InputAction.CallbackContext context)
    2.     {
    3.         if(context.performed)
    4.         {
    5.             state = 1;
    6.             controls.map2.Disable(); // <---- changed this to be first
    7.             controls.map1.Enable();
    8.         }
    9.     }
    10.     public void OnToState2(InputAction.CallbackContext context)
    11.     {
    12.         if(context.performed)
    13.         {
    14.             state = 2;
    15.             controls.map1.Disable(); // <---- changed this to be first
    16.             controls.map2.Enable();
    17.         }
    18.     }
     
  6. philippelabalette

    philippelabalette

    Joined:
    Mar 23, 2019
    Posts:
    14
    Thanks for your help. But it didn't work
    I use also in my game the enable/disable of maps. It works fine most of the time but not if you disable a map after pushing a button. The associated action is then like stuck in an undefined state. If you enable again this action map later, the action is not triggered on the first time but only on the second time.
     
    Last edited: Dec 27, 2019
  7. NanushTol

    NanushTol

    Joined:
    Jan 9, 2018
    Posts:
    121
    a few days after I have posted here I encountered the same problem in my project, my escape key was mapped in 2 action maps to a "Cancel" action, and when I was trying to get out of menus I needed to press it twice to get it to work,
    I found that I had an old piece of code enabling the input actions, so effectively I enabled them twice, once I removed the old redundant code every thing worked fine.

    I'm also using a different syntax to get the key press, like this
    InputActions.MyMap.MyAction.performed += ctx => MyFunction();


    maybe try removing the enable from the start method, and from any other script. try to enable it only once

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3. public class stateManagement : MonoBehaviour, NewControls.IMap1Actions, NewControls.IMap2Actions
    4. {
    5.     private NewControls controls;
    6.     public int state;
    7.     public void OnToState1(InputAction.CallbackContext context)
    8.     {
    9.         if(context.performed)
    10.         {
    11.             state = 1;
    12.             controls.map1.Enable();
    13.             controls.map2.Disable();
    14.         }
    15.     }
    16.     public void OnToState2(InputAction.CallbackContext context)
    17.     {
    18.         if(context.performed)
    19.         {
    20.             state = 2;
    21.             controls.map2.Enable();
    22.             controls.map1.Disable();
    23.         }
    24.     }
    25.     void Start()
    26.         controls.map1.SetCallbacks(this)
    27.         controls.map2.SetCallbacks(this);
    28.         controls = new NewControls();
    29.         state = 1;
    30.     }
    31. }
     
  8. Koen-Matthijs

    Koen-Matthijs

    Joined:
    Aug 1, 2015
    Posts:
    5
    I posted this reply in another thread too, but I decided to post here as well as it will probably help you get this working :

    Had the same issue using TAB-Key.

    Then I switched the Interaction on that Action to "Release Only" and it started working.
    Seems the preliminary analysis of others in this thread is correctly : if you call SwitchCurrentActionMap() during the Completed-phase of a "Press Only" Interaction, the control-state becomes unstable.
     
  9. philippelabalette

    philippelabalette

    Joined:
    Mar 23, 2019
    Posts:
    14
    This issue has been solved in the 1.0.0-preview 6. Thanks!!