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
  1. Systems and modules

Instance Handler

PreviousDirect Local Execution of RPCsNextScene Management

Last updated 6 months ago

The Instance Handler allows you to easily get access the from anywhere, even non or scripts:

var nm = InstanceHandler.NetworkManager;

But more importantly, it allows you to easily register, unregister and get instances yourself! Below is an example of registering and unregistering the instance, as well as the instances being used from static methods. Mind that you can also get the instances from other scripts, whether they are MonoBehaviour or not.

public class GameManager : NetworkBehaviour
{
    private void Awake() 
    {
        //We register the GameManager instance
        InstanceHandler.RegisterInstance(this);
    }
    
    private void OnDestroy() 
    {
        //Upon being destroyed, we unregister the game manager instance
        InstanceHandler.UnregisterInstance<GameManager>();
    }

    private static void GetInstanceExample() 
    {
        //This will fail if the manager isn't registered
        InstanceHandler.GetInstance<GameManager>().Success();
    }
    
    private static void  TryGetInstanceExample() 
    {
        //This will only run if we get the manager. Potentially invert the if statement and log and error if you don't get it
        if(InstanceHandler.TryGetInstance(out GameManager manager))
            manager.Success();
    }

    private void Success() 
    {
        Debug.Log("Now we're logging from the GameManager instance!", this);
    }
}
🖥️
Network Manager
Network Identity
Network Behaviour