Handling multiple joypads - mainly, detecting when a joypad is connected during runtime

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bizt

I have a football game and it can support a maximum of two joypads per game (one for each team). So, have have created the following input maps: “joypad_0_left”, “joypad_1_left”, “joypad_0_right”, “joypad_1_right”, etc … each of these map to device 0, or device 1.

When I have both joypads connected, I have the following returned from Input.get_connected_joypads():

[0,1]

So this works as I have both joypad_* input maps set to each of these devices.

However, I was the ability to be able to detect when a controller is connected whilst the game is running so I added the following code:

func _on_joy_connection_changed(device_id, connected):
  update_connected_joypads()
  print(Input.get_connected_joypads())

Now I see this:

[0,1,2]

The second controller doesn’t work, perhaps as it’s been mapped to device 2(?) and I don’t have any input map for that device

But, if I restart the game with both controllers already connected, for Input.get_connected_joypads() I see:

[0,1]

Any idea how I might be able to handle this, as it would be good to be able to detect when a player plugs in a second controller during the game (e.g. select controller screen, realise only one controller is connected, plug in the second one and use it without having to restart)