Netcode for GameObjects: ClientRpc Not Executing on All Clients After ServerRpc Call

Hello everyone,

I’m working on a multiplayer game using Unity’s Netcode for GameObjects where players can edit terrain in a procedurally generated world hosted by one of the players. The editing functionality works great on a single client, but I’m facing challenges syncing these edits across all connected clients.

My initial approach was straightforward: when a player wants to make an edit, they send a ServerRpc to the host with the details of the edit (position and type). Subsequently, the host should broadcast this information to all clients using a ClientRpc, which would then execute the edit locally on each client, thus keeping the terrain state consistent across the game.

Here’s the process I intended to implement:

  • Player sends a ServerRpc to the server.
  • Server processes the request.
  • Server sends a ClientRpc to all clients with the edit details.
  • All clients receive the ClientRpc and perform the terrain edit locally.

However, the issue arises after the ServerRpc is called: the ClientRpc seems to only execute on the client that initiated the ServerRpc call. Other clients ignore the ClientRpc, failing the if (!IsOwner) return; check.

This behavior does not align with my understanding of the documentation, which suggests that a ClientRpc should execute on all clients. I’m beginning to suspect there’s a fundamental concept I’ve misunderstood about the ownership and execution flow of ClientRpc and ServerRpc methods.

Below is the code snippet illustrating the logic for the terrain editing and networking calls:

Code example

public class TerrainNetworkLogic : NetworkBehaviour
{
    private myBrush brush;
    private TerrainEditorUpdater updater;
    private LayerMask terrainLayer;
   
    public void Setup(myBrush b, TerrainEditorUpdater u, LayerMask t)
    {
        brush = b;
        updater = u;
        terrainLayer = t;
        DebugController.Log("TerrainEditor components set on TerrainNetworkLogic " + brush + " " + updater + " " + terrainLayer);
    }

    private void Update()
    {
        if (!IsOwner) return;
       
        if (Input.GetKeyDown(KeyCode.T))
        {
            TestGroundInteraction();
        }
    }

    [Command] // command that can be called from console
    private void TestGroundInteraction()
    {
        if (IsOwner)
        {
            DebugController.Log("TestGroundInteraction called");

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out RaycastHit hit, 1000, terrainLayer))
            {
                Vector3 pos = hit.point;
                DebugController.Log("Hit terrain at " + pos);
                EditTerrainServerRpc(pos, BrushType.Raise); // call the serverrpc so that this terrain edit can be synchronised by the server
            }
        }
    }

   
    [ServerRpc]
    private void EditTerrainServerRpc(Vector3 pos, BrushType brushType)
    {
        DebugController.Log("EditTerrainServerRpc called test " + OwnerClientId + " " + pos + " " + brushType);

        testClientRpc(pos, BrushType.Raise); // simply forward this information too all clients, so the terrain edit can happen in everyones world
    }

   
    [ClientRpc]
    private void testClientRpc(Vector3 pos, BrushType brushType)
    {
        if (!IsOwner) return;
       
        DebugController.Log("testClientRpc called " + pos + " " + brushType + OwnerClientId);
        PerformTerrainUpdate(pos, brushType);
    }
   
   
   
    // a local method
    private void PerformTerrainUpdate(Vector3 pos, BrushType brushType)
    {
      
        var tmp = brush.preset; // this is never run because it's not owner
       
        ...
   
        DebugController.Log("Performed " + brushType + " at " + pos);
    }
}

Has anyone encountered a similar issue, or can anyone spot what I might be doing wrong here? I’m looking for any advice or suggestions that could help me understand this problem better and, hopefully, resolve it.

Thank you in advance for your time and help!

What networking framework?

Netcode uses [Rpc] attribute if you work with the latest 1.8 (or even earlier versions).
Within the attribute you specify who receives the Rpc call.

Anyhow, if you do (!IsOwner) in a client Rpc that all clients should process, then you’ve misunderstood the ownership concept: there can be only one! (cue music) :wink:

If this script is on a terrain modifier object and the server owns this terrain object, then all clients will early out in that method. Otherwise only a single client owns the object with the script on, and only that client will not early out. Depending on the network framework, the owner may refer to who sent the Rpc but the result is the same.

With NGO you can specify who to send the Rpc to, here this should be ClientsAndHost.

Hi and thanks for answering,

The script is a script attached to all players, however this script only has its references set for the owner, I suspect this is related to why I get null references when I don’t owner check the client rpc.

The ClientsAndHost declaration, I’m a bit confused, because as I understand it, that is the default setting for the rpcs isn’t it? Why would setting that change anything?

How do I use that Enum? Is it part of the SendParams or? I cant find an example of where/how it is used.

I like to specify it explicitly because I want to read it in the code, not having to know/remember what it does implicitly. Hence I don’t even know what the default is to be honest. :wink:

The 1.8 release of Netcode adds the plain [Rpc] attribute with parameters.
You may want to consider upgrading because it makes working with Rpc easier and more flexible. Specifically you can specify that an Rpc is sent only to the object’s Owner. The methods also only need to end in “Rpc” now.

Thank you very much for the insight, I upgraded to 1.8.1, I too prefer this way of working it is more explicit. I managed to fix my issue. And I would love to know if my thinking of this fix is correct or not, I don’t want to run around with wrong assumptions. Let me split my thinking into 3 steps to better explain it:

Step 1: Initial RPC Call

  • Client 1 sends an RPC to the server requesting a terrain edit.
  • Client 1 has a reference to the network object (in this case, a brush) that sends the RPC.

Step 2: Issue with Client-Side Execution

  • The server attempts to send the RPC to all clients to perform the terrain edit.
  • Client 1 can execute the edit because it has the correct reference to the brush on Network Object 1.
  • Other clients fail to execute the edit because they don’t have the correct reference to the brush. The reference they hold points to a different instance or object, which doesn’t match Network Object 1.

Step 3: The Fix

  • The server sends RPCs to all clients to perform the terrain edit.
  • All clients receive the ClientRpc on their respective network objects.
  • Instead of trying to perform the operation directly on the network object (which caused the previous issue), the operation is sent to a non-network object (perhaps a manager or controller) that is responsible for applying the terrain edit.

Here it is in Diagram form:

So is my thinking correct? That the key takeaway is that each client calls a method on a locally available object, which then performs the necessary operation. This method doesn’t rely on the network object’s ownership but instead relies on a common functionality available to all clients?

I wouldn’t have the brush send the RPC. That’s just the editing tool. A script on the terrain object would make more sense, since the brush will also change the terrain it should call into the terrain script to make the modification, which then does the RPC call to the server. It may also apply the change but store the command in case the server rejects the edit.

Command pattern is important for undo/redo and the general implementation. You may even need to keep a history of all terrain change commands from all users so that the sequence of changes can be applied to the initial terrain to restore the current terrain by applying all changes in sequence, in the order the changes were made (network tick).

The tricky part being two users editing the same area with conflicting tools. One client may raise the terrain +10, another may set it to an absolute height 0 (flatten). Locally, the clients will have their edit applied before the remote edit, so they see the terrain differently until the server tells them what order these changes happened. The server needs to instruct clients to apply the (most recent) changes in a specific order based on the network tick the clients sent their change requests.

Thanks @CodeSmile you’ve been very helpful. I’m still trying to figure out the in’s and outs of this system, but even though im a bit lost, I can tell it’s a really nice system and quite explicit. I don’t understand however why this doesnt do what I expect it to:

[Rpc(SendTo.Server)]
    private void EditTerrainServerRpc(Vector3 pos, BrushType brushType)
    {
        DebugController.Log("SERVERRPC called: " +
                            "OwnerId " + OwnerClientId + " " +
                            "at position " + pos + " " +
                            "with brush type " + brushType);
        testClientRpc(pos, BrushType.Raise);
    }

    [Rpc(SendTo.NotMe)]
    private void testClientRpc(Vector3 pos, BrushType brushType)
    {
        DebugController.Log("CLIENTRPC called: " +
                            "OwnerId " + OwnerClientId + " " +
                            "at position " + pos + " " +
                            "with brush type " + brushType);
        GameManager.Instance.GameManagerPerformTerrainUpdate(pos, brushType);
    }

Here I’d expect the player to send the RPC to the server, and then the client rpc would be sent to everyone but ‘not the original sender’, so in this case I was trying to make the edit in terrain locally, and then send the serverrpc and consequentially clients rpc to everyone else so that everyone updates to my state, but I don’t need to wait for the server to have my edits made. (I do take your point that We may want to keep track of it in case we have rules, which we will, and the server wants to reject the edit. but im not there yet).

What happens here is, if the host sends this code, everything works fine, but if a client does it, then that client will edit the terrain twice, once locally and asecond time from the client RPC and the host wont edit it even once.

Me too. :slight_smile:

Networking is not easy, and particularly because there’s no clear separation between server and client code, or owner and non-owner code, or local vs remote code. It’s all in the same script by default.

No, the “NotMe” refers to the server/host here because of the execution context. Since testClientRpc is called by the server, the “me” is the server/host.

SendTo.NotOwner should work as you intend if the server/host owns the object in question.
You can use the RpcParams to filter out the target owner, or simply add the clientId as a parameter and locally check if the clientId is the same as “mine”.

Yea this seems to work well:

[Rpc(SendTo.Server)]
private void NotifyServerForTerrainEditRpc(Vector3 pos, BrushType brushType, ulong clientId)
{
    // will do some validation here to see if this edit should be accepted
  
    DebugController.Log("Server notified for terrain edit at position " + pos + " with brush type " + brushType);
    UpdateOtherClientsRpc(pos, BrushType.Raise, RpcTarget.Not(clientId, RpcTargetUse.Temp));
}

thanks for the input