Building on "Hello World"
In this guide we will build on the work we have already done in Hello World and add in a few more features, we will be covering the following:
- Adding scripts to your objects
- Adding editor modes inside your game (Host Server and Client)
- Basic Player Movement
- Permissions
- Basic RPC use
#
RequirementsThis tutorial requires an MLAPI-supported version of Unity (2019.4+).
We recommend that you complete the Your First Networking Game "Hello World" guide before starting this one.
#
Adding Scripts to Hello WorldThis section will add some scripts to Hello World which will contain the new features we will be covering in the tutorial.
Click the Assets folder.
Create a new Folder and call it Scripts.
Create an empty
GameObject
rename it HelloWorldManager.Create a script called
HelloWorldManager
.Add the
HelloWorldManager
script as a component.Open the
HelloWorldManager.cs
script.Edit the
HelloWorldManager.cs
script to match the following.
tip
You can copy the script from here and paste it into your file.
- Select the code sample.
- Click Copy in the top right corner.
- Paste it into your code editor.
Click to show/hide the Code.
#
Adding Editor Modes to Hello WorldInside the HelloWorldManager.cs
script, we define two methods which mimic the editor buttons inside of NetworkManager during Play mode.
Click to show/hide the Code.
NetworkManager
implements the singleton pattern as it declares its singleton named Singleton
. This is defined when the MonoBehaviour
is enabled. This component also contains very useful properties, such as IsClient
, IsServer
, and IsLocalClient
. The first two dictate the connection state we have currently established that you will use shortly.
We call these methods inside of OnGUI()
.
Click to show/hide the Code.
note
You will notice the introduction of a new method, SubmitNewPosition()
; which we will be using later.
#
Adding basic movement to the Player objectThis script adds some basic movement to the Hello World player.
- Create a new script
HelloWorldPlayer
. - Open the
HelloWorldPlayer.cs
script. - Edit the
HelloWorldPlayer.cs
script to match the following.
Click to show/hide the Code.
- Select the Player prefab.
- Add the script
HelloWorldPlayer
script as a component.
This class will inherit from NetworkBehaviour
instead of MonoBehaviour
.
Click to show/hide the Code.
Inside this class we now define a NetworkVariable
to represent this player's networked position.
Click to show/hide the Code.
#
Introducing permissionsIn the HelloWorldPlayer.cs
script we introduce read and write permissions on a NetworkVariable
. For the purposes of this demo, the server will be authoritative on the NetworkVariable
representing position. All clients are able to read the value, however.
HelloWorldPlayer
overrides NetworkStart
.
Click to show/hide the Code.
Any MonoBehaviour
implementing NetworkBehaviour
can override the MLAPI method NetworkStart()
. This method is fired when message handlers are ready to be registered and the networking is setup. We override NetworkStart
since a client and a server will run different logic here.
note
This can be overriden on any NetworkBehaviour
.
On both client and server instances of this player, we call the Move()
method, which will simply do the following.
Click to show/hide the Code.
#
Some simple RPC useIf this player is a server-owned player, at NetworkStart()
we can immediately move this player, as suggested in the following code.
Click to show/hide the Code.
If we are a client, we call a ServerRpc
. A ServerRpc
can be invoked by a client to be executed on the server.
Click to show/hide the Code.
This ServerRpc
simply sets the position NetworkVariable
on the server's instance of this player by just picking a random point on the plane.
Click to show/hide the Code.
The server instance of this player has just modified the Position NetworkVariable
, meaning that if we are a client, we need to apply this position locally inside of our Update loop.
Click to show/hide the Code.
We can now go back to HelloWorldManager.cs
and define the contents of SubmitNewPosition()
.
Click to show/hide the Code.
Whenever you press the GUI button (which is contextual depending on if you are server or a client), you find your local player and simply call Move()
.
You can now create a build which will demonstrate the concepts outlined above.
tip
Make sure SampleScene is included in BuildSettings.
One build instance can create a host. Another client can join the host's game. Both are able to press a GUI button to move. Server will move immediately and be replicated on client. Client can request a new position, which will instruct the server to modify that server instance's position NetworkVariable
. That client will apply that NetworkVariable
position inside of it's Update() method.
Congrats!
Congratulations you have learned the basics of a networked game
Special Thanks
This guide would not have been possible without the hard work and support of Fernando Cortez, Unity.