Skip to content

fix(controller): guard btnHome against gamepads without a buttons array - #1005

Open
Endymi0n74 wants to merge 1 commit into
redphx:typescriptfrom
Endymi0n74:feat/poll-gamepad-crash-guard
Open

fix(controller): guard btnHome against gamepads without a buttons array#1005
Endymi0n74 wants to merge 1 commit into
redphx:typescriptfrom
Endymi0n74:feat/poll-gamepad-crash-guard

Conversation

@Endymi0n74

Copy link
Copy Markdown

Fixes the crash class reported in #991: controller input silently stops working — pollGamepad throws Cannot read properties of undefined (reading 'pressed').

Root cause

poll-gamepad reads the Home button state on every poll:

const btnHome = currentGamepad.buttons[16];

Gamepad objects are not guaranteed to expose a buttons array at every poll (observed on a GameSir G7 SE). When the array is missing, buttons[16] throws, the exception kills the whole polling loop and controller input silently stops working until a reload — the exact symptom in #991.

Fix

const btnHome = currentGamepad.buttons?.[16];
  • When buttons is missing, btnHome is undefined → the controller-shortcuts block becomes a no-op.
  • The rest of the poll (gamepad input delivery, button mapping) keeps running.
  • Zero behavioral change for gamepads that report buttons normally.

Verification

  • Build: exit 0 (eslint + TS gate).
  • Bundle: guarded access present, unguarded buttons[16] gone.
  • Minimal diff: 1 file, +3/−1.

@redphx — happy to adjust if you'd rather guard currentGamepad itself too.

Gamepad objects are not guaranteed to expose a `buttons` array at every
poll (reported on a GameSir G7 SE, redphx#991): `currentGamepad.buttons[16]`
then throws, the exception kills the whole polling loop and controller
input silently stops working until a reload.

Use `currentGamepad.buttons?.[16]`: when the array is missing, `btnHome`
is `undefined`, the controller-shortcuts block becomes a no-op and the
rest of the poll (gamepad input delivery) keeps running. No behavioral
change for gamepads that report buttons normally.

Branch ready locally — not opened yet (awaiting the maintainer's
feedback window on the perf PRs before sending more).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant