Gamepad events not triggering

Hi all
Long shot here, I’m trying to make a ludum dare game and after a blazing first 2 hours i’ve now spent 3 hours trying to get any kind of input from a gamepad, here’s my input manager, I get console log messages for the gamepad connecting and disconnecting, and stepping through with VScode debugging I can see it’s adding the events, but they never trigger when the inputs are applied


import "@babylonjs/core/Gamepads/gamepadSceneComponent";
import { GamepadManager, Xbox360Pad } from "@babylonjs/core/Gamepads";
import { IGame, IInputManager } from "./interfaces";
import { Vector2 } from "@babylonjs/core/Maths/math";

export class InputManager implements IInputManager {
  gpm: GamepadManager

  public constructor(owner: IGame) {
    //joypad
    this.gpm = new GamepadManager(owner.scene);
    this.gpm.onGamepadConnectedObservable.add((gp, state) => {
      console.log(`gamepad connected ${gp.id}`)
      if (gp instanceof Xbox360Pad){
        const xpad = gp as Xbox360Pad
        xpad.onleftstickchanged((values)=>{ 
          console.log(`joypad move ${values.x}, ${values.y}`)
          this.move.set( values.x, values.y )
        })
        xpad.onbuttondown((button)=>{
          console.log(`joypad button ${button}`)
        })
      }
    })

    this.gpm.onGamepadDisconnectedObservable.add((gp, state)=>{
      console.log(`gamepad disconnected ${gp.id}`)
    })
  }

  move = new  Vector2(0,0)
  fire:boolean = false
}

i’m using typescript, es6 tree shaking, webpack etc
tested using chrome and edge with 2 different xbox controllers
repos here: GitHub - CiderPunk/NuclearHammer: Game, Ludum dare entry
thankyou!

@PolygonalSun Can you help?

Hey @CiderPunk,
Just out of curiosity, what kind of controller are you using to test the code (360, One, Series X/S, etc.)?

@PolygonalSun I tried with a genuine xbox360 wired and an 8bitDo blue tooth controller which shows up as XINPUT, they both worked fine with this playground: https://playground.babylonjs.com/#U3XJTB#38
but not in my ES6 tree shaking project.
Now i’ve got a bit more time i’ll try recreate the problem separately

Babylon overrides prototypes to extend classes , so you often will have bugs due to missing functionality when you do individual imports. First, i suggest using the full bundle for anything that isnt a product catalog or similar where the user navigating away within < 2s on mobile internet and slow processor is a concern. We all use the playground and that thing is like 15mb, it literally doesnt matter. Anyway, if you do want to continue down that road, the best way to do it is to open up the babylon codebase and search for “.prototype.myMissingMethod” if you have an error when trying to access the missing property, or maybe need to be a little more creative in your search.

To get the gamepad working, u can try this demo , it works well. F18FlightSimulator-ammojs/f18GamePadController.ts at master · renjianfeng/F18FlightSimulator-ammojs · GitHub

I think I’ve found the problem, I was initialising the GamepadManager with my scene as a parameter… if you do this, the gamepad manager never starts it’s monitoring.
you connect your gamepad, this is called in gamepadmanager.js

  _startMonitoringGamepads() {
        if (!this._isMonitoring) {
            this._isMonitoring = true;
            //back-comp
            if (!this._scene) {
                this._checkGamepadsStatus();
            }
        }
    }

and in _checkGamepadStatis, it’s wont queue the next update unless there’s no scene.:

     if (this._isMonitoring && !this._scene) {
            Engine.QueueNewFrame(() => {
                this._checkGamepadsStatus();
            });
        }

pretty sure that’s a bug @PolygonalSun ?

Thank you @jeremy-coleman that’s sound advice, I’ve always been obsessed with optimization waay to early, including babylon/core has made intellisense way more helpful too!

Lemme take a quick look at this because if a non-null Scene param is causing an issue, that’d absolutely be a bug.

So, there’s definitely a bug here. I have to confer with the team about some of the logic but since you’ve already identified the offending lines, the fix will be super quick and easy.

1 Like

Fix has been merged: GamepadManager: Fixed issue where providing scene object to constructor would prevent status updates by PolygonalSun · Pull Request #13798 · BabylonJS/Babylon.js (github.com)

1 Like