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

Question Consuming InputEvent with InputSystem.onAnyButtonPressed

Discussion in 'Input System' started by nucky9th, Oct 22, 2023.

  1. nucky9th

    nucky9th

    Joined:
    May 16, 2023
    Posts:
    6
    I'm using the new input system to trigger a popup when the player is in range of an interactable object, and triggers the correct Action. Once the popup is visible, I want the player to be able to press any key/button to close the popup.

    I'm using InputSystem.onAnyButtonPressed for this, and it works for any button, except the button that triggers the Interaction Action. If that button is pressed, the method to close the popup is fired, but then the player is still in range of the interactable, and so immediately interacts with it again, triggering the popup again.

    Is there a way to consume the event read from InputSystem.onAnyButtonPressed, so that, if the button is mapped to an Action, it doesn't trigger that Action? I've manually disabled the response to the callback so that when the popup is open, the callbacks are ignored, but as soon as the popup closes and the callbacks are being acted on again, the interact Action is read and used.

    Here's the code I'm using:
    Code (CSharp):
    1. private bool _vistaOpen;
    2.  
    3. private void Update()
    4. {
    5.     if (!_vistaOpen) return;
    6.  
    7.     InputSystem.onAnyButtonPress.CallOnce(e => CloseVista());
    8.  
    9. }
    10.  
    11. public void OnInteracted()
    12. {
    13.     if (_vistaOpen) return;
    14.     OpenVista();
    15. }
    16.  
    17. private void OpenVista()
    18. {
    19.      UIEvents.OnOpenVista?.Invoke(vistaSprite, imageSize);
    20.     GameEvents.DisablePlayerControlsUntimedEvent?.Invoke();
    21.     _vistaOpen = true;
    22. }
    23.  
    24. private void CloseVista()
    25. {
    26.      UIEvents.OnCloseVista?.Invoke();
    27.     _vistaOpen = false;
    28.     GameEvents.EnablePlayerControlsEvent?.Invoke();
    29. }
    30.