Skip to content
This repository has been archived by the owner on Aug 15, 2022. It is now read-only.

Decoupling Netcoding (Server and Client Responsibility)

Peter Halasz edited this page May 29, 2019 · 1 revision

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" ;)

Home

Getting Started
Network Contract Wizard (NCW)
Network Object
Remote Procedure Calls (RPCs)
Unity Integration
Basic Network Samples
Scene Navigation
NetWorker
Master Server
Web Server
Netcoding Design Patterns
Troubleshooting
Miscellaneous
Forge Networking Alloy
Steamworks
Clone this wiki locally