• 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!

GameMaker Setting up a local multiplayer lobby with 4 different gamepads

R

Rhatos

Guest
I've been trying to work this out for a while and I just can't seem to get it.

What I'm asking is how do you assign a controller to a "player number" when they press A.
Example: 4 friends are playing, Friend A pressed A and his controller is now assigned to player 1, Friend B pressed A and his controller is now assigned to player 2, and so on.

I know I have to use arrays and loops. But I can't figure it out exactly.

I appreciate any help.
 

TechBoy397

Member
This is a good tutorial to start out with: https://gdpalace.wordpress.com/2018/02/26/splitscreen/. From this you can easily have a player spawned when pressing A and assigning it. The controller assigning thing I had looked into as I wanted it for my game, but there's nothing the controller communicates to gms2 I could find that could help identify each individual controller, instead forcing me to keep them in a list and change the list when new ones are added or removed.
 
Friend A pressed A and his controller is now assigned to player 1, Friend B pressed A and his controller is now assigned to player 2, and so on.
Controllers map themselves automatically when they power on (in the order they powered on). If you wanted players to specifically choose their player number, you'd have to write your own mapping system in GML. Also xbox 360 controllers actually show you which player number you are, and if you re-order the player number in your code, this will not line up with the indicator.

If this is really what you're looking for (and isn't just a misunderstanding of how the gamepad code works in GML), you could use a map to "reroute" the gamepad id.

Create
GML:
// Create event
gamepadMap = ds_map_create(); // Map of gamepadID -> assigned gamepadID
Step
GML:
// If there are still players that haven't assigned themselves yet
if ds_map_size(gamepadMap) < gamepad_get_device_count() // ** Change gamepad_get_device_count() to how many players you expect there to be
    {
    // Loop through all gamepads
    var i = 0;
    repeat gamepad_get_device_count()
        {
        // If this gamepad is on
        if gamepad_is_connected(i)
            {
            // If they pressed A
            if gamepad_button_check_pressed(i,gp_face1)
                {
                // If they hadn't already been assigned
                if is_undefined(ds_map_find_value(gamepadMap,i))
                    {
                    // Assign them to the next available player number
                    ds_map_add(gamepadMap,i,ds_map_size(gamepadMap));
                    }
                }
            }
      
        // Next gamepad to check
        i++;
        }
    }
else // Now that everyone is assigned
    {
    // If the internally assigned player 1 presses A
    if gamepad_button_check_pressed(0,gp_face1)
        {
        // Do whatever
        }
  
    // If YOUR assigned player 1 presses A
    if gamepad_button_check_pressed(ds_map_find_value(gamepadMap,0),gp_face1)
        {
        // Do whatever
        }
    }
 
Last edited:
I'm sure Rhatos either found a solution or moved on to something else in the three years since he posted this.
Yeah I noticed this after I replied, but presumably @TechBoy397 had a similar question, found this topic through a google search and wanted to pass on what he found! So for those following a similar path in the future, I offer my solution too - at least that's how I'm going to justify it to myself.
 

woods

Member
it really sucks when you have an issue and start googling for fixes and find hundreds of outdated threads with the same issue... not resolved.

i for one appreciate the three year late reply if its relevant and can help someone(like me) down the road

edit:
what is the official cutoff time to deem something as being a necro if it is still relevant? ;o)
 
Top