Search Unity

Multiple callback in one frame

Discussion in 'Input System' started by PereKastor, Jul 15, 2019.

  1. PereKastor

    PereKastor

    Joined:
    May 3, 2019
    Posts:
    4
    Hello guys,

    I'm starting a new project and I have some issue with the new Input System. I get multiple callbacks in one frame for my input. But it's not consistent, some frame it's OK, some frame i get 2 or 3 callback calls.

    This is my example :

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3.  
    4. public class Controller : MonoBehaviour
    5. {
    6.     private GameInputs m_Inputs;
    7.     private int m_ProcessFrame = -1;
    8.  
    9.     void Start()
    10.     {
    11.         Debug.Log("Controller is started");
    12.         m_Inputs = new GameInputs();
    13.         m_Inputs.Player.Enable();
    14.         m_Inputs.Player.Movement.performed += OnInputs;
    15.     }
    16.  
    17.     private void OnDestroy()
    18.     {
    19.         Debug.Log("Controller is destroyed");
    20.         m_Inputs.Player.Movement.performed -= OnInputs;
    21.         m_Inputs.Player.Disable();
    22.     }
    23.  
    24.     public void OnInputs(InputAction.CallbackContext context)
    25.     {
    26.         int currentFrame = Time.frameCount;
    27.         Vector2 val = context.ReadValue<Vector2>();
    28.         if (m_ProcessFrame != currentFrame)
    29.         {
    30.             Debug.Log(Time.frameCount + $": {context.action} get call back with values {val.x}/{val.y}");
    31.             m_ProcessFrame = currentFrame;
    32.         }
    33.         else
    34.         {
    35.             Debug.LogWarning(Time.frameCount + $": {context.action} already get call back. These are the new values {val.x}/{val.y}");
    36.         }
    37.     }
    38. }
    As you can see on the image I added to this thread, i get a warning saying that I get multiple callback on the same frame. In addition, we can see the value is different between the same frame.

    You can see the see the settings I have. I changed the update mode to be during "Dynamic Update".

    Am i alone with this problem? What do you guys think?

    Unity version : 2019.1.9f1
    Input System version : preview - 0.2.10
     

    Attached Files:

    Stephano likes this.
  2. PereKastor

    PereKastor

    Joined:
    May 3, 2019
    Posts:
    4
    Hello @Rene-Damm , I still have this issue and want to understand if it's a normal behavior or not.

    I do not process inputs directly inside the callback and store them in variables to process all of them in the update method. It's really strange for me to have, inside a same frame, two different values of the same input. Which one do i have to select ? Should I process the input twice or just ignore the first one ? Both of these solutions are not really beautiful :(

    I upgrade the input system to the 0.9.6-preview version and i am on the 2019.2.4f1 unity version now.
     
  3. Jichaels

    Jichaels

    Joined:
    Dec 27, 2018
    Posts:
    237
    I think you should add them up and process in Update, like you would do for capturing inputs in Update to process in FixedUpdate. That's what I do and it's working fine so far
     
  4. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    This is expected - your callback fires whenever input changes, which can be multiple times per frame. How you need to treat this depends on your use case. If you want to eg. get values from a stick to steer movement, it is usually ok to just take the latest value. If you want to make sure that every button press is registered for some action, make sure you react to any value (as a button could be pressed _and_ released within one frame).