-
-
Notifications
You must be signed in to change notification settings - Fork 307
Decoupling Netcoding (Server and Client Responsibility)
When networkObjects become very big, there is the issue of bloat, and hence, it is hard to understand what part of the code falls to the responsibility of the server, client and/or both.
Splitting the code in a readable way, which seperates the responsibilities of: server/client/both, helps a lot in more than debugging ;)
This responsibility-split can only be achieved by using events, so it is suggested you look on Decoupling Netcoding (Events) beforehand, because it uses what is shown there, and hence, it will be easier to understand this example if you have understood that one!
Below you see a custom NetworkBehavior which has an RPC called ExampleRPC
using System;
public class CustomExampleNetworkBehavior : ExampleNetworkBehavior
{
// An action is simply an event with extra steps.
// In practice, it is a combination of
// an event and a delegate. But simply put,
// it is used for subscribing methods/functions.
public Action<RpcArgs> OnExampleRPC;
// This is automatically called when the networkObject
// is initialized in the network and ready to work!
protected override void NetworkStart ()
{
base.NetworkStart();
RegisterEventsServerClient();
if (networkObject.IsServer)
RegisterEventsServer();
else
RegisterEventsClient();
}
//Subscribe to functions depending if we are the server or client
// For Server
public virtual void RegisterEventsServer ()
{
OnExampleRPC += OnExampleRPC_Server;
}
// For Client
public virtual void RegisterEventsClient ()
{
OnExampleRPC += OnExampleRPC_Client;
}
// For Server&Client, which means for everyone
public virtual void RegisterEventsServerClient ()
{
OnExampleRPC += OnExampleRPC_ServerClient;
}
//The functions below are called depending if we are server or client
private void OnExampleRPC_Server (RpcArgs pArgs)
{
// Your code for the server here....
}
private void OnExampleRPC_Client (RpcArgs pArgs)
{
// Your code for the client here....
}
private void OnExampleRPC_ServerClient (RpcArgs pArgs)
{
// Your code for server and client here....
}
// This RPC, will call whatever is subscribed to OnExampleRPC
public override void ExampleRPC (RpcArgs pArgs)
{
if (OnExampleRPC != null)
OnExampleRPC(pArgs);
}
}
Of course, the code ends up a lot bigger, but you can use the above as a template for every new networkObject!
So, the code will always be clean and easily readable, allowing you to skip parts you don't care about. In simpler words, it is bigger in code, but smaller to read than "normal" ;)
Getting Started
Network Contract Wizard (NCW)
Remote Procedure Calls (RPCs)
Unity Integration
Basic Network Samples
Scene Navigation
Master Server
Netcoding Design Patterns
Troubleshooting
Miscellaneous
-
Connection Cycle Events
-
Rewinding
-
Network Logging
-
Working with Multiple Sockets
-
Modify Master and Standalone servers
-
NAT Hole Punching
-
UDP LAN Discovery
-
Offline Mode
-
Ping Pong
-
Lobby System
-
Upgrading Forge Remastered to Develop branch or different version
-
Forge Networking Classic to Remastered Migration Guide
-
Script to easily use Forge Networking from sources
-
Run Two Unity Instances with Shared Assets for Easiest Dedicated Client Workflow