• Hey! Guest! The 40th (!!!) GMC Jam will take place between February 25th, 12:00 UTC to March 1st 12:00 UTC. Why not join in this very special anniversary jam! Click here to find out more!

GMS 2 gamepad not working gms2 (SOLVED)

KPJ

Member
Hi everyone. i have an issue with my gamepad. I have it connected, and it works if I play games with it. It also shows up on my control panel. however, game maker isn't recognizing it.

In my player step event, I have:

if (gamepad_is_connected(0))
{
instance_destroy();
}

however my player isn't destroying. Any help? thanks!
 
I

IzzoriousAxel

Guest
Code:
var numPads= gamepad_get_device_count();
for(var i = 0; i < numPads; i++;)
{
    if(gamepad_is_connected(i)) show_message(string(i) + ": " + gamepad_get_description(i));
}
Or draw it as text to the GUI, see what ID your gamepad is, but be aware that the ID can change, you shouldn't hardcode the value.
 

KPJ

Member
TheSly, it is an Xbox 360 wired controller
obscene, sure ill try it out!
IzzoriousAxel, i tried it, but nothing happens
 

KPJ

Member
Okay! So i got somewhere. Here is my code:
Code:
//DRAW GUI
var gp_num = gamepad_get_device_count();
for (var i = 0; i < gp_num; i++;)
{
if gamepad_is_connected(i)
    {
    draw_text(32, 32 + (i * 32), gamepad_get_description(i));
    }
else
    {
    draw_text(32, 32 + (i * 32), "No Gamepad Connected");
    }
}
However, this shows up (top left):

Anyone know whats going on?
 

Attachments

KPJ

Member
It worked! The first index of the twelve gamepads said XBox controller, so I put 1 instead of 0 as the device and it worked perfectly! Thanks to everyone!
 

NightFrost

Member
Well, your XB controller is showing up on device count 1, for some reason or another. Assuming you didn't cut off any lines. When you want to detect gamepad(s), things like this is why you want to go through the entire device count instead of assuming any numbers. Unless you want to create some fancy gamepads interface, I suppose saving the ID of the first one you find and using that for all controls is enough.
 
I

IzzoriousAxel

Guest
You're at the mercy of the OS here, the controller ID can't be static or it'll randomly break because the ID changed.

Edit: Thinking about it, I would save the description of the last known controller, and on startup search for it via
Code:
for(var i = 0; i < gamepad_get_device_count(); i++)
{
    if(gamepad_get_description(i) == lastKnownControllerDescription)
    {
        controllerID = i;
        break;
    }
}
This is what I'm doing in my own code now. I also set up a quick n ugly GUI menu to allow manual switching. (attached screenshot) Clicking on those buttons should set the last known controller variable, and if gamepad input is detected, that value should be written to a config file that's read in at startup to use in the search above.
 

Attachments

Last edited by a moderator:

Walky

Member
Keep in mind that if you do this only on startup and the ID changes during gameplay you'll lose the gamepad connection (not sure if it's possible, but I guess changing the gamepad's USB port could cause this).
 

NightFrost

Member
Keep in mind that if you do this only on startup and the ID changes during gameplay you'll lose the gamepad connection (not sure if it's possible, but I guess changing the gamepad's USB port could cause this).
Yes, a good method is described in this YYG blog post which uses an async system event to poll for gamepad state changes. It has other stuff explained too, but all you need to take away from it for detection is to:
Code:
// CREATE EVENT
Port_Count = gamepad_get_device_count();
Game_Pad = array_create(Port_Count, false);

// ASYNC SYSTEM EVENT
// If gamepad connection async event fires, mark the port as connected.
if(async_load[? "event_type"] == "gamepad discovered"){
    Game_Pad[async_load[? "pad_index"]] = true;
    gamepad_set_axis_deadzone(async_load[?"pad_index"], 0.05);
    gamepad_set_button_threshold(async_load[? "pad_index"], 0.1);
}
// If gamepad disconnection async event fires, mark the port as not connected.
else if(async_load[? "event_type"] == "gamepad_lost"){
    Game_Pad[async_load[? "pad_index"]] = false;
}
And then pick a gamepad from Game_Pad array. For example if you just want to pick up the first ID, run on every frame:
Code:
Pad = -1;
for(var i = 0; i < Port_Count; i++){
    if(Game_Pad[i] == true) Pad = i;
    break;
}
 
I

IzzoriousAxel

Guest
Code:
//First room's creation code
global.lastKnownController = "";

ini_open("config.ini");
if(!ini_key_exists("Global", "LastKnownController")) ini_write_string("Global", "LastKnownController", "");
else global.lastKnownController = ini_read_string("Global", "LastKnownController", "");
ini_close();
Code:
//Controller manager object - Create event
var player = instance_find(obj_player, 0); //Or a loop through all obj_player instances for games with more than one player

//Try to find saved controller
for(var i = 0; i < gamepad_get_device_count(); i++)
{
    if(gamepad_get_description(i) == global.lastKnownController)
    {
        player.controllerSlot = i;
        break;
    }
}
Code:
//Controller manager object - Async System event
switch(async_load[? "event_type"])
{
    case "gamepad discovered":
        var player = instance_find(obj_player, 0);
        var pad = async_load[? "pad_index"];
        if(gamepad_get_description(pad) == global.lastKnownController) player.controllerSlot = pad;
        break;
}
Code:
//Button object step event
//~snip~
var player = instance_find(obj_player, 0);
player.controllerSlot = action; //The controller menu object creates instances of the buttons and sets their text and action, action is the controller ID
ini_open("config.ini");
ini_write_string("Global", "LastKnownController", gamepad_get_description(action));
ini_close();
//~snip~
Code:
//Player object step event
//-1 indicates the keyboard in my system
//Since this system starts the controller slot at -1, if the user chooses a slot that has nothing connected, it'll fall through to using the keyboard the next time the game is opened
usingController = controllerSlot != -1;
if(usingController)
{
    //Check analog stick
    if(abs(gamepad_axis_value(controllerSlot, gp_axislh)) > analogStickDeadzone) stickX = gamepad_axis_value(controllerSlot, gp_axislh);
    //etc
This is how I set my system up if it helps.
 
Last edited by a moderator:

LawH

Member
I made a similar piece of code and my xbox type controller is not recognized. Then I tried this and still on all 12 slots it says no controller connected. Any input on this problem? The controller works fine outside gamemaker.
 
Top