Using InControl in Unity for Local Multiplayer, reconnecting controllers

InControl is an awesome plugin developed by the guys at Gallant Games. What it does is allow super easy configuration of multiple input controllers to give similar outputs, meaning you only have to setup the controls for your game once, rather than for every single different type of controller you would like to use. This allowed us to quickly setup NZA to run with Xbox360, PS3, MOGA ect. controllers with minimum effort.

So InControl  is great, but where it falls short a little bit is when a controller disconnects and then reconnects. Now this is not InControls fault, the problem lies with the fact that controllers don’t have unique identification codes , read more here.

How did I get around this problem? Well if the game was singleplayer there is a simple solution, use InControls built in ActiveDevice. This uses whichever device was last touched to give the output, so as soon as the device reconnects it works again. But I have multiple controllers being used at the same time, so ActiveDevice is no good.

So here’s what I did.

When a new player joins the game, they are assigned an InputDevice to there profile. This device is then also added to a static List<InputDevice> “playerDevices” on the GameController. On the PlayerController an int “deviceID” is also assigned for quick reference to the device in “playerDevices”.
Now thats all setup we create an InputDevice “mInputDevice” on the PlayerController like so:

InputDevice mInputDevice{
        get{
            return GameController.PlayerDevices[deviceID];
        }
    }

Now the PlayerController will always reference the list in the GameController to find it’s device.

Now we just make a small addition to the InputDevice.cs . Adding the following line lets us keep track of which devices are attached.

public bool active = true;

Now we know if a device is active, we add this to the Attach and Detach events:

void DeviceAttached(InputDevice device){
   Debug.Log"Attached: " + device.Name );

   for (int i=0;  iGameController.PlayerDevices.Counti++) {
       InputDevice InD = GameController.PlayerDevices[i];
       if (!InD.active){
          if(InD.Name==device.Name && InD.Meta==device.Meta){
                GameController.PlayerDevices[i]=device;
                break;
            }
        }
    }
}

void DeviceDetached(InputDevice device){
    Debug.Log"Detached: " + device.Name );

    foreach(InputDevice InD in GameController.PlayerDevices){
        if (InD==device){
            InD.active=false;
            break;
        }
    }
}

What this does is set the device to be not active when it is disconnected. Then if a new device is connected with the same name and meta data it will automatically assign it to PlayerDevices list, allowing the player to continue playing!

Obviously if another identical controller is connected before the original one, the controllers will switch. But there is nothing that can be done about that.

If you want to remove a player from the game (they quit) then you can just set their device to null (in the PlayerDevices List). This way it wont effect the other devices.

So that’s working so far for me, feel free to post in the comments if you have a better way of doing this!

2 thoughts on “Using InControl in Unity for Local Multiplayer, reconnecting controllers

  1. Hey! really good explanation of the re-connect way for InControl. It wold be pretty awesome if you guys make a quick tutorial of how to implement the multi-player code in c#!

    Thanks!

    1. Hi Isac,
      I’m afraid we’ve moved on from inControl and now we use Rewired. InControl couldn’t support 8 controllers on windows so we had to ditch it (well incontrol could, XInput couldn’t…).
      Maybe I’ll do one for rewired, the codes pretty similar :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>