Challenge 4: RPC call
- Game Development course
- Advanced
- Lesson 70/71
- 5 min
Write a script that defines a ServerRpc method.
Challenge 4: ServerRpc Network Calls
Under an Authoritative Server architecture, clients cannot directly mutate game state. Actions are requested on the server by invoking methods decorated with [ServerRpc].
// Called on client, executed strictly on the server
[ServerRpc]
public void RequestFireServerRpc(Vector3 direction) {
SpawnProjectileOnServer(direction);
}Following Netcode naming conventions, these methods must end with the ServerRpc suffix.
Practice ServerRpc method name test
Define a Start method. Inside, declare string rpcMetodu = "TakeDamageServerRpc"; and print "Tetiklenen RPC: " + rpcMetodu to the console.
Starter code
using UnityEngine;
public class RpcMethodTester : MonoBehaviour
{
// Start icinde rpcMetodu tanimla ve yazdir
}Sample solution
using UnityEngine;
public class RpcMethodTester : MonoBehaviour
{
void Start()
{
string rpcMetodu = "TakeDamageServerRpc";
Debug.Log("Tetiklenen RPC: " + rpcMetodu);
}
}Expected output
Tetiklenen RPC: TakeDamageServerRpc
Practice RPC client ID tracking
Define a Start method. Inside, declare int clientId = 1; and print "Istemci kimligi: " + clientId to the console.
Starter code
using UnityEngine;
public class RpcClientTester : MonoBehaviour
{
// Start icinde clientId tanimla ve yazdir
}Sample solution
using UnityEngine;
public class RpcClientTester : MonoBehaviour
{
void Start()
{
int clientId = 1;
Debug.Log("Istemci kimligi: " + clientId);
}
}Expected output
Istemci kimligi: 1
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which RPC type allows a client to trigger an operation on the server in a Netcode architecture?
- ClientRpc
- StaticRpc
- ServerRpc Correct answer
- BroadcastRpc
Explanation: Remote procedure calls dispatched from a client to execute on the server are defined as ServerRpc.
2 Declare ServerRpc function
[____] public void SubmitScoreServerRpc(int score) {}
[ServerRpc] public void SubmitScoreServerRpc(int score) {}
Explanation: Methods intended for server-side execution are decorated with the '[ServerRpc]' attribute.
3 Multiplayer RPC concepts
| Term | Meaning |
|---|---|
ServerRpc |
Method called on server from client |
ClientRpc |
Broadcasts from server to all clients |
RPC |
Remote procedure call mechanism |
Authoritative Server |
Server authoritative over game state |