Game Development Community

Handling controller disconnect.

by Matthew Hoesterey · in Torque X 2D · 07/10/2010 (10:57 am) · 7 replies

I'm having an issue with handling controller disconnects. When playing 4 players if I disconnect multiple controllers at once and then reconnect them before the input maps are initialized 2 controllers will sometimes end up controlling the same characters. Sadly this is very hard to repro as it doesn't happen very often.

So the way I'm handling controller setup is:
When player 1 presses start I set a _p1Gamepad property equal to the controller id that hit start.

When any other players join by pressing start their _pXGampad variable is set equal to the controller id that hit start. I then use these variables to determine what player is navigating menus and to set up input maps to control the characters.

I'm not sure why disconnecting multiple controllers would mess the above methodology up. Do controllers change ids when they are reconnected? If so how exactly should I be handling that?

Thanks :)

#1
07/12/2010 (6:21 am)
Hey Matthew,

the controllers do not change Id. Your problem is caused by some racing condition or some coding error: how do you manage the disconnection/reconnection? The InputMap doesn't really suffer from that (see _SignalButtonState in XInputDevice.cs). The way to detect if a device is disconnected is to check the property HasBeenDisconnected and change your game state accordingly, just waiting for the property IsConnected to become true again or prompting the user to press start on a different controller, but in this case you have to unbind the previous InputMap for the disconnected controller still has that binding active.

Cheers,
Pino
#2
07/15/2010 (1:55 pm)
Thanks Pino. I don't think I'm handing that properly now then. Thanks for the info. :)
#3
07/20/2010 (2:59 am)
I think I have most the disconnect/reconnect code worked out.

I'm having one problem. The "please reconnect your controller" message automatically pops up when I disconnect player 1s controller but not the other players controllers.

Talking to the guys on the XNA forums they mentioned that :

"The Xbox "please reconnect your controller" message pops up without any action from you. In fact, you have absolutely no control over that message (either displaying it or making it go away). It runs separately from your game."

Can you guys think of any reason why this message wouldn't show up when a controller is disconnected? The disconnect IS detected by my code and everything else seams to be working. I just can't get that xbox message to display. Thanks!

#4
07/20/2010 (11:26 am)
That message is shown by the framework only to the main account controller (often this is just the last one logged in). Via XNA there is no control on framework messages.

There are no other info available for XBLIG developers about this message so just ignore it ;)
#5
07/20/2010 (2:02 pm)
Ah, I'll have to roll my own message then :) Thanks man.
#6
07/22/2010 (2:25 am)
Heya, I think I found a bug in the engine. Wanted to run it by you before I submitted.

In PlayerManager.cs The "_players" list is added to the first time you create players input maps. Thing is if you exit your level and have players rejoin (so that player 2 could become the new player 3 and player 3 could become the new player 2 ect...) The old input maps are still held as they are never disposed.

So when you reenter the game 2 controllers will control the same character as the manager binds the controller to an input map that already has a different controller (if that makes sense)

To fix this when I unload my level I created a function in PlayerManger that clears the player data.

public void ClearPlayers()
        {
            _players.Clear();
        }

I have a feeling I found an edge case as I'm guessing most peoples games would not ever have players switching their player number as you do in a fighting game.
#7
07/22/2010 (3:00 pm)
Good catch! I'll see into that to make a permanent general fix.