Is there a way to check for a controller and what type is being used?

● ARCHIVED · READ-ONLY
Started by Uzuki 10 posts View original ↗
  1. I'm just curious if there's a way to check if a player has a controller plugged into their computer when playing the game. I've got a bunch of controller icons for PS4, Switch and Xbox buttons so if I could check what kind of controller they have in would be nice too.
  2. Bump
  3. To check whether there's a recognised gamepad connected you could try this in a Script command:
    JavaScript:
    var swId = 1;  // ID of game switch to toggle if a gamepad is recognised
    var o = navigator.getGamepads();
    var value = o ? Object.keys(o).some(function(k) { return !!o[k]; }) : false;
    $gameSwitches.setValue(swId, value);
    Note that I don't have a gamepad to test with, so it might be slightly off. :kaoslp:

    Not sure how to check "type" or stuff like that, but here's a link to the relevant API docs in case that helps~
    Gamepad API - Web APIs | MDN
  4. @caethyril Once again you come in with the saving code. It works perfectly! You've helped me out on so much of my simple stuff, I should be paying you at this point.
  5. Sorry to necro a four year old thread, but after several hours trying to figure out how to get the gamepad ID, I finally figured it out. Since this is the thread that kept popping up when I was trying to solve this, I'm posting the solution here so that anyone else needing this can find it. Also, this thread was of great help; Caethyril was basically there. Additionally, this should work in MV or MZ (I'm using MZ).

    In a Script Call:
    JavaScript:
    var gamepadID = navigator.getGamepads()[0].id;
    $gameVariables.setValue(1, gamepadID);

    That gets the gamepad ID and sets it to Variable 1, just change the 1 to whatever Variable you want to use.
    If you want to get more complicated but also more specific, then you could do something like this:

    In a Script Call:
    JavaScript:
    var gamepad = navigator.getGamepads()[0];
    
    if (gamepad) {
        var gamepadID = gamepad.id;
        $gameVariables.setValue(1, gamepadID);
    
        if (gamepadID.includes('Xbox')) {
            // Xbox Controller set to 1
            $gameVariables.setValue(2, 1);
        } else if (gamepadID.includes('054c')) {
            // Sony Controller set to 2
            $gameVariables.setValue(2, 2);
        } else if (gamepadID.includes('057e')) {
            // Nintendo Controller set to 3
            $gameVariables.setValue(2, 3);
        } else {
            // Generic Controller set to 4
            $gameVariables.setValue(2, 4);
        }
    } else {
        // No gamepad detected
        $gameVariables.setValue(2, 0);
    }

    That will detect the gamepad's type, set Variable 1 to the ID, and it will set Variable 2 based on the manufacturer of the gamepad. This identifies the three big manufacturers, Microsoft, Sony, and Nintendo as well as setting up Generic Controller and Keyboard values as well. From here you could use conditional branches for button prompts.
    One could get WAY more involved with the JavaScript for this, but this should provide a solid jumping off point.

    Hope this helps someone. Z.
  6. Zael said:
    That gets the gamepad ID and sets it to Variable 1, just change the 1 to whatever Variable you want to use.
    This identifies the three big manufacturers, Microsoft, Sony, and Nintendo as well as setting up Generic Controller and Keyboard values as well.
    Heyhey! I just wanted to mention...that only gets the first device listed as a gamepad purr-ipheral!

    I haven't read through the API :kaoswt:

    But I'd totally bet that's determined by the order the user plugged things into their computer-nyan. So getGamepads()[0] may not be the pad they want to use - if I plugged my Nostromo in first, it might not even be a gamepad! :kaomad2:

    Good luck! :kaoluv:
  7. @ATT_Turan I've read through the API and it says that it is supposed to grab the first one connected or the first one to have a button pressed if multiple are connected.
    After testing, I can say that this is not the case, and it defaults to the first one plugged in like you said. Now, I cannot say if that is because I am using a wired controller and a bluetooth controller instead of two wired controllers or not.
    I also cannot find any other method to check controllers to see which one is actively being used barring writing a plugin to check which one has had a button pressed more recently than the others.
  8. @Zael I've purr-ticipated in other convos about how it's supposed to wait for a button press...but doesn't... :kaoswt:

    Switch on when detects a controller
    It could be differences in systems...or NW.js versions...or controllers...it's confusing-nyan.

    The totally most accurate method would prolly be to iterate through the length of getGamepads(), use that to generate a list for Show Conditions, then let the player chew-se which one they want to use. :kaoluv:
  9. I have it figured out.
    I wrote a plugin that tracks if your most recent input was keyboard or gamepad and if it was a gamepad, then it tracks the ID string of the gamepad.
    This will let us store those and use them to dynamically and automatically change the button prompts.
    It is written for MZ, but it should work fine with MV, I think, maybe.
    There's probably a better way to do it, but it works and it doesn't interfere with any of my other stuff. :rheh:
  10. This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.