- Home /
The question is answered, right answer was accepted
Rpc is received by client but Cmd is not received by server
Below is a fragment of my code where the Rpc and Cmd are called. As you can see I check if the player is the client or the server, and depending on which they are I call either an Rpc or a Cmd passing the same parameter. Debugging this script I can see that the server calls RpcSendRList
and the client calls CmdSendRList
as desired.
if(networkController.isServer)
{
networkController.RpcResetRppList();
foreach(KeyValuePair<int, float> entry in resources)
{
networkController.RpcAddToRppList(entry.Key, entry.Value);
}
}
else
{
networkController.CmdResetRppList();
foreach(KeyValuePair<int, float> entry in resources)
{
networkController.CmdAddToRppList(entry.Key, entry.Value);
}
}
Below are the Rpcs being called:
[Command]
public void CmdAddToRppList(int _key, float _value)
{
opponentResources.Add(_key, _value);
}
[ClientRpc]
public void RpcAddToRppList(int _key, float _value)
{
opponentResources.Add(_key, _value);
}
Debugging this second script only the Rpc
is actually called on the client and the Cmd
is never called on the server (the breakpoint is never hit).
What am I doing wrong with the commands?
Answer by Dudledok · Jul 17, 2015 at 10:26 AM
The problem was that the command was not running on the local player. Maybe there should be a warning for if you are trying to send commands from a player that isn't yours, even though when worded like that it should be obvious. I also changed it so that the NetworkManager automatically spawns the player prefab. Letting that do its own thing is the easiest way for sure.
Answer by n1gth · Jul 15, 2015 at 11:32 AM
You have to make sure that the Command is happening on the Player object - but you should see a warning for that. It's probably because you're not passing an appropriate type through, you should try with a simple type.
Also, in terms of this pattern, it's worth thinking about who is the server and who is the client. For example, you might be a client, but still running on the server. It might be appropriate depending on your use case for a Cmd to be called whether or not you're on the server, and then the Cmd does an RPC to update all objects running on clients.
I have a server (who is also a client) and another client. In the legacy system I would simply do an RPC with mode All so everyone runs the code.
@n1gth - I updated the question with the latest which functions fully as expected for the the RPCs, but no Commands received. Setup: These functions are on a script which inherits from NetworkBehaviour and is attached to a standalone GameObject in the scene which exists for both the server and client.
Are you running a local client on the server like I mentioned above? In this case it'll always hit the first branch of the if, as isServer will be true. Try taking that if statement out and just make the Cmd call.
Sorry, maybe I didn't make it clear when I said the client calls CmdSendRList as desired. What I mean is the if statement works fine and the client will get to the Command and won't try to call the RPC (which would result in an error I think?), but it's simply not received by the server.
Follow this Question
Related Questions
Multiplayer Inventory System with RPCs 0 Answers
SyncListString not changing in a Command 0 Answers
Network how to send data/activate function 1 Answer
ClientRpc not called on Client 1 Answer