Menu
Lumberyard
Developer Guide (Version 1.11)

Tutorial: Getting Started with Multiplayer

This tutorial walks you through the steps to create a simple multiplayer game test level. These steps include binding an entity to the network and connecting a client to the host. At the end of the tutorial, you should have a level with a simple network bound entity that is ready for a multiplayer game.

This tutorial guides you through the following tasks:

  • Create a level and add in new entities.

  • Bind an entity’s transform component to the network.

  • Connect a client to the server and verify network replication.

Prerequisites

This tutorial assumes the following:

  • You have installed Amazon Lumberyard.

  • You have created a game project.

  • Your game project has the Multiplayer gem and the User Login: Default gem enabled. You can enable these gems in Lumberyard's Project Configurator.

Note

This tutorial uses Visual Studio 2013, but you can also use Visual Studio 2015.

Step 1: Creating a Level and Adding a Sphere and a Box

Your first step is to create a level and prepare a simple sphere and box shape so that you can test Lumberyard's networking features.

To create a level, sphere, and box

  1. In the Lumberyard Project Configurator, choose a project that has the Multiplayer Gem enabled, and then click Set as default.

  2. Open Lumberyard Editor, create a level, and give it a name.

    
              Create a level
  3. In the Lumberyard Editor viewport, right-click and select Create new component entity.

  4. With the entity selected, use the Entity Inspector to name the entity CameraEntity.

  5. Click Add Component.

  6. Select Game, Camera to assign a camera component to the entity.

    
              Assign a Camera component
  7. In the Lumberyard Editor viewport, right-click and select Create new component entity.

  8. With the entity selected, use the Entity Inspector to name the entity SphereEntity.

  9. Click Add Component, Shape, Sphere Shape.

  10. In Entity Inspector, click Add Component, Rendering, Static Mesh to assign a static mesh component to SphereEntity.

  11. In Entity Inspector, under Static Mesh, click the next to Static asset.

  12. In the Preview window, click Objects, default, and choose primitive_sphere.cgf.

  13. Click Open.

  14. In the Lumberyard Editor viewport, right-click and select Create new component entity.

  15. With the entity selected, use the Entity Inspector to name the entity BoxEntity.

  16. Click Add Component, Shape, Box Shape.

  17. In Entity Inspector, click Add Component, Rendering, Static Mesh to assign a static mesh component to BoxEntity.

  18. In Entity Inspector, under Static Mesh, click the next to Static asset.

  19. In the Preview window, click Objects, default, and choose primitive_cube.cgf.

  20. Click Open.

    
              Adding a Static Mesh component
    
              Choose the .cgf file
  21. Select the SphereEntity. In Entity Inspector, click Add Component, Physics, Physics.

  22. In Entity Inspector, under Physics, click the + next to Behavior.

  23. In the Class to create dialog box, choose Rigid Body to attach rigid body physics to the component.

  24. Select the BoxEntity, and follow the same steps to attach rigid body physics to it.

  25. In the viewport, move the sphere and box entities above the plane so that they have room to fall.

  26. In Entity Inspector, set Physics, At Rest Initially to false to allow the sphere and box to begin simulating after the level is loaded.

    
              Set At Rest Initially to false

You now have two simple component entities with physics in the level that you created.

Step 2: Binding Sphere Transform Components to the Network

After you create the initial level with the sphere and the box, you bind the sphere’s transform component to the network. This allows clients to replicate the sphere and see changes over the network.

To bind the sphere’s transform to the network

  • Select the sphere entity. In Entity Inspector, click Add Component, Network, Network Binding to add a NetBinding component to it. This allows the host to replicate the transform component of the sphere to all clients.


          Add a Network Binding component

You have now created a server authoritative sphere entity.

Step 3: Connecting a Client to the Server

This step shows you how to connect a client to the server instance and then observe your networked sphere in action.

To connect a client game to the host game

  1. Choose File, Export to Engine, or press Ctrl+E to export your level.

  2. Run the game launcher from the Bin directory that you are using. If you are using Visual Studio 2013, the directory is \dev\Bin64vc120\. If you are using Visual Studio 2015, the path is \dev\Bin64vc140\. The name of your launcher is <your-game-project-name>WindowsLauncher.exe.

  3. Press the ` key (above the TAB key) to open the console.

  4. Run the command map <MultiplayerTutorial> where <MultiplayerTutorial> is the name of the level to load.

    
              Run the map command
  5. Press the ` key to open the console. Run the command mphost to make your client host a network session.

    
              Run the mphost command
  6. Use the launcher to open another game and press the ` key to open the console.

  7. Run the command sv_port 0 to set the client port to 0 and the ephemeral port.

    Note

    On a single computer, only one process is allowed to bind to a particular port. Therefore, to run more than one game process on the same computer (as in this multiplayer sample), you must use ephemeral ports. The sv_port CVar defines the UDP port on the local machine for the multiplayer sample, and the setting of 0 specifies the ephemeral port. This allows two clients on the same computer to talk to each other.

  8. Run the command mpjoin to join to the host game. You should see the sphere synchronized by location on the client. However, the box will be desynchronized and have different locations on the client and host.

    
              Synchronized sphere

Congratulations! You have successfully created a simple networked level. You can now use the Network Binding component to synchronize transforms of entities and connect clients to servers.

You have created a simple networking sample to see the effects of networking in Lumberyard. Now visit the following links to see what else you can add to your game: