ObserversRpc
ObserversRpc broadcasts a method call from the server to all clients observing the object. Every connected client that can see the object receives the call simultaneously.
How it works
The server calls a method marked with [ObserversRpc] and PurrNet distributes the packet to every client currently observing that NetworkIdentity. Observers are determined by the Network Visibility system — by default, all connected clients observe all objects.
Common uses: broadcasting game events (player death, game start), pushing authoritative state after server-side validation, and notifying all clients about world changes.
Attribute parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
channel | Channel | Channel.Reliable | The transport channel used for the broadcast. |
requireServer | bool | Network Rules | When true, only the server can call this RPC. Overrides Network Rules for this method. |
bufferLast | bool | false | When true, replays the most recent call to newly connecting observers. Use for state that late joiners must receive, like the current round phase. |
runLocally | bool | false | When true, the server also executes the method locally when it sends the broadcast. |
Basic usage
using PurrNet;
using UnityEngine;
public class GameManager : NetworkBehaviour
{
// Server calls this to tell everyone the game started
[ObserversRpc(bufferLast: true)]
public void RpcGameStarted(int countdownSeconds)
{
// Runs on ALL connected clients (and server if runLocally)
UI.ShowCountdown(countdownSeconds);
AudioManager.PlayStartJingle();
}
// Server-side trigger — only callable from server code
[ObserversRpc(requireServer: true)]
public void RpcForceRestart()
{
SceneManager.LoadScene("MainGame");
}
}bufferLast for late joiners
Without bufferLast, a client connecting after an ObserversRpc fires will miss it entirely. With bufferLast: true, PurrNet stores the most recent call and replays it when a new observer joins. This is essential for broadcasting initial game state.
public class HealthSystem : NetworkBehaviour
{
private int _currentHealth = 100;
protected override void OnSpawned(bool asServer)
{
if (asServer) RpcInitHealth(_currentHealth);
}
// bufferLast: new clients joining mid-game immediately get current health
[ObserversRpc(Channel.Reliable, bufferLast: true)]
private void RpcInitHealth(int health)
{
_currentHealth = health;
healthBar.SetValue(health);
}
public void TakeDamage(int amount)
{
if (!isServer) return;
_currentHealth -= amount;
RpcUpdateHealth(_currentHealth); // broadcast to all
}
[ObserversRpc]
private void RpcUpdateHealth(int health)
{
_currentHealth = health;
healthBar.SetValue(health);
if (health <= 0) OnDeath();
}
}bufferLast vs SyncVar
bufferLast on ObserversRpc for one-time events that must be replayed (game start, round end).Situational example — kill feed
public class KillFeedManager : NetworkBehaviour
{
// Server calls this after confirming a kill
[ServerRpc(requireOwnership: false)]
public void CmdReportKill(string killerName, string victimName, string weapon, RPCInfo info = default)
{
if (!info.asServer) return; // runLocally guard
// Validate: did info.sender actually make this kill?
BroadcastKill(killerName, victimName, weapon);
}
[ObserversRpc(runLocally: true)]
private void BroadcastKill(string killer, string victim, string weapon)
{
KillFeed.AddEntry(killer, victim, weapon);
}
}requireServer
requireServer is false (default), a client can call ObserversRpc directly if Network Rules allow it. In server-authoritative games, always set requireServer: true or rely on Network Rules to block this.