Tutorial: Getting Started with Multiplayer
This tutorial walks you through the steps to create a simple multiplayer test game 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 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 installed Amazon Lumberyard.
-
You created a game project. For more information, see Creating and Switching Game Projects.
-
Your game project has the Multiplayer gem. You can enable the gem in Lumberyard's Project Configurator. After enabling the gem, build your project.
Note
This tutorial uses Visual Studio 2013, but you can also use Visual Studio 2015.
Topics
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
-
In the Lumberyard Project Configurator, select a project that has the Multiplayer gem enabled, and then click Set as default.
-
Open Lumberyard Editor, create a level, and type a name.
-
In the Lumberyard Editor viewport, right-click and choose Create entity.
-
With the entity selected, click Tools, Entity Inspector. Use the Entity Inspector to name the entity CameraEntity.
-
Click Add Component.
-
Select the Camera component to attach it to the entity.
-
In the Lumberyard Editor viewport, right-click and choose Create entity.
-
With the entity selected, use the Entity Inspector to name the entity SphereEntity.
-
In the Entity Inspector, click Add Component, Rendering, Mesh to attach a Mesh component to the SphereEntity.
-
In the Mesh component, click the (…) icon next to Mesh asset.
-
In the Pick Static Mesh window, expand Engine, Objects, default, and select
primitive_sphere.cgf
. -
Click OK.
-
In the viewport, right-click and choose Create entity.
-
With the entity selected, use the Entity Inspector to name the entity BoxEntity.
-
In the Entity Inspector, click Add Component, Rendering, Mesh to attach a Mesh component to the BoxEntity.
-
In the Mesh component, click the (…) icon next to Mesh asset.
-
In the Pick Static Mesh window, under Engine, Objects, default, select
primitive_cube.cgf
. -
Click OK.
-
In the viewport, select the SphereEntity. In the Entity Inspector, click Add Component, Physics, Rigid Body Physics.
-
Select the BoxEntity. In the Entity Inspector, click Add Component, Physics, Rigid Body Physics.
-
Select the SphereEntity. In the Entity Inspector, click Add Component, Physics, Mesh Collider.
-
Select the BoxEntity. In the Entity Inspector, click Add Component, Physics, Mesh Collider.
-
In the viewport, move the sphere and box entities above the plane so that they have room to fall.
To move the sphere and box entities above the plane
-
In the viewport, select the entity.
-
On the EditMode toolbar, choose the Move
icon.
-
On the EditMode toolbar, choose the Constrain to Z Axis
icon.
-
In the viewport, drag the item up vertically.
-
-
In the Entity Inspector, Rigid Body Physics component, ensure that At rest initially is unchecked. This allows the sphere and box to begin simulating after the level is loaded.
You now have two simple component entities with rigid body 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 the sphere's changes to be replicated over the network to clients.
To bind the sphere’s transform to the network
-
Select the sphere entity.
-
In the Entity Inspector, click Add Component, Networking, Network Binding. Adding the NetBinding component to the entity allows the host to replicate the Transform component of the sphere to all clients.
-
With the sphere entity selected, in the Transform component, ensure that the Network Sync, Sync to replicas property is enabled.
You have now created a server authoritative sphere entity that enables changes to the sphere to replicate over the network. However, you didn't bind the box to the network, so changes in the box will remain unreplicated.
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
-
In Lumberyard Editor, choose Game, Export to Engine, or press Ctrl+E to export your level.
-
Run the game launcher from the
Bin
directory that you are using. If you are using Visual Studio 2013, the directory is
. If you are using Visual Studio 2015, the path islumberyard_version
\dev\Bin64vc120\
. The name of your launcher islumberyard_version
\dev\Bin64vc140\
.your-game-project-name
Launcher.exe -
Press the ` key (above the TAB key) to open the console.
-
Run the command map
MultiplayerTutorial
whereMultiplayerTutorial
is the name of the level to load. -
Press the ` key to open the console. Run the command mphost to make your client host a network session.
-
Move the first launcher window to one side so that you will be able to see the second launcher window. Use the launcher again to open a second instance of the game. Press the ` key to open the second console.
-
Run the command sv_port 0 to set the client port to
0
(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
console variable defines the UDP port on the local machine for the multiplayer sample, and the setting of0
specifies the ephemeral port. This allows two clients on the same computer to talk to each other. -
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.
You have successfully created a simple networked level. You can now use the Network Binding component to connect clients to servers and synchronize transforms of entities.
Related Tasks and Tutorials
You have created a simple networking sample to see the effects of networking in Lumberyard. See the following to learn more about what else you can add to your game: