ServerRpc
ServerRpc lets a client call a method that executes on the server. The client sends a packet; the server receives and runs the method. The client never runs the method body.
How it works
When a client calls a method marked with [ServerRpc], PurrNet serializes the arguments and transmits them over the network. The server deserializes and runs the method. The calling client never executes the method body — unless runLocally: true is set.
Use ServerRpc for any action that requires server-side validation before it affects game state: character movement, damage application, spawning objects, or changing ownership.
Attribute parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
requireOwnership | bool | Network Rules | When true (default), only the object's current owner can call this RPC. Set to false to allow any client to call it — useful for global actions like voting or damage requests. |
channel | Channel | Channel.Reliable | The transport channel to use. Reliable = guaranteed delivery; Unreliable = faster but may drop. |
runLocally | bool | false | When true, the caller also executes the method locally before the packet reaches the server. Useful for immediate local feedback. |
Basic usage
using PurrNet;
using UnityEngine;
public class PlayerController : NetworkBehaviour
{
// Called from any client — executes on server
[ServerRpc]
private void CmdMove(Vector3 direction)
{
// Only runs on server
transform.position += direction * speed;
}
// Only the owner can call this
[ServerRpc(requireOwnership: true)]
private void CmdJump()
{
// Server-authoritative jump logic
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
void Update()
{
if (!isOwner) return;
var dir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
if (dir != Vector3.zero) CmdMove(dir);
}
}Reading sender info with RPCInfo
Add the optional RPCInfo info = default parameter to any RPC method to inspect who sent it and whether you are on the server side.
[ServerRpc(requireOwnership: false)]
private void CmdPickupItem(int itemId, RPCInfo info = default)
{
// info.sender is the player who called this
PlayerID caller = info.sender;
if (!CanPlayerPickup(caller, itemId))
{
Debug.LogWarning($"{caller} tried to pick up item they don't own!");
return;
}
GiveItemToPlayer(caller, itemId);
}| Parameter | Type | Default | Description |
|---|---|---|---|
info.sender | PlayerID | — | The PlayerID of the client who made the call. Available when the method signature includes RPCInfo info = default. |
info.asServer | bool | — | True when the method body is running on the server. |
RunLocally — immediate client feedback
By default, a ServerRpc only runs on the server. With runLocally: true, the caller also runs the method immediately before the packet reaches the server. This gives zero-latency visual feedback at the cost of potentially diverging from server state if validation fails.
// Client fires immediately (muzzle flash, audio) then server validates
[ServerRpc(runLocally: true)]
private void CmdFire(Vector3 origin, Vector3 direction)
{
if (info.asServer)
{
// Server: validate ammo, apply damage
if (ammo <= 0) return;
ammo--;
Physics.Raycast(origin, direction, out var hit, 100f);
if (hit.collider && hit.collider.TryGetComponent<Health>(out var h))
h.TakeDamage(damage);
}
// Both server and client run this:
SpawnMuzzleFlash();
PlayFireSound();
}RunLocally divergence
runLocally: true, the client may display a temporary state that the server will later correct. Always reconcile server-authoritative state on the client to prevent visual glitches.Situational example
Player respawn
public class RespawnSystem : NetworkBehaviour
{
[SerializeField] private Transform[] spawnPoints;
// Dead player requests respawn after timer expires
[ServerRpc(requireOwnership: true)]
public void CmdRequestRespawn(RPCInfo info = default)
{
// Server picks a spawn point and sets position
var point = spawnPoints[Random.Range(0, spawnPoints.Length)];
transform.position = point.position;
// Broadcast the new position to all clients
RpcSetPosition(point.position);
}
[ObserversRpc]
private void RpcSetPosition(Vector3 pos)
{
transform.position = pos;
}
}Server-only execution
if (isServer) guards for any logic branching.Never trust client inputs
Anti-patterns
- Calling ServerRpc every frame: Use
Channel.Unreliablefor continuous input and throttle with a send interval. Never send reliable RPCs at 60 Hz. - Trusting the caller: Never use
info.senderto grant resources to a player without validation — always cross-check against server-owned state. - Heavy computation in ServerRpc: Move expensive work to a coroutine or job to avoid blocking the server tick.