• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

How to Skip a For Loop Check for a Player not Using a Gamepad?

Hello, I have a Async gamepad for loop check that checks for connected gamepads and assigns them to one of four player objects. What I would like to do is for the for loop to skip checking for a gamepad if player 1 is just using a keyboard. Since if I left it the way it is, if player 1 is using a keyboard and a gamepad is connected, that gamepad takes control of player 1, instead of player 2. So I need a way for like, if player one were to choose like a "keyboard" variable, then it somehow tells the for loop to skip assigning a gamepad "pad_index" to player 1.

these are the four global variables for the gamepad slots:

global.Controller_Slot[0] = noone;
global.Controller_Slot[1] = noone;
global.Controller_Slot[2] = noone;
global.Controller_Slot[3] = noone;

Basically, if player 1 wants to only use the keyboard, how do I set it to were the for loop skips "global.Controller_Slot[0]"?

This is the code I'm using:

Async System in o_game:
GML:
// Parse the async_load map to see which event has been triggered
switch(async_load[? "event_type"]) {
  
    // A game pad has been discovered
    case "gamepad discovered":
  
        // Get the pad index value from the async_load map
        var pad = async_load[? "pad_index"];
      
        // find the first player with no assigned gamepad
        for (var i=0; i<k_max_players; i++;) {
          
            if (global.Controller_Slot[i] == noone) {
              
                show_debug_message("Assigned Pad " + string(pad) + " to Player " + string(i));
              
                //assign pad to player and set deadzones
                global.Controller_Slot[i] = async_load[? "pad_index"];
                gamepad_set_axis_deadzone(pad, 0.5);
                gamepad_set_button_threshold(pad, 0.1);
              
                break; // end for
            }
          
        }
      
        break;  // end gamepad connected case

    // Gamepad has been removed or otherwise disabled
    case "gamepad lost":
  
        // Get the pad index
        var pad = async_load[? "pad_index"];
      
        //find the player this pad is assigned to
        for (var i=0; i<k_max_players; i++;) {
          
            if (global.Controller_Slot[i] == async_load[? "pad_index"]) {
              
                //un-assign the pad
                show_debug_message("Disconnected Pad " + string(pad) + " from Player " + string(i));
                global.Controller_Slot[i] = noone;
              
                break; // end for
            }
          
        }

        break; // end gamepad lost case
}
I put the init code in o_game Create:

GML:
// init
#macro k_max_players 4
global.Controller_Slot = array_create(k_max_players);

global.Controller_Slot[0] = noone; // meaning player 0 has no gamepad detected yet
global.Controller_Slot[1] = noone;
global.Controller_Slot[2] = noone;
global.Controller_Slot[3] = noone;
and the gamepad code like so:

GML:
action_one_   = gamepad_button_check(global.Controller_Slot[0], gp_face1);
action_two_   = gamepad_button_check(global.Controller_Slot[0], gp_face2);
action_three_ = gamepad_button_check(global.Controller_Slot[0], gp_face3);
action_four_  = gamepad_button_check(global.Controller_Slot[0], gp_face4);
 
Last edited:
Top