Game Development Community

Input for local multiplayer

by Andy S. · in Torque X 2D · 01/22/2010 (3:11 pm) · 10 replies

Hi,

I still did not really get how it works with assigning input to logical players, or how to make sure which player uses which gamepad on Xbox360
In another thread, Henry wrote the following example code:


in game.cs I create CurrentPlayer

view plainprint?
01.public PlayerIndex CurrentPlayer
02. { get; set; }



view plainprint?
01.for (PlayerIndex index = PlayerIndex.One; index <= PlayerIndex.Four; index++)
02.{
03. if (GamePad.GetState(index).Buttons.Start == ButtonState.Pressed)
04. {
05. Game.Instance.CurrentPlayer = index;
06. Game.Instance.ShowMainMenu();
07. }
08.}

or you can use the project with GIU provided with TX.

I understand that this makes sure that the menu is controlled with the current gamepad that the player is using, or that the first player which hits the startbutton gets control in the menu, in case there are two gamepads connected (2 players).
Now say I have a 2 player local game (not networked). Since I now have the playerindex the current player is using, I could use a movementcomponent and pass in this "CurrentPlayer" to give logical player one control over his character. How do I do this for the second player?
Maybe this is a dumb question, but still I did not realyl get the concept of all this. I would be fine with dealing only with playerindex.one, playerindex.two and so on....
Can anyone explain maybe a little or does anybody maybe even have a really simple sample about how I could have a level with two staticsprites, both with a movement component, and the gamepad that pressed first the startbutton in the startcreen will be controlling player1 and the second gamepad will control player2?

Any help is much appreciated.

#1
01/22/2010 (3:51 pm)
The PlayerNumber property of the MovementComponent determines who the controlling Controller is.

The default value for it is 0 which is translated to "gamepad0" by the setupinputmap function call in the _OnRegister with "gamepad" + _playerNumber.

So if in code you have
MovementComponent MoveComp = new MovementComponent();
MoveComp.PlayerNumber = 1;
PlayerObject Player2 = new PlayerObject();
Player2.Components.AddComponent(MoveComp);

would create a MovementComponent that is controlled by controller 2.
#2
01/22/2010 (3:56 pm)
Scott, thanks for the fast reply.
So does this mean that "gamepad0" is always the gamepad that the first player is using in my scenario that I described? I mean, in the example in my post above, "currentpalyer" would always be "gamepad0"?
#3
01/22/2010 (5:19 pm)
You shouldn't rely on it. Basically the safest thing is not to make any assumptions about who will be on which gamepad.
#4
01/22/2010 (5:47 pm)
As duncan said you can't rely on that.

for instance if you have 2 controllers connected and someone starts your game from the start screen with controller 2
then your currentplayer variable will point to gamepad1 (in theory)

It all boils down to the Index's In the example I posted above this it rely's on the MovementComponent using the 1 to make it gamepad1. (which in theory means controller 2)

So that the gamepad is determined seperatly from the movement component and then pass the gamepad string into the component.
#5
01/22/2010 (6:58 pm)
sorry but I still don't get it.
I use the GUI Sampel that comes with TX2D. I added another property to the game class to save the second playerindex. So in game.cs I have these:
public int Player1ControllerIndex { get; set; }
public int Player2ControllerIndex { get; set; }

In the GuiStartscreen, the Player1ControllerIndex is set to whichever controller the gamer uses that hits the startbutton. So now I know during my whole game which controller player1 uses.

Now I need to find out which controller player2 uses. And here is my problem. How do I do this? How can I poll the gamepads to assign the playerindex for player2?

Once I have this, I will create the players characters from templates and want to assign them movement components in code (just like in the splitscreen sample), but I don't know which playerindex player2 has, since I can't assume anything.

Thanks Scott and Duncan for the help and patience with a beginner.
#6
01/22/2010 (8:08 pm)
Ahh ok now I see where the confusion is.

I see 2 scenarios (i'm sure there are more)

1) everyone starts the game at the same time
2) Other players join the game in progress.


In scenario 1:
I would probably have a GUI Screen that works like a start screen but is only polling the other controllers and has upto the number of players that you are going to support in your game and when a start button is pressed on a controller you then take that controller index and assign it to your next available player slot and then create a player object with a movementcomponent assigned to that index.

In Scenario 2:
It gets a little more complicated and can probably be easily solved with Input Maps.
you would create an inputmap with assignments to each controller (connected or not)
IE:
InputMap map = new InputMap();
map.BindAction("gamepad0", (int)XGamePadDevice.GamePadObjects.Start, PlayerOneJoin);
map.BindAction("gamepad1", (int)XGamePadDevice.GamePadObjects.Start, PlayerTwoJoin);
map.BindAction("gamepad2", (int)XGamePadDevice.GamePadObjects.Start, PlayerThreeJoin);
map.BindAction("gamepad3", (int)XGamePadDevice.GamePadObjects.Start, PlayerFourJoin);
InputManager.Instance.PushInputMap(map);

This is an example ( I haven't tested this ) I'm not 100% sure how torque processes input so I don't know if the MovementComponent would override the PlayerOne Mapping.

However you could expand this into a function and push/pop it from the inputmanger as people join/leave with only the inactive controllers using your properties.
#7
01/23/2010 (9:49 am)
Scott, in scenario 1, how would I poll the other controllers? For example, player1 uses gamepad2 when pressing start on the startscreen. Now playerindex.two is saved for player1 in the property Player1ControllerIndex. But it could be as well playerindex.one. How exactely can I poll all controllers except the one that is already assigned to player1 so I can find player2's playerindex?

does it have to be something like:

if (player1controllerindex = 0)
{
poll only controller 1,2,3
}

if (player1controllerindex = 1)
{
poll onlky controller 0,2,3
}

and so on? Is there no more elegant way to do this?
#8
01/23/2010 (12:32 pm)
loop through gamepads
{
if gamepad not already associated with a player then poll it to see if a new player wants to join
}
#9
01/24/2010 (6:21 am)
thanks duncan and scott, it works now ;-)
#10
02/11/2010 (10:34 pm)
I have a question.. Is there a tutorial that shows how to allow multiplayer in platformer x kit? any help would be appreciated.. Thanks