Search code examples
xnamultiplayer

Local multiplayer in XNA


Is there any tutorial or sample on how to create a local multiplayer in an XNA game(Windows)?

I often see tutorials for networking game but never on how to enter a second player. How to ask the second player to sign in, to use the same object but with different controller...

Thanks.


Solution

  • I've not come across any tutorials, but I can outline some basic ideas for you.

    If all of your players will be identical:

    Modify your 'player' objects constructor to have a parameter for some playerIndex. When checking for input inside the player class, use this index. That way, you can create a bunch of 'players' without needing to write individual classes for each. You can then write general-purpose code like:

        if (inputManager.IsMoveRight(playerIndex))
        { 
            // do some stuff etc.
        }
    

    This will require some modifications to your input management structure. You should be aiming to generalise the code you already have.

    If each player will have completely different implementations:

    You'll need to take advantage of polymorphism in this case. If each player should select their character in a menu screen, you'll need to have some attribute in the player class that holds their chosen character (Note: If each player should be a preset default, i.e player 1 is always mario, player 2 is always sonic, it would probably be best to subclass your player class for each player). This attribute could be moved up a layer and stored in a 'controller' class if you wanted.

    Menu logic:

    Handling player sign-ins is really best done before any levels are loaded. I'm going to assume that you don't need players to drop in/out during the game. Basic outline:

    • When the game is in a character selection screen (or just the main menus) check for input from all controllers. If a controller presses 'start', sign them in. This could involve simply adding a playerIndex to an array for example. Then, when the game loads, check the array for active players and spawn any that are found.

    • Character selection can be implemented in very much the same way, although you'd probably want a specific menu sign-in screen. Check for input from active players during the sign in screen and allow them to scroll through and select a character. Store the result somewhere (player or controller class for example).

    Other things to consider:

    • Are getting input from the keyboard as well as gamepads? If this is the case, it might be easier to default the keyboard to player one, and only allow sign in/out from gamepads.

    • I mentioned this before, but you will need to generalise your input management to prevent a major case of spaghetti code. In order for any of this to work, you should not be checking previous/current controller states from directly within your player class. The MSDN Game State Management sample is worth examining as it uses a similar system.

    This isn't a very comprehensive guide, but hopefully I've raised some points for you to consider.