🐾PurrNet Docs
Advanced

Network Modules

NetworkModule lets you create reusable, network-aware components that are not MonoBehaviours. Attach multiple modules to a single NetworkIdentity to compose complex networked behaviours.

PlayerNetworkBehaviourHealthModule80hpStaminaModule55%AbilityModule3s cdClient AClient BEach module syncs its slice of state independently
3 modules — each syncs independently

What is a NetworkModule?

A NetworkModule is a base class (not a MonoBehaviour) that gives you access to the same RPC attributes, SyncVars, and lifecycle hooks available in NetworkBehaviour. The key difference: modules are pure C# classes that you declare as fields on a NetworkIdentity, making them fully composable and reusable across any prefab.

Instead of building a monolithic NetworkBehaviour with health, inventory, and abilities all in one class, you write a HealthModule, an InventoryModule, and an AbilityModule — then attach whichever combination each prefab needs.

Module lifecycle

ParameterTypeDefaultDescription
OnSpawn()virtual void—Called when the parent NetworkIdentity spawns on the network. Override for network initialisation.
OnDespawn()virtual void—Called before the parent object is removed from the network. Clean up subscriptions here.
isServerbool—True when running on the server. Mirrors the parent identity's isServer property.
isOwnerbool—True on the client that owns the parent NetworkIdentity. Mirrors parent's isOwner.
IsControllerbool—True if owner OR (server with no owner). Mirrors parent's IsController.
identityNetworkIdentity—Reference back to the parent NetworkIdentity this module is attached to.

Complete HealthModule example

HealthModule.cs
using PurrNet;
using UnityEngine;
using System;

[Serializable]
public class HealthModule : NetworkModule
{
    [SerializeField] private int maxHealth = 100;

    private SyncVar<int>  _health    = new();
    private SyncVar<bool> _isDead    = new();

    // Public events — other scripts subscribe locally
    public event Action<int, int> OnHealthChanged;  // (newHealth, maxHealth)
    public event Action           OnDeath;

    public int  CurrentHealth => _health.value;
    public bool IsDead        => _isDead.value;

    public override void OnSpawn()
    {
        base.OnSpawn();
        _health.onChanged += (hp) => OnHealthChanged?.Invoke(hp, maxHealth);
        _isDead.onChanged += (dead) => { if (dead) OnDeath?.Invoke(); };

        if (isServer)
            _health.value = maxHealth;
    }

    public void TakeDamage(int amount)
    {
        if (!isServer || _isDead.value) return;
        _health.value = Mathf.Max(0, _health.value - amount);
        if (_health.value == 0) _isDead.value = true;
    }

    public void Heal(int amount)
    {
        if (!isServer || _isDead.value) return;
        _health.value = Mathf.Min(maxHealth, _health.value + amount);
    }

    [ObserversRpc(runLocally: true)]
    public void RpcPlayHitEffect(Vector3 hitPoint)
    {
        VFXManager.SpawnBlood(hitPoint);
    }
}

Attaching a module to a NetworkBehaviour

Declare the module as a field — that's all. PurrNet detects and registers it automatically.

PlayerCharacter.cs
using PurrNet;
using UnityEngine;

public class PlayerCharacter : NetworkBehaviour
{
    // Modules declared as fields — PurrNet auto-registers them
    private HealthModule    _health    = new();
    private InventoryModule _inventory = new();
    private AbilityModule   _abilities = new();

    protected override void OnSpawned(bool asServer)
    {
        // Modules have already been initialised by this point
        _health.OnDeath += HandleDeath;
        _health.OnHealthChanged += (hp, max) => healthBar.SetValue(hp, max);

        if (isOwner)
            _abilities.OnAbilityReady += ShowAbilityGlow;
    }

    private void HandleDeath()
    {
        if (isServer)
            StartCoroutine(RespawnAfterDelay(5f));
    }

    // External code (e.g., a bullet) calls the module directly
    public void ReceiveDamage(int damage, Vector3 hitPoint)
    {
        _health.TakeDamage(damage);
        _health.RpcPlayHitEffect(hitPoint);
    }
}

Reuse across prefabs

The same HealthModule works on a PlayerCharacter, an NPC, a Destructible, or a Vehicle. No copy-paste, no inheritance chains.

NetworkModule vs NetworkBehaviour

  • Use NetworkBehaviour when your script needs to be a component in the Unity Inspector (serialized fields, MonoBehaviour events like Update()).
  • Use NetworkModule for pure-logic, reusable network components that do not need their own GameObject slot — health, inventory, abilities, cooldowns, status effects.

Do not store modules in collections

Network modules must be declared as fields or properties directly on a NetworkIdentity. Storing them inside a List<NetworkModule>, array, or dictionary will break automatic network registration. Declare each module as its own named field.

Modules can reference each other

Modules can hold references to sibling modules through the constructor or via identity.GetModule<T>() inside OnSpawn(). This lets an AbilityModule check _health.IsDead before allowing the ability to fire.