Basic Handling of Multiple Controllers
One of the largest issues with XBLCG submissions is the inability to use any controller with a game. This seems odd to me because it’s such a simple thing to fix and is something that every game should support. I should be able to pick up any controller and operate your game given that I started with that controller. Yet many games only work with controller one.
So I’m here to show you a primitive way to get started making this experience better in your game. The code you need is rather dead simple so I’ll just paste it all here:
public enum LogicalGamerIndex
{
One,
Two,
Three,
Four
}
public static class LogicalGamer
{
private static readonly PlayerIndex[] playerIndices =
{
PlayerIndex.One,
PlayerIndex.Two,
PlayerIndex.Three,
PlayerIndex.Four,
};
public static PlayerIndex GetPlayerIndex(LogicalGamerIndex index)
{
return playerIndices[(int)index];
}
public static void SetPlayerIndex(
LogicalGamerIndex gamerIndex,
PlayerIndex playerIndex)
{
playerIndices[(int)gamerIndex] = playerIndex;
}
}
How’s that for basic? I just created a new enumeration for the logical idea of a gamer and made a static class that uses an array to set and get PlayerIndex values for any of the LogicalGamerIndex values. What does this do for us? Let’s take a look.
So what most games do (and is very much an acceptable solution) is to present the user with a start screen (like the ones that have the title and say “Press Start To Continue”). The point of these (besides showing off any licensed tech you have, legal text, and a cool title logo) is that you can detect the start button press from any controller and then use that controller as the primary game pad for the entire game.
What we can do with this LogicalGamer class, then, is something like this:
for (int i = 0; i < 4; i++)
{
if (GamePad.GetState((PlayerIndex)i).Buttons.Start == ButtonState.Pressed)
{
LogicalGamer.SetPlayerIndex(LogicalGamerIndex.One, (PlayerIndex)i);
// move on to main menu
}
}
Now we’ve detected the primary controller, set that in the LogicalGamer class, and moved on to the main menu for the game. So now all you have to do is make sure that when you query for any input states, you use the LogicalGamer class. So instead of simply having
GamePadState gps = GamePad.GetState(PlayerIndex.One);
we would now have
GamePadState gps = GamePad.GetState(LogicalGamer.GetPlayerIndex(LogicalGamerIndex.One));
And that’s it for a single player game. The LogicalGamer class supports up to four players as one would expect so this should scale just fine for fully multiplayer games.
While this adds a bit more to the code (as you see above) it lets you still have a logical idea of “this is player 1″ without directly coupling that with “this is physical controller 1″. So take this code, work with it, bend it, change it, adapt it, and use it so that your games can be even better in XBLCG.
I am glad to see you posting again. Welcome back, brother.
That’s a great functionality. Now I should get myself an XBox360 and try that thing. Anyway, I’m glad to see you are still here!
Super helpful… thanks!