RPCs

RPCs

RPCs (Remote Procedure Calls) are a way of sending a method call across the network and executing it on another peer. uLobby features a rich RPC system that allows you to easily send messages containing arbitrary data between peers in a reliable way. It shares most of the features that uLink offers but differs slightly in the API.

Defining an RPC Method

RPC methods are normal methods defined with the UnityEngine.RPC attribute. You can specify any method parameters, and optionally a uLink.BitStream and LobbyMessageInfo parameter. The LobbyMessageInfo parameter contains information about the RPC message, such as the peer who sent it.
The following are two examples of RPC method definitions:
[RPC] public void MyRPC(int argument) { // Do something. }
[RPC] public void MyOtherRPC(string argument, LobbyMessageInfo info) { Debug.Log("Received RPC sent by " + info.sender); }

Sending RPCs

RPCs are sent using the Lobby.RPC() method, that has the following declarations:
void RPC(string rpcName, LobbyPeer target, params object[] args)

void RPC(string rpcName, IList<LobbyPeer> targets, params object[] args)
rpcName is the name of the method to execute, and the second argument is either a list of peers or a single peer in which to execute the RPC. The third argument is an arbitrary list of arguments that will be serialized and sent with the RPC to be retrieved as arguments for the RPC method implementation.
Only the lobby is allowed to send RPCs to any connected peer. Servers and clients are only allowed to send to the lobby.
Just like uLink, uLobby will automatically attempt to serialize any class or struct values given as a parameter to the call, without you having to write special serialization code. However, you can also define your own serialization codecs to fully customize the way data is sent and retrieved.

Receiving RPCs

Before a component containing an RPC method definition can receive RPCs, the Lobby.AddListener() method must be called to add the component as a listener of RPCs (and other events). There is also a corresponding Lobby.RemoveListener() method.