Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Question Understanding QueueStateEvent & Input System Lifecycle

Discussion in 'Input System' started by baakta, Feb 3, 2023.

  1. baakta

    baakta

    Joined:
    May 22, 2019
    Posts:
    14
    Currently, I'm building out a custom input device that passes its state to the system via QueueStateEvent in an OnUpdate callback. This custom device takes its state either from another "built-in" input device or from a third-party input API.

    Here's an exemplification of my issue: I press (without releasing) ButtonA which is bound to ActionA with an ActionType of Button and default interactions. My expected behavior is for the action to be "started" and "performed" once while pressed, with a "canceled" callback on release. However, I'm receiving repeated "performed" callbacks on seemingly every input system update while the ButtonA is pressed.

    I think the issue is that because I'm queueing a new event state for my custom device on every update (even if the control values themselves are unchanged), actions are re-triggered.

    So, is it my responsibility to track the state of each of the controls in my custom device and only queue state events for a control when its value has changed? I guess I naively expected the input system to just ingest state events and only trigger actions for controls that had changed state. Am I understanding this correctly? How would I best go about producing the expected behavior I outlined in my second paragraph? Any recommendation or best practices? Thanks in advance!
     
  2. rdjadu

    rdjadu

    Joined:
    May 9, 2022
    Posts:
    115
    Input actions will not trigger if the state is bit by bit the same. An input action puts a state monitor (
    InputState.AddChangeMonitor
    ) on the bit of state it is interested in. When state is modified and there's a monitor, the state about to be written is memcmp'd to the state that's current stored. If it's different, it'll set off the monitor and the action triggers.
     
  3. baakta

    baakta

    Joined:
    May 22, 2019
    Posts:
    14
    Thanks for the explanation. So, the monitor shouldn't qualify a state struct passed into the system via QueueStateEvent as a state change as long as its values are identical to the last struct passed in? Is the monitor tracking the overall state of the struct or the state of each individual control within the device state struct?
     
  4. rdjadu

    rdjadu

    Joined:
    May 9, 2022
    Posts:
    115
    Yup.

    The memory region of each individual control. Which, however, requires these regions to be set up correctly. If, for example, a control includes some garbage unused memory in its memory region and the contents of that garbage section change around (e.g. if someone constructs state events from uncleared memory), then the state monitors will get phantom triggers.
     
    baakta likes this.
  5. baakta

    baakta

    Joined:
    May 22, 2019
    Posts:
    14
    Thanks for this @rdjadu - I think I'm following your explanation.

    I came back to this issue after working on other things for a couple months, and I'm still having trouble. I can't for the life of me figure out why, for example, a "Select" action bound to triggerPressed on my custom device is firing through all three phases (started, performed, cancelled) with every OnUpdate callback that the trigger is pressed. The value is not changing (as verified in the Input Debugger); but the action is refiring in its entirety with each update. Maybe, like you mention, I'm not setting up my memory regions correctly? If you have the chance, would you mind taking a look at the custom device and data class in question?

    Code (CSharp):
    1. using UnityEngine;
    2. public struct HandedXRControllerDeviceState : IInputStateTypeInfo
    3. {
    4.     public FourCC format => new FourCC(a: 'P', b: 'L', c: 'A', d: 'Y');
    5.  
    6.     [InputControl(layout = "Vector2")]
    7.     public Vector2 thumbstick;
    8.  
    9.     [InputControl(layout = "Axis")]
    10.     public float trigger;
    11.     [InputControl(layout = "Axis")]
    12.     public float grip;
    13.  
    14.     [InputControl(name = "triggerPressed", layout = "Button", bit = 0)]
    15.     [InputControl(name = "gripPressed", layout = "Button", bit = 1)]
    16.     [InputControl(name = "buttonSouth", layout = "Button", bit = 2)]
    17.     [InputControl(name = "buttonNorth", layout = "Button", bit = 3)]
    18.     [InputControl(name = "buttonSystem", layout = "Button", bit = 4)]
    19.     [InputControl(name = "thumbstickClicked", layout = "Button", bit = 5)]
    20.     [InputControl(name = "triggerTouched", layout = "Button", bit = 6)]
    21.     [InputControl(name = "buttonSouthTouched", layout = "Button", bit = 7)]
    22.     [InputControl(name = "buttonNorthTouched", layout = "Button", bit = 8)]
    23.     [InputControl(name = "thumbstickTouched", layout = "Button", bit = 9)]
    24.     public int buttons;
    25. }
    26.  
    27. #if UNITY_EDITOR
    28. [InitializeOnLoad]
    29. #endif
    30. [InputControlLayout(displayName = "HandedXRController", stateType = typeof(HandedXRControllerDeviceState), commonUsages = new string[] { "ActiveHand", "PassiveHand" })]
    31. public class HandedXRControllerDevice : XRControllerWithRumble, IInputUpdateCallbackReceiver
    32. {
    33. public Vector2Control thumbstick { get; private set; }
    34. public AxisControl trigger { get; private set; }
    35. public AxisControl grip { get; private set; }
    36. public ButtonControl triggerPressed { get; private set; }
    37. public ButtonControl gripPressed { get; private set; }
    38. public ButtonControl buttonSouth { get; private set; }
    39. public ButtonControl buttonNorth { get; private set; }
    40. public ButtonControl buttonSystem { get; private set; }
    41. public ButtonControl thumbstickClicked { get; private set; }
    42. public ButtonControl triggerTouched { get; private set; }
    43. public ButtonControl buttonSouthTouched { get; private set; }
    44. public ButtonControl buttonNorthTouched { get; private set; }
    45. public ButtonControl thumbstickTouched { get; private set; }
    46.  
    47. protected override void FinishSetup()
    48. {
    49.     base.FinishSetup();
    50.  
    51.     thumbstick = GetChildControl<Vector2Control>("thumbstick");
    52.     trigger = GetChildControl<AxisControl>("trigger");
    53.     grip = GetChildControl<AxisControl>("grip");
    54.     triggerPressed = GetChildControl<ButtonControl>("triggerPressed");
    55.     gripPressed = GetChildControl<ButtonControl>("gripPressed");
    56.     buttonSouth = GetChildControl<ButtonControl>("buttonSouth");
    57.     buttonNorth = GetChildControl<ButtonControl>("buttonNorth");
    58.     buttonSystem = GetChildControl<ButtonControl>("buttonSystem");
    59.     thumbstickClicked = GetChildControl<ButtonControl>("thumbstickClicked");
    60.     triggerTouched = GetChildControl<ButtonControl>("triggerTouched");
    61.     buttonSouthTouched = GetChildControl<ButtonControl>("buttonSouthTouched");
    62.     buttonNorthTouched = GetChildControl<ButtonControl>("buttonNorthTouched");
    63.     thumbstickTouched = GetChildControl<ButtonControl>("thumbstickTouched");
    64. }
    65.  
    66. #if UNITY_EDITOR
    67.     static HandedXRControllerDevice() => RegisterLayout();
    68. #endif
    69.  
    70.     [RuntimeInitializeOnLoadMethod]
    71.     public static void RegisterLayout()
    72.     {
    73.         InputSystem.RegisterLayout(
    74.             type: typeof(HandedXRControllerDevice),
    75.             name: "HandedXRController",
    76.             matches: new InputDeviceMatcher()
    77.                 .WithDeviceClass("^HandedXRController", supportRegex: true)
    78.         );
    79.     }
    80.  
    81.     public void OnUpdate()
    82.     {
    83.         // Copy state from generic XRController input device and queue this state for this custom device.
    84.         HandedXRControllerDeviceState state = GetXRControllerState();
    85.  
    86.         InputSystem.QueueStateEvent(this, state);
    87.     }
    88.  
    89.     private HandedXRControllerDeviceState GetXRControllerState()
    90.     {
    91.         HandedXRControllerDeviceState state = new HandedXRControllerDeviceState();
    92.  
    93.         // Finds the appropriate XRController device by handedness.
    94.         InputDevice targetDevice = InputSystem.GetDevice<XRController>(
    95.             usages.Contains(InputDeviceManager.XRUsages.ActiveHand)
    96.             ? InputDeviceManager.ActiveHand
    97.             : InputDeviceManager.PassiveHand
    98.         );
    99.  
    100.         // If appropriate device was found, copy it's state to new state struct.
    101.         if (targetDevice != null)
    102.         {
    103.             state.thumbstick = targetDevice.GetChildControl<Vector2Control>("thumbstick").value;
    104.             state.trigger = targetDevice.GetChildControl<AxisControl>("trigger").value;
    105.             state.grip = targetDevice.GetChildControl<AxisControl>("grip").value;
    106.  
    107.             if (targetDevice.GetChildControl<ButtonControl>("triggerPressed").isPressed)
    108.                 state.buttons |= 1 << 0;
    109.             if (targetDevice.GetChildControl<ButtonControl>("gripPressed").isPressed)
    110.                 state.buttons |= 1 << 1;
    111.             if (targetDevice.GetChildControl<ButtonControl>("primaryButton").isPressed)
    112.                 state.buttons |= 1 << 2;
    113.             if (targetDevice.GetChildControl<ButtonControl>("secondaryButton").isPressed)
    114.                 state.buttons |= 1 << 3;
    115.             if (targetDevice.GetChildControl<ButtonControl>("start").isPressed)
    116.                 state.buttons |= 1 << 4;
    117.             if (targetDevice.GetChildControl<ButtonControl>("thumbstickClicked").isPressed)
    118.                 state.buttons |= 1 << 5;
    119.             if (targetDevice.GetChildControl<ButtonControl>("triggerTouched").isPressed)
    120.                 state.buttons |= 1 << 6;
    121.             if (targetDevice.GetChildControl<ButtonControl>("primaryTouched").isPressed)
    122.                 state.buttons |= 1 << 7;
    123.             if (targetDevice.GetChildControl<ButtonControl>("secondaryTouched").isPressed)
    124.                 state.buttons |= 1 << 8;
    125.             if (targetDevice.GetChildControl<ButtonControl>("thumbstickTouched").isPressed)
    126.                 state.buttons |= 1 << 9;
    127.         }
    128.  
    129.         return state;
    130.     }
    131. }
    132.  
     
  6. rdjadu

    rdjadu

    Joined:
    May 9, 2022
    Posts:
    115
    Sorry to hear the thing is fighting you.

    Nothing that readily stands out in the code to me. All the state change monitors certainly should be coming out just fine. Nothing tricky going on there in that state.

    What I'd probably do next is look at the actual events. In the debugger, there's an event trace at the bottom. Double-clicking an event pops up its contents. The trace can be paused so that you have time to sift through the data.

    If there's no button ups in the events, then the problem must indeed be elsewhere. Note that the if there's multiple successive events with an intermittent reset but the eventual state being "pressed", the button would still display as down in the debugger. It's only in the event stream that sub-frame (or high-frequency) state changes can be observed in the debugger.

    Also, out of curiosity, are you instantiating multiple of these devices in parallel? Are multiple controls (maybe from more than one of these devices) bound to the same action?

    Sorry, just fishing here. Don't really have a very specific suspicion :(
     
  7. baakta

    baakta

    Joined:
    May 22, 2019
    Posts:
    14
    No problem, I appreciate the help!

    I looked into the event trace as you suggested. For as long as the button (in this case, "triggerPressed" - but behavior seems to apply to all controls) is held, there's a consistent, rapid pattern of three events being logged. Two events wherein the value of triggerPressed is 1 and a third with the value 0. This seems to be consistent with the action rapidly phasing through "started", "performed", and "canceled". I don't really understand the raw memory views for these traced events, but I'm including a screen cap of the data from six sequential events which demonstrate the pattern I'm describing: https://imgur.com/a/AFzeVWM

    As for your other questions - I am instantiating two of my custom devices, each with a different usage ("active"/"passive" handed), but I just tested with only one device being instantiated and behavior is the same. There is currently only one control from this device bound to the action.
     
  8. baakta

    baakta

    Joined:
    May 22, 2019
    Posts:
    14
    Update! After further investigation, I discovered the issue is originating from the device I'm copying state from (in my case, an OculusTouchController) - not my custom device.

    onUpdate is firing three times per frame while a button control is held. On the first two sub-frames, the control on the source device reads "1"; on the third sub-frame, the value reads "0" (https://imgur.com/a/hR5Izmw). I don't know why this is happening, but it's knowledge I can work with.

    My solution is checking whether or not the source device has already been updated in the given frame, and if it has, I leave the state of my custom device as is.

    Code (CSharp):
    1.     public void OnUpdate()
    2.     {
    3.         if (_sourceDevice == null)
    4.         {
    5.             _sourceDevice = InputSystem.GetDevice<XRController>(
    6.                 usages.Contains(InputDeviceManager.XRUsages.ActiveHand)
    7.                     ? InputDeviceManager.ActiveHand
    8.                     : InputDeviceManager.PassiveHand
    9.             );
    10.  
    11.             if (_sourceDevice == null) return;
    12.         }
    13.        
    14.         if (!_sourceDevice.wasUpdatedThisFrame) return;
    15.        
    16.         HandedXRControllerDeviceState state = (PlatformManager.INSTANCE.platform) switch
    17.         {
    18.             PlatformManager.Platform.OculusVR => GetXRControllerState(),
    19.             PlatformManager.Platform.WebXR => GetWebXRControllerState(),
    20.             _ => new HandedXRControllerDeviceState()
    21.         };
    22.  
    23.         InputSystem.QueueStateEvent(this, state);
    24.     }
    Initial testing shows my issue is resolved with this workaround! That being said, it still feels like a workaround, and I'm going to have to test more extensively to make sure I'm not creating other issues. Don't love fixing problems without fully understanding the cause. Anyway, that's it for now. Thanks for your help on this!
     
  9. rdjadu

    rdjadu

    Joined:
    May 9, 2022
    Posts:
    115
    Ah doh, I have a suspicion. Dunno why this didn't occur to me earlier.

    There's a very annoying aspect to input updates which is that the editor gets its own updates. Complete with its own separate state. This doesn't happen in the player but in the editor,
    InputUpdateType.Editor
    unfortunately is a thing.

    So my guess is just doing

    Code (csharp):
    1. public void OnUpdate()
    2. {
    3.     #if UNITY_EDITOR
    4.     if (InputState.currentUpdateType == InputUpdateType.Editor)
    5.         return;
    6.     #endif
    7.    
    8.     //...
    Will solve the problem.

    The state where the button is unpressed is pretty surely coming from the editor update where the device has not received any input.
     
    frank-ijsfontein and baakta like this.
  10. baakta

    baakta

    Joined:
    May 22, 2019
    Posts:
    14
    Yep, that was it! Glad we got to the bottom of it - thanks for walking me through this.
     
    rdjadu likes this.