GamePad index becomes incorrect after disconnecting another GamePad whose index was lower (and was connected first)

Hi everyone!

I’m new to this forum. My name is Rafael and I’ve been using MonoGame for the last 6 months.
I’ve recently decided to create a 2D engine and I’m on the InputManager part where I manage gamePads connected and assign them to player slots.

While testing my engine, I came up with something strange that I don’t know exactly if anyone else had experienced it before. I am using Windows 10 and the OpenGL cross platform project of MonoGame 3.7, also, to test I am using two identical XboxOne controllers via USB.

The steps to reproduce this issue are very easy:

  1. Connect gamePad A, monogame will assign it to index 0.
  2. Connect gamePad B, monogame will assign it to index 1.
  3. If you disconnect gamePad A (the one at index 0), you will notice that monogame still remembers correctly that gamePad B is at index 1, but upon connecting gamePad A again, it is not assigned to any slot, and gamePad B becomes both index 1 (as it was) and now also index 0.

Ideal behavior: It should be expected that upon connecting a gamePad, it either gets the first empty slot (in that case index 0 because it was free), or add one to the highest index being used (in that case, would be assigned to index 2). What baffles me not even the fact that the newly connected controller didn’t had an index assigned to it, but that the controller that remained connected (index 1, gamePad B), gets to have index 0 too as soon as the new gamePad is connected.

Remarks: This issue does not happen if its the other way around. For example, if you have a gamePad connected as slot 0, and connect another one at slot 1, and disconnect and connect again the slot 1 controller as many times as you need, the slot 1 will always be assigned to slot 1, and the previous controller will always stay at slot 0 (as expected).

What would be a possible solution to this issue?

Thanks!!

Summary: It a gamePad with a LOWER index is disconnected and connected again while another gamePad with a HIGHER index exists, the newly connected gamePad does not have an index assigned to it, and the gamePad with HIGHER index respondes to both index 0 and 1.

That actually makes sense because if a GamePad is unplugged, indeed, it should be removed from the list. About the only solution I can think of is to store the references to the GamePad objects somewhere and routinely check if they’re still in the list.