StaticRpc
StaticRpc sends a method call to all clients without requiring a NetworkBehaviour instance. Useful for global events that aren't tied to any specific networked object.
Static RPCs
In most RPC patterns the method lives on a NetworkBehaviour instance. A static RPC lives on a static method but is still dispatched through a specific NetworkIdentity— PurrNet uses that identity's connection to route the packet, but the method itself has no this reference.
Useful for singleton managers, global event dispatchers, or utility classes that don't need to store per-instance state.
using PurrNet;
using UnityEngine;
// This manager exists as a single instance in every scene.
public class GlobalAnnouncer : NetworkBehaviour
{
// Static RPC — no instance state needed, but routes via this NetworkIdentity.
[ObserversRpc(bufferLast: false)]
public static void BroadcastAnnouncement(string message, AnnouncementPriority priority)
{
// Runs on ALL clients when the server calls it.
AnnouncerUI.ShowBanner(message, priority);
AudioManager.PlayAnnouncerClip(priority);
}
// Only the server triggers world announcements
public void AnnounceKingOfTheHill(PlayerID capturer)
{
if (!isServer) return;
string name = PlayerRegistry.GetDisplayName(capturer);
BroadcastAnnouncement($"{name} captured the hill!", AnnouncementPriority.High);
}
}Static RPCs still require a NetworkIdentity
NetworkIdentity that is live. The static method must be declared inside a class that inherits from NetworkBehaviourand an instance of that behaviour must be spawned on the network. The method simply doesn't use this.Generic RPCs
A generic RPC takes a type parameter T so that a single network method can accept different payload shapes. Ideal for addon or plugin systems, generic inventory transactions, or cases where you want a single RPC declaration to cover multiple structurally similar messages of different types.
Rules for generic RPCs:
- The type parameter must be serializable by PurrNet (struct +
IPackedAutorecommended) - Every concrete type used must be registered with
[RegisterNetworkType] - Type constraints limit what PurrNet will attempt to serialize
Type constraints for generics
| Parameter | Type | Default | Description |
|---|---|---|---|
T : struct | constraint | — | Generic RPCs work best with structs because they are value types and are fully serialized by PurrNet without any additional configuration. |
T : IPackedAuto | constraint | — | Adding IPackedAuto lets PurrNet automatically serialize every field. Required for non-primitive struct payloads. |
[RegisterNetworkType(typeof(T))] | attribute | — | Registers a concrete type so PurrNet includes it in the type manifest. Place this attribute on the struct definition. |
T : class | constraint | — | Class types are supported but must implement a custom IPackedAuto serializer. Prefer structs to avoid GC pressure on frequently called RPCs. |
Basic generic RPC
using PurrNet;
public class DataRelay : NetworkBehaviour
{
// A single declaration handles any registered struct payload.
[ServerRpc(requireOwnership: true)]
private void CmdSendData<T>(T payload) where T : struct, IPackedAuto
{
// Server receives and processes any registered T
ProcessPayload(payload);
}
// Callers pass whatever concrete type they need:
public void SendEquipment(EquipRequest req) => CmdSendData(req);
public void SendCraftOrder(CraftRequest req) => CmdSendData(req);
}Situational example — generic inventory transaction system
Instead of writing a separate RPC for every inventory operation (equip, drop, trade, craft) a generic transaction RPC handles them all with a single network declaration. Each concrete transaction type encodes its own logic.
using PurrNet;
using UnityEngine;
// ---- Payload types ----
[RegisterNetworkType(typeof(EquipTransaction))]
public struct EquipTransaction : IPackedAuto
{
public int itemId;
public int slotIndex;
}
[RegisterNetworkType(typeof(TradeTransaction))]
public struct TradeTransaction : IPackedAuto
{
public PlayerID target;
public int offeredItemId;
public int requestedItemId;
}
[RegisterNetworkType(typeof(CraftTransaction))]
public struct CraftTransaction : IPackedAuto
{
public int recipeId;
public int ingredientSlot;
}
// ---- Network behaviour ----
public class InventoryTransactionSystem : NetworkBehaviour
{
// One generic RPC handles all transaction types.
[ServerRpc(requireOwnership: true)]
public void CmdSubmitTransaction<T>(T tx, RPCInfo info = default) where T : struct, IPackedAuto
{
// Server resolves the concrete type and delegates
switch (tx)
{
case EquipTransaction equip:
HandleEquip(info.sender, equip);
break;
case TradeTransaction trade:
HandleTrade(info.sender, trade);
break;
case CraftTransaction craft:
HandleCraft(info.sender, craft);
break;
default:
Debug.LogWarning($"[Server] Unknown transaction type: {typeof(T).Name}");
break;
}
}
private void HandleEquip(PlayerID player, EquipTransaction tx)
{
var inv = PlayerRegistry.GetInventory(player);
if (!inv.HasItem(tx.itemId)) return;
inv.EquipToSlot(tx.itemId, tx.slotIndex);
}
private void HandleTrade(PlayerID initiator, TradeTransaction tx)
{
TradeManager.ProposeTrade(initiator, tx.target, tx.offeredItemId, tx.requestedItemId);
}
private void HandleCraft(PlayerID player, CraftTransaction tx)
{
CraftingSystem.AttemptCraft(player, tx.recipeId, tx.ingredientSlot);
}
}Register every concrete type
[RegisterNetworkType(typeof(MyStruct))] on a concrete type used with a generic RPC will cause a serialization error at runtime the first time that type is sent. Register all types in a central file (e.g. NetworkTypeRegistry.cs) so none are missed when new transaction types are added.Combine static + generic for utility managers
EffectManager.BroadcastEffect<T>(T fx) routes any registered effect struct to all clients without adding a new RPC method per effect type.