Improvements to haptic error handling

- Move gamepad index check to the JS side to simplify the code
- Handle gamepads that don't support haptics
This commit is contained in:
Cameron Gutman
2022-11-06 20:20:45 -06:00
parent ce52398dc5
commit a191915b9d
2 changed files with 4 additions and 9 deletions

View File

@@ -87,7 +87,10 @@ function handleMessage(msg) {
const weakMagnitude = parseFloat(eventData[1]);
const strongMagnitude = parseFloat(eventData[2]);
console.log("Playing rumble on gamepad " + gamepadIdx + " with weakMagnitude " + weakMagnitude + " and strongMagnitude " + strongMagnitude);
navigator.getGamepads()[gamepadIdx].vibrationActuator.playEffect('dual-rumble', {
// We may not actually have a gamepad at this index.
// Even if we do have a gamepad, it may not have a vibrationActuator associated with it.
navigator.getGamepads()[gamepadIdx]?.vibrationActuator?.playEffect('dual-rumble', {
startDelay: 0,
duration: 5000, // Moonlight should be sending another rumble event when stopping.
weakMagnitude: weakMagnitude,