Gamepad help please

Gamepads. Read the documentation but still have no clue.

I assumed it was something like

if(this.app.gamepad.isPressed(pc.PAD_1){
    do.amazing.stuff('clearly not real code');
};

would do the trick as long as I knew the mapping of the gamepad, but I’m guessing there’s more to it than that. Was curious about people’s thoughts and solutions on this?

I’d like to get the analog sticks as well, but for now the simple dpad will do.

Engine source is open, and has actual code, which serves best source of direct information of capabilities: https://github.com/playcanvas/engine/blob/master/src/input/game-pads.js

Although I would likely use native GamePad API instead of engine based.

Great! Thanks for the link!

So I tried doing

this.app.gamepads.isPressed(pc.PAD_UP)

and it didn’t seem to work, I pressed every button on my controller to double check that the mapping wasn’t redirected incorrectly or something odd like that. Not sure what to do next and what do you mean by native GamePad API?

To be specific this is the controller i’m testing with.

It works fine with games on stream, but not sure if it will with web games.

Test on this page: http://html5gamepad.com/

Man how do you guys know so damn much! This link again is awesome.

This is what I got from the site.

It’s an xbox 360 api, which seems not to be currently supported through your engine, as you predicted.

So how do I get this info then remap it to playcanvas api.

I hope what I type make sense.

so b12 = up,
b13 = down,
b14 = left,
b15 = right.

I literally just googled it.

As mentioned before, I recommend you use a native web GamePad API, just google MDN docs for it.

damn my google skills need improvement lol.

Thanks. I’ll report back if I get stuck again.

I got gamepads to work on my game! But only the isPressed() function.

Not sure how to go about the wasPressed() function. Some advice on this please?

I see it says that I have to update() but not sure why or how. Can someone elaborate?

I don’t have a gamepad to test this, but have you tried doing:

this.app.gamepads.update(dt)

In your script’s update function, and then calling:

this.app.gamepads.wasPressed(1) // Or whatever button index

I can guess that update just keeps track of buttons that were pressed in the last frame, and wasPressed returns that.

It might also be useful to look at the source to see exactly how they’re doing it.

Hope this helped!

Tried it, to no avail :frowning:

Yea I also took a look at the document, not sure how to interpret it that’s why I’m asking for some assistance on this.

Thanks @OmarShehata

@max maybe you can help shine some insight on this?

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

Yea I’ll test this! I’m sure it will work too looks like it.

So perhaps we should make this remark to the Playcanvas folk on fixing it. Because that seems like a lot of extra code to make an already functional code for other keys.

I have a workaround actually for this, but was curious to why it was so difficult to implement. Thanks :slight_smile:

1 Like

Guys, if you think you’ve found a bug in the engine, feel free to log here. Thanks! :smile:

Have done it :smiley:

BACK AGAIN! Thought I reignite this thread :slight_smile:

So it seems that just basic functionality for gamepads that I use to use for older versions of PlayCanvas don’t work any more. Or maybe I’m missing something

if(this.app.gamepads.isPressed(pc.PAD_1 , pc.PAD_UP)){
        console.log('pressed up');
}

I get an error that takes me to ‘isPressed’ and says it’s null :S not sure what’s going on but thought I’d let you all know. Would be great to get this to work for my game project!

Also is there a way to let playcanvas know if there are gamepads at all?

Do you see the gamepad in this tester? http://html5gamepad.com/

Hey - you need to enable Gamepads under the Input Settings:

45%20PM

2 Likes

Oh what!!! Is that new?

I was about to do it via straight Javascript lol. Thanks!

how do you code down and left on the sticks