CodeBlanks Logo Icon CodeBlanks

Challenge 4: RPC call

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

Player.cs
using UnityEngine;

public class RpcMethodTester : MonoBehaviour
{
    // Start icinde rpcMetodu tanimla ve yazdir
}

Sample solution

Player.cs
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

Player.cs
using UnityEngine;

public class RpcClientTester : MonoBehaviour
{
    // Start icinde clientId tanimla ve yazdir
}

Sample solution

Player.cs
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

Question
[____] public void SubmitScoreServerRpc(int score) {}
Answer
[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

All lessons in Advanced