Thursday 10 August 2023

Register custom Gamepad class with GamepadEvent results in Failed to convert value to 'Gamepad'

I'm trying trying build a little virtual controller as Gamepad class and register it.

For now it is a copy of the Gamepad class:

class MyJoystick {
    readonly axes: ReadonlyArray<number>;
    readonly buttons: ReadonlyArray<GamepadButton>;
    readonly connected: boolean;
    readonly hapticActuators: ReadonlyArray<GamepadHapticActuator>;
    readonly id: string;
    readonly index: number;
    readonly mapping: GamepadMappingType;
    readonly timestamp: DOMHighResTimeStamp;

    constructor() {
        this.axes = [];
        this.buttons = [];
        this.connected = true;
        this.hapticActuators = [];
        this.id = "my-id";
        this.index = 200;
        this.mapping = "standard";
        this.timestamp = Math.floor(Date.now() / 1000);
    }
}

Using

const event = new GamepadEvent("gamepadconnected", {
    gamepad: new MyJoystick() as Gamepad
})
window.dispatchEvent(event);

But I'm getting the following error message:

Failed to construct 'GamepadEvent': Failed to read the 'gamepad' property from 'GamepadEventInit': Failed to convert value to 'Gamepad'.
TypeError: Failed to construct 'GamepadEvent': Failed to read the 'gamepad' property from 'GamepadEventInit': Failed to convert value to 'Gamepad'.

Question: How can I create my own Gamepad class implementation which I can also register with an GamepadEvent?



from Register custom Gamepad class with GamepadEvent results in Failed to convert value to 'Gamepad'

No comments:

Post a Comment