2

I need to create an object that behaves in every way like the Gamepad objects found in navigator.getGamepads(), but presents specific static data rather than data about a real gamepad. How can I create such an object?

> navigator.getGamepads()[0]
Gamepad {
  id: "USB Gamepad (Vendor 0079 Product: 0011)",
  index: 0,
  connected: true,
  timestamp: 1234.567890,
  mapping: "",
  axes: [0, 0],
  buttons: [GamepadButton, GamepadButton, GamepadButton, GamepadButton, GamepadButton, GamepadButton], 
  __proto__: Gamepad
}
Sparr
  • 7,489
  • 31
  • 48

1 Answers1

0

I know it's kinda late, but cooncidentally enough I ran into the same thing.

I wanted to create fake gamepad object for when my script thinks when there is a gamepad connected, but it can't find it.

So here's a simple solution:

function GamepadObject() {
  return { id: "Fake-Gamepad", index: 0, mapping: "", hand: "", connected: true, buttons: [], axes: [], timestamp: 0, pose: GamepadPose, hapticActuators: [] };
}

// Use it like this:
let gamepad = GamepadObject();

console.log(gamepad);

// Object  { id: "Fake-Gamepad", index: 0, … }

I copied a real gamepad and made all the variables reset. This way it can get all child objects from the fake gamepad, and not run into any errors.

Parking Master
  • 551
  • 1
  • 4
  • 20