Skip to content

ClientRPCs invoked within other ClientRPCs are only executed on the host player #2326

@RikuTheFuffs

Description

@RikuTheFuffs

Description

ClientRPCs invoked within other ClientRPCs are only executed on the host player.

The code below calls a ClientRpc nested within another ClientRpc. It makes sure that only the host calls the nested Rpc, because only the server can do that:

private void Update()
   {
       if (Input.GetKeyDown(KeyCode.T))
       {
           TestNestedRPCCall();
       }
   }

   private void TestNestedRPCCall()
   {
       Debug.LogError("TestNestedRPCCall");
       FirstMethodClientRpc();
   }

   [ClientRpc]
   private void FirstMethodClientRpc()
   {
       Debug.LogError("FirstMethodClientRpc");
       if (IsHost)
           SecondMethodClientRpc();
   }

   [ClientRpc]
   private void SecondMethodClientRpc()
   {
       Debug.LogError("SecondMethodClientRpc");
   }

This will result in logs:

  • On Host: TestNestedRPCCall --> FirstMethodClientRpc --> SecondMethodClientRpc
  • On Client: FirstMethodClientRpc

Solution I'd like

If it is intended to work like this, I would really like there to be a warning for this, as it is easily missed in more complex code. Otherwise, I'd expect the SecondMethodClientRpc() to be executed on all clients as well.

Additional context:

from @fatimah-f :

"when NGO invokes an Rpc, __RpcExecStage is set to either Server or Client meaning the execution context is from "server's eyes" or "client's eyes". there's a similar code running locally too when user invokes an Rpc, and during that if RpcExecStage is not set to None (in other words, if it's either Server or Client instead of None), it treats it like a local NGO -> RPC execution, not like User -> RPC execution.
A potential solution would be to have 2 __RpcExecStage defined: incoming and outgoing — instead of storing the stage on 1 variable. that way, while incoming stage would be set to .Server, outgoing would still be set to .None and the nested RPC call would take outgoing stage into consideration to either send the Rpc call over the network, or execute its body, or skip the function call."

Metadata

Metadata

Assignees

Labels

type:featureNew feature, request or improvement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions