Search Unity

Actions canceled when game loses focus

Discussion in 'Input System' started by nickleplated, Mar 27, 2020.

  1. nickleplated

    nickleplated

    Joined:
    Nov 2, 2018
    Posts:
    26
    I've got a physical toggle switch that I'm using for the parking brake on a vehicle.
    I've set it up the action so that performed engages the brake and canceled releases it.

    I've noticed that when the game loses focus, the parking brake is released and my vehicle starts rolling away.
    When I refocus it, it stops moving.

    I know disabling an action causes canceled to get fired and performed is fired again when it's re-enabled, but didn't expect it to happen when I switched to a different window.

    I wish there was a "freeze" option that would stop an action from receiving new input like disable does but wouldn't change the current phase of the action. I think that would be very useful when opening the game menu, changing bindings, and also when the app loses focus.

    Am I going to have to put checks in all my action handlers to see if the user alt-tabbed away?
     
  2. MlleBun

    MlleBun

    Joined:
    Sep 19, 2017
    Posts:
    163
    To see if the game is focused, you can have OnApplicationFocus. Personnaly, I simply pause the game when hasFocus is false so that I don't loose the current action or user input.
     
  3. nickleplated

    nickleplated

    Joined:
    Nov 2, 2018
    Posts:
    26
    I did some testing with this and things are happening in the following order:
    Losing Focus
    1. Application.isFocused set to false
    2. action.canceled is fired
    3. OnApplicationFocus is called with false
    Regaining Focus
    1. Application.isFocused set to true
    2. OnApplicationFocus is called with true
    3. action.performed is fired
    So if I check Application.isFocused in the canceled event, I can prevent it from running that code, but I can't prevent performed from getting called again when re-focusing by checking isFocused.

    Additionally, if the user actually toggled the switch off while the game didn't have focus, I wouldn't see that change when they came back unless I scanned all the toggle switches in OnApplicationFocus, although this is probably unlikely to happen.

    How are you pausing your game that prevents the action from being canceled?

    I tried setting Application.runInBackground to false, but it still got canceled (and I can't use that anyway).
    Unfortunately, mine is a networked multiplayer game, so I'm limited in what things I can stop, but if you have another way to do it, I'm happy to try it.
     
  4. nickleplated

    nickleplated

    Joined:
    Nov 2, 2018
    Posts:
    26
    I've hooked into InputSystem.onDeviceCommand and whenever there's a QueryCanRunInBackground command, I set canRunInBackground to true. So far, this seems to be working to make it stop canceling my actions when the game loses focus, but I'll have to do some more testing to see if it causes any bad side effects.
     
  5. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    @nickleplated RE this and other feedback you have posted, just want to post real quick that this isn't going unnoticed (in general, I read everything in this forum even if I don't get to reply). Agree there's things that need further refinement. There's some general refinement around action behavior (or at least control over it) that is needed and there's further work on focus behavior that I think is needed. Looking into it. Some of the action-specific things won't get addressed until after 1.0 but whatever isn't making it for 1.0 (which is getting imminent) is going onto the pile.
     
  6. nickleplated

    nickleplated

    Joined:
    Nov 2, 2018
    Posts:
    26
    @Rene-Damm, thanks for the update and thanks for all the work you're doing on this. Fortunately I've been able to work around most of the problems I've encountered and I've been able to live with the few that I haven't.