RPC calls
- Game Development course
- Advanced
- Lesson 16/71
- 4 min
Learn remote calls with ClientRpc and ServerRpc.
Remote Procedure Calls (RPC)
An RPC (Remote Procedure Call) allows code running on one machine to trigger a function on a remote machine across the network. Games use RPCs to broadcast visual effects, audio cues, or instant announcements to all players.
// Invoked on server, executes across all connected clients
[ClientRpc]
public void RpcPlayExplosionEffect(Vector3 pos) {
Instantiate(explosionPrefab, pos, Quaternion.identity);
}In Mirror, calls made from server to clients are marked with the [ClientRpc] attribute and an Rpc prefix.
ServerRpc and Netcode for GameObjects
In Unity's official Netcode for GameObjects (NGO) framework, RPC calls are named explicitly: client-to-server calls are [ServerRpc], and server-to-client calls are [ClientRpc].
// In NGO convention, ServerRpc ends with a suffix
[ServerRpc]
public void SubmitScoreServerRpc(int score) {
// Server records the score
}This symmetric structure makes the direction of data flow explicit and easily auditable in code.
Practice ClientRpc invocation flow
Define a Start method. Inside, print the text "Sunucudan tum istemcilere ClientRpc gonderildi" using Debug.Log.
Starter code
using UnityEngine;
public class RpcController : MonoBehaviour
{
// Start icinde RPC akisini yazdir
}Sample solution
using UnityEngine;
public class RpcController : MonoBehaviour
{
void Start()
{
Debug.Log("Sunucudan tum istemcilere ClientRpc gonderildi");
}
}Expected output
Sunucudan tum istemcilere ClientRpc gonderildi
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which RPC type does the server use to trigger an explosion effect simultaneously across all connected clients?
- StaticRpc
- ClientRpc Correct answer
- ServerRpc
- LocalRpc
Explanation: To invoke remote functions from the server across all connected clients, ClientRpc is used.
2 Declare ClientRpc
[____] public void RpcPlayExplosion() {}
[ClientRpc] public void RpcPlayExplosion() {}
Explanation: Methods intended for the server to run across all clients are tagged with the '[ClientRpc]' attribute.
3 RPC mechanisms
| Term | Meaning |
|---|---|
RPC |
Call running a function remotely |
ClientRpc |
Call from server to all clients |
ServerRpc |
Request call sent from client to server |
TargetRpc |
Call from server to one chosen client |