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.
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:
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.
It holds the following 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.
It holds the following 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.
In order to call RPC's, you have to make sure your script inherits from at least , but it is recommended that you inherit from to get more functionality.
The RPC logic can depend on ownership, and this is modifiable in your .
RequireOwnership - This will override any settings within the or the inspector. With this, you can modify whether ownership is required to call the RPC.
The ObserversRPC will call Server -> All clients, or Client -> All clients if your allow it. This means that the method being called will trigger for every client.
RequireServer - This will override any settings within the as to whether clients can call the ObserversRpc directly.
BufferLast - If set to true, when a new joins, they will get the most recent call of the method with the data within the parameters.
The TargetRPC will call Server -> Client, or Client -> Client if your allow it. This means that the method called, will only be triggered on the client which is given.
RequireServer - This will override any settings within the as to whether clients can call the ObserversRpc directly.
BufferLast - If set to true, when the target 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.