SyncEvent
SyncEvent turns a standard C# event into a networked one. When the authority raises the event, PurrNet automatically propagates it to every observer — no manual RPC dispatch required.
How it works
Decorate a C# event field with [SyncEvent] on a NetworkBehaviour. When the server (or owner, if owner-auth is enabled) invokes the event, PurrNet intercepts the invocation and fans it out as a network message. On all observing clients the event fires locally — any subscriber receives the call exactly as if it had been raised on that machine.
The key difference from [ObserversRpc] is that no separate method is needed for broadcasting. You just raise the event in your game logic and PurrNet handles the network layer.
Declaration
using PurrNet;
using System;
using UnityEngine;
public class GameEvents : NetworkBehaviour
{
// Zero-argument event — simple signal
[SyncEvent] public event Action OnRoundStarted;
// Events with arguments — any serializable types
[SyncEvent] public event Action<PlayerID, int> OnPlayerScored; // player, points
[SyncEvent] public event Action<PlayerID> OnPlayerEliminated;
[SyncEvent] public event Action<string> OnAnnouncementPlayed;
// ---- Server raises these ----
public void StartRound()
{
if (!isServer) return;
// Set up server state...
OnRoundStarted?.Invoke(); // Fires on ALL clients automatically
}
public void AwardScore(PlayerID player, int points)
{
if (!isServer) return;
scoreTracker.Add(player, points);
OnPlayerScored?.Invoke(player, points); // Fires on ALL clients
}
}Subscribing from any client
Clients subscribe and unsubscribe to a SyncEvent exactly like a normal C# event. Subscription is purely local — no network message is sent when you add or remove a listener.
using PurrNet;
using UnityEngine;
public class ScoreboardListener : NetworkBehaviour
{
[SerializeField] private GameEvents _gameEvents;
[SerializeField] private ScoreboardUI _ui;
[SerializeField] private EliminationFeed _feed;
protected override void OnSpawned(bool asServer)
{
base.OnSpawned(asServer);
// Subscribe on all machines — server and clients alike
_gameEvents.OnRoundStarted += HandleRoundStarted;
_gameEvents.OnPlayerScored += HandlePlayerScored;
_gameEvents.OnPlayerEliminated += HandlePlayerEliminated;
}
protected override void OnDespawned(bool asServer)
{
base.OnDespawned(asServer);
// Always unsubscribe to prevent memory leaks
_gameEvents.OnRoundStarted -= HandleRoundStarted;
_gameEvents.OnPlayerScored -= HandlePlayerScored;
_gameEvents.OnPlayerEliminated -= HandlePlayerEliminated;
}
private void HandleRoundStarted()
{
_ui.ResetScores();
countdownDisplay.StartCountdown(3);
}
private void HandlePlayerScored(PlayerID player, int points)
{
_ui.AnimateScore(player, points);
}
private void HandlePlayerEliminated(PlayerID player)
{
_feed.AddEntry(player);
if (player == localPlayer)
ShowDeathScreen();
}
}Situational example — GameManager with multiple sync events
Centralising game-flow signals in a single manager makes it easy for any other behaviour in the scene to react without needing a direct reference to the source of the action.
using PurrNet;
using System;
using System.Collections;
using UnityEngine;
public class GameManager : NetworkBehaviour
{
[SyncEvent] public event Action<float> OnGameTimerStarted; // duration
[SyncEvent] public event Action OnGameOver;
[SyncEvent] public event Action<PlayerID> OnMVPAnnounced;
[SyncEvent] public event Action<int> OnScoreThresholdReached; // new threshold
private SyncVar<int> _totalScore = new(0);
protected override void OnSpawned(bool asServer)
{
base.OnSpawned(asServer);
if (asServer)
{
_totalScore.onChanged += OnTotalScoreChanged;
StartCoroutine(RunGame());
}
}
private IEnumerator RunGame()
{
float duration = 180f;
OnGameTimerStarted?.Invoke(duration); // fires on all clients
yield return new WaitForSeconds(duration);
PlayerID mvp = ScoreRegistry.GetTopPlayer();
OnMVPAnnounced?.Invoke(mvp); // fires on all clients
yield return new WaitForSeconds(2f);
OnGameOver?.Invoke(); // fires on all clients
}
private void OnTotalScoreChanged(int newScore)
{
// Server checks milestones and fires threshold events
int[] thresholds = { 100, 250, 500, 1000 };
foreach (var t in thresholds)
{
if (newScore >= t && (_totalScore.previousValue < t))
OnScoreThresholdReached?.Invoke(t);
}
}
}SyncEvent vs ObserversRpc
| Parameter | Type | Default | Description |
|---|---|---|---|
SyncEvent | [SyncEvent] event Action | — | Declare a C# event. Invoking it on the authority automatically broadcasts to all observers. No manual RPC call needed. |
ObserversRpc | [ObserversRpc] void Method() | — | A named method the server explicitly calls to push a message. Supports bufferLast for late joiners. More explicit, slightly more boilerplate. |
ObserversRpc + bufferLast | [ObserversRpc(bufferLast: true)] | — | Replays the last call for new observers. SyncEvent does not buffer — use ObserversRpc + bufferLast when late joiners must receive the event. |
SyncEvent does not buffer for late joiners
[ObserversRpc(bufferLast: true)], a SyncEvent is fire-and-forget — clients who connect after the event fires will not receive it. For events that late joiners must see (game state, round phase), use a SyncVar to hold the current state, or use an ObserversRpc with bufferLast: true.Only invoke on the authority
[ServerRpc] to request the server raise the SyncEvent on their behalf.