Gamepad help please

So I found a gamepad, and it looks like this might be a bug in PlayCanvas.

What update is doing is it always update the current value, and sets the previous value to whatever was in current before it updated, so it’s always lagging one frame behind.

Now wasPressed returns true only if current says it’s pressed, and previous says it is not pressed. The problem seems to be that both current and previous always have the same value. If you print them out, they’re synced together, so the condition for wasPressed is never true.

My guess is that this is because previous and current are set to objects, and so they might both be pointing to the same object as opposed to making a copy.

I implemented my own version of update and wasPressed here as a proof of concept:

var Test = pc.createScript('test');

Test.prototype.initialize = function() {
    this.previousUpPressed = false; 
    this.currentUpPressed = false;
};

Test.prototype.wasUpPressed = function(){
    if(!this.currentUpPressed) return false; 
    
    return this.currentUpPressed && !this.previousUpPressed;
};

Test.prototype.update = function(dt) {
    this.previousUpPressed = this.currentUpPressed;
    this.currentUpPressed = this.app.gamepads.isPressed(pc.PAD_1, pc.PAD_UP);
    
    
    if(this.wasUpPressed()){
        console.log("Pressing Up");
    }
};

Please try this. It works for me. (Obviously this is only for one specific key but I thought I’d confirm if this does what you need before attempting to generalize it).

1 Like