Skip to main content
This is a Q&A site. Every post is an inquiry, so the title should actually ask the question itself.
Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

Player Input Inquiry Can player input be missed while yielding WaitForSeconds?

Source Link

Player Input Inquiry

I just wanted to fact check myself on a scenario where the player may be locked out of input.

With this example, there is a chance that the player's input will not be received if they happen to press the button during WaitForSeconds(), correct?

private IEnumerator CheckForButton()
{
    // Check for player input at the beginning of the method
    if (GameEngine.Instance.InputController.playerActions.Menu.Select.WasPressedThisFrame())
        {
            // do stuff
            yield break;
        }
        
        // Is the player is locked out of input here?
        yield return NewWaitForSeconds(2f)
}

To avoid that, would using a loop as a timer and checking for player input like this be a more correct way?

private IEnumerator CheckForButton
{
    // Check for input at the beginning of the method
    if (GameEngine.Instance.InputController.playerActions.Menu.Select.WasPressedThisFrame())
    {
        // do stuff
    }
    float t = 0;
    float targetTime = 2f;
    
    // Also check for input while the timer counts down
    while(t < targetTime)
    {
        if (GameEngine.Instance.InputController.playerActions.Menu.Select.WasPressedThisFrame())
        {
            // do stuff
            yield break;
        }
        t += Time.deltaTime;

        yield return null;
    }
}

To be more concise, will the player be locked out of input if they press a button during WaitForSeconds()? Thanks!