RPCs

Lecture Summary: RPCs

In this lecture, we explored RPCs (Remote Procedure Calls), a simple way to send messages between the client and server in Unity's Netcode for Entities. We learned that RPCs are just simple messages sent from a client to the server or vice versa, enabling basic communication in our multiplayer game.

First, we defined an RPC by creating a struct (SimpleRpc) that implements IRpcCommand. This struct contains the data we want to send, like an integer value. We then set up client and server systems, ensuring they run only in their respective worlds using WorldSystemFilter attributes.

On the client system, we implemented key input detection to trigger the RPC. To send an RPC, we created an entity, attached the SimpleRpc component, and added the SendRpcCommandRequest component. This component handles the actual sending of the RPC. If it were a server RPC, we could optionally specify a target client. Client RPCs can only go to the Server.

On the server system, we listened for incoming RPCs by querying entities with the SimpleRpc and ReceiveRpcCommandRequest components. After processing the RPC, we destroyed the entity to avoid errors.

Testing confirmed that pressing a key on the client successfully sent an RPC to the server, which received and processed it. This basic connection lays the groundwork for more advanced features, such as synchronizing constant data, which we'll cover in the next lecture by marking the connection as "InGame."

Complete and Continue  
Discussion

3 comments