PurrNet
  • 🐈Introduction
    • ‼️Unique to PurrNet
    • 💲Pricing
    • 💻Compatibility
    • 📚Addon library
    • 🗺️Roadmap
    • 🏎️Performance
      • RPC Benchmarks
      • Network Transform benchmarks
    • 🥰Support PurrNet
  • 📚Guides
    • Installation/Setup
    • Getting Started
    • Converting to PurrNet
      • Converting from Mirror
      • Converting from FishNet
    • Lobby System
    • Networking custom classes, structs & types
    • Chat system with broadcasts
    • Easy Multiplayer Physics (input Sync)
  • 🎮Full game guides
    • 🔫First Person Shooter
    • 🪓Survival Game
  • 🖥️Systems and modules
    • Network Manager
      • Network Rules
      • Network Prefabs
      • Network Visibility
        • Distance condition
      • Transports
        • Composite Transport
        • UDP Transport
        • Web Transport
        • Local Transport
        • Steam Transport
        • Purr Transport
      • Authentication
    • PlayerID (Client connection)
    • Network Identity
      • NetworkBehaviour
      • Ownership
      • Sync Types
        • SyncVar
        • SyncList
        • SyncArray
        • SyncQueue
        • SyncDictionary
        • SyncEvent
        • SyncHashset
        • SyncTimer
      • Don't destroy on load
    • Network Modules
    • Collider Rollback
    • Client Side Prediction
      • Overview
      • Predicted Identities
      • Predicted Hierarchy
      • Best Practices
      • Input Handling
      • State Handling
    • Plug n' play components
      • Network Transform
      • Network Animator
      • Network Reflection (Auto Sync)
      • State Machine (Auto Networked)
    • Spawning & Despawning
    • Remote Procedure Call (RPC)
      • Generic RPC
      • Static RPC
      • Awaitable RPC
      • Direct Local Execution of RPCs
    • Instance Handler
    • Scene Management
    • Broadcast
  • 🤓Terminology
    • Early Access
    • Channels
    • Client Auth/Everyone (Unsafe)
    • Host
    • Server Auth (Safe)
  • 💡Integrations
    • Dissonance
    • Cozy Weather
Powered by GitBook
On this page
  • IPackedAuto
  • IPackedSimple
  • IPacked
  • Static custom type
  • Packing directly RPC
  1. Guides

Networking custom classes, structs & types

PreviousLobby SystemNextChat system with broadcasts

Last updated 5 months ago

PurrNet auto networks structs, classes and interfaces for you, given they directly or indirectly consist of serializable data.

Networking custom classes and structs can be extremely useful to send over the network, for example by using basic .

IPackedAuto

public struct ManagedStruct : IPackedAuto
{
    public int myInt;
    public object data;
}

IPackedSimple

This allows you to handle the serialization yourself. This is the easiest way if you need to make your own serializer. An example of using this is below:

public struct ManagedStruct : IPackedSimple
{
    public int myInt;
    public object data;
    
    public void Serialize(BitPacker packer)
    {
        Packer<int>.Serialize(packer, ref myInt);
        Packer<object>.Serialize(packer, ref data);
    }
}

IPacked

This also allows you to handle the serialization yourself. Contrary to the previous method, this allows you to disconnect the reading and writing of data, for more special cases. An example of using this is below:

public struct ManagedStruct : IPacked
{
    public int myInt;
    public object data;
    
    public void Write(BitPacker packer)
    {
        Packer<int>.Write(packer, myInt);
        Packer<object>.Write(packer, data);
    }
    
    public void Read(BitPacker packer)
    {
        Packer<int>.Read(packer, ref myInt);
        Packer<object>.Read(packer, ref data);
    }
}

Static custom type

This is the most performant way of handling custom reading and writing of your data. The PurrNet serialization system will automatically find your static type. Below is an example of using this with a Vector2:

public static CustomSerializerForExternalType
{
    public static void Write(this BitPacker packer, Vector2 value)
    {
        packer.Write(value.x);
        packer.Write(value.y);
    }
    
    public static void Read(this BitPacker packer, ref Vector2 value)
    {
        packer.Read(ref value.x);
        packer.Read(ref value.y);
    }
}

Packing directly RPC

You can pack data directly and optimally using the BitPackerPool and simply sending the BitPacker directly through an RPC. This uses less memory and allows you to easily pack custom data on the fly.

protected override void OnSpawned(bool asServer)
{
    if (!asServer)
    {
        using var writer = BitPackerPool.Get();
        
        Packer<string>.Write(writer, "Hello, server!");
        Packer<string>.Write(writer, "Maybe some audio data being pumped?");

        MyRPC(writer);
    }
}

[ServerRpc(requireOwnership: false)]
private void MyRPC(BitPacker data)
{
    using (data)
   {
        string message = default;
        string audioData = default;

        Packer<string>.Read(data, ref message);
        Packer<string>.Read(data, ref audioData);

        PurrLogger.Log(message); // Hello, server!
        PurrLogger.Log(audioData); // Maybe some audio data being pumped?
    }
}

This will allow PurrNet to automatically handle the serialization for you. This is useful for , as data outside of a may have trouble serializing over the network. An example of using this is below:

📚
Broadcasting
Network Behaviour
RPC's