Gamepad Example

Hi All,

Struggled to find any examples of the gamepad features or much documentation. So now that I have figured it out I thought I would share some code to help you work with gamepads in your games.

So I made a folder in my assets called Scripts → Global and another folder inside Scripts called Scenes. I’ve then created this first defaultScript into the Scenes folder and a gamepad script into Global.

Place the scene script into your scene and then run the project and check the console for gamepad button presses. I’ve also mapped this as an xbox controller layout (XYBA).

defaultScript

import { _decorator, Component, EventGamepad, input, Input } from 'cc';
import { gamepad } from '../Global/gamepad';
const { ccclass, property } = _decorator;

@ccclass('defaultScript')
export class defaultScript extends Component {


    start() {
        input.on(Input.EventType.GAMEPAD_INPUT, this.onButtonDown, this);
    }

    onButtonDown(event: EventGamepad){
        const button = gamepad.check(event.gamepad);
        var buttonPressed = false;
        if(button.button_a == 1){
            buttonPressed = "a";
        }
        if(button.button_x == 1){
            buttonPressed = "x";
        }
        if(button.button_y == 1){
            buttonPressed = "y";
        }
        if(button.button_b == 1){
            buttonPressed = "b";
        }
        if(button.button_start == 1){
            buttonPressed = "start";
        }
        if(button.button_select == 1){
            buttonPressed = "select";
        }
        if(button.button_up == 1){
            buttonPressed = "dpad up";
        }
        if(button.button_down == 1){
            buttonPressed = "dpad down";
        }
        if(button.button_right == 1){
            buttonPressed = "dpad right";
        }
        if(button.button_left == 1){
            buttonPressed = "dpad left";
        }
        if(button.l1 == 1){
            buttonPressed = "l1";
        }
        if(button.l2 == 1){
            buttonPressed = "l2";
        }
        if(button.r1 == 1){
            buttonPressed = "r1";
        }
        if(button.r2 == 1){
            buttonPressed = "r2";
        }
        if(button.button_left_stick == 1){
            buttonPressed = "Left stick";
        }
        if(button.button_right_stick == 1){
            buttonPressed = "Right stick";
        }
        if(buttonPressed != false){
            console.log("Button "+buttonPressed+" pressed");
        }
    }
}

gamepad

import { _decorator, Component } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('gamepad')
export class gamepad extends Component {

    public static check(gamepad){
        var button  = [0];
        button.button_a = gamepad.buttonSouth.getValue();
        button.button_x = gamepad.buttonWest.getValue();
        button.button_y = gamepad.buttonNorth.getValue();
        button.button_b = gamepad.buttonEast.getValue();
        button.button_select = gamepad.buttonShare.getValue();
        button.button_start = gamepad.buttonOptions.getValue();
        button.button_up = gamepad.dpad.up.getValue();
        button.button_down = gamepad.dpad.down.getValue();
        button.button_left = gamepad.dpad.left.getValue();
        button.button_right = gamepad.dpad.right.getValue();
        button.l1 = gamepad.buttonL1.getValue();
        button.l2 = gamepad.buttonL2.getValue();
        button.r1 = gamepad.buttonR1.getValue();
        button.r2 = gamepad.buttonR2.getValue();
        button.button_left_stick = gamepad.buttonL3.getValue();
        button.button_right_stick = gamepad.buttonR3.getValue();
        button.left_stick = gamepad.leftStick.getValue();
        button.left_stick_up = 0;
        button.left_stick_down = 0;
        button.left_stick_left = 0;
        button.left_stick_right = 0;
        if(gamepad.leftStick.getValue().y > 0){
            button.left_stick_up = gamepad.leftStick.getValue().y;
        } else if(gamepad.leftStick.getValue().y < 0){
            button.left_stick_down = -gamepad.leftStick.getValue().y;
        }
        if(gamepad.leftStick.getValue().x > 0){
            button.left_stick_right = gamepad.leftStick.getValue().x;
        } else if(gamepad.leftStick.getValue().x < 0){
            button.left_stick_left = -gamepad.leftStick.getValue().x;
        }
        button.right_stick = gamepad.rightStick.getValue();
        button.right_stick_up = 0;
        button.right_stick_down = 0;
        button.right_stick_left = 0;
        button.right_stick_right = 0;
        if(gamepad.rightStick.getValue().y > 0){
            button.right_stick_up = gamepad.rightStick.getValue().y;
        } else if(gamepad.rightStick.getValue().y < 0){
            button.right_stick_down = -gamepad.rightStick.getValue().y;
        }
        if(gamepad.rightStick.getValue().x > 0){
            button.right_stick_right = gamepad.rightStick.getValue().x;
        } else if(gamepad.rightStick.getValue().x < 0){
            button.right_stick_left = -gamepad.rightStick.getValue().x;
        }
        return button;
    }
    
}

Thanks,
-iDev

2 Likes

Nice job!!

1 Like

Thank you for sharing ! I pinned this nice code sample

1 Like