Remote Procedure Call (RPC)
Last updated
Last updated
In short, an RPC allows you to call a method on another device/machine. If you're on a client and successfully call a ServerRpc, that method will now be called on the server machine and not on the local machine actually calling the method. This makes it very easy to interact with other machines over the network.
In order to call RPC's, you have to make sure your script inherits from at least Network Identity, but it is recommended that you inherit from Network Behaviour to get more functionality.
The RPC logic can depend on ownership, and this is modifiable in your Network Rules.
There are 3 different kinds of RPC's: - ServerRPC - ObserversRPC - TargetRPC
Different RPC's has different parameters to take in, like so:
Below you'll find the different RPC types with the parameters they each hold.
The ServerRPC will call Client -> Server meaning that the method which you execute on the client, will be ran on the Server instead.
It holds the following parameters:
RequireOwnership - This will override any settings within the Network Rules or the Network Identity inspector. With this, you can modify whether ownership is required to call the RPC.
RunLocally - By default, this is false, however, if you override it to true it will mean that the called will run the logic as well as the server.
The ObserversRPC will call Server -> All clients, or Client -> All clients if your network rules allow it. This means that the method being called will trigger for every client.
It holds the following parameters:
RequireServer - This will override any settings within the Network Rules as to whether clients can call the ObserversRpc directly.
BufferLast - If set to true, when a new client joins, they will get the most recent call of the method with the data within the parameters.
RunLocally - If set to true, the caller will run the method logic locally, avoiding the networking route. If the server calls it, the server will also run the method. If a client calls it, the client will run the method immediately, instead of awaiting the servers call.
The TargetRPC will call Server -> Client, or Client -> Client if your network rules allow it. This means that the method called, will only be triggered on the client which PlayerID is given.
It holds the following parameters:
RequireServer - This will override any settings within the Network Rules as to whether clients can call the ObserversRpc directly.
BufferLast - If set to true, when the target client joins again (we can only hold their data if we've seen them before), they will get the most recent call of the method with the data within the parameters.
RunLocally - If set to true, the caller will run the method logic locally, avoiding the networking route. If the server calls it, the server will also run the method. If a client calls it, the client will run the method immediately, instead of awaiting the servers call.
RPC Info is a super useful tool to get information about an RPC that has just been sent.
You simply add the RPCInfo as a parameter of your RPC and default the value to default and it will auto populate upon receiving the RPC
This value is mostly used to get the sender.