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
  • Calling RPC's is easy
  • Types of RPC's
  • RPC Info
  1. Systems and modules

Remote Procedure Call (RPC)

PreviousSpawning & DespawningNextGeneric RPC

Last updated 5 months ago

In short, an RPC allows you to call a method on another device/machine. If you're on a client and successfully call a ServerRpc, that method will now be called on the server machine and not on the local machine actually calling the method. This makes it very easy to interact with other machines over the network.

Calling RPC's is easy

Example script called NetworkTest:

using PurrNet;

public class NetworkTest : NetworkBehaviour
{
    //On spawned will be called when the object is first seen by the network.
    //If you are a host, this OnSpawned will be called twice, once for server and once for client
    protected override void OnSpawned(bool asServer)
    {
        if (asServer)
            return;
        
        //If your network rules are set to everyone on ServerRpc, this can be called from all clients
        //Alternatively, it can only be called by the owner of the identity
        TestServerMethod();
    }

    [ServerRpc]
    private void TestServerMethod()
    {
        //This code will be called on the server.
        
        //The server is now calling the observers rpc method on all observing clients.
        //If your Network Rules allow for it, this could also be called from clients, essentially skipping the Server RPC
        TestObserverMethod();
    }

    [ObserversRpc]
    private void TestObserverMethod()
    {
        //This code will be called on every observing clients machine.
        //This is a good way to update all observing clients with the same information.
    }
    
    [TargetRpc]
    private void TestTargetMethod(PlayerID target)
    {
        //This code will only be called on the local instance of the target
        //This is a good way to update a target client
    }
}

Types of RPC's

There are 3 different kinds of RPC's: - ServerRPC - ObserversRPC - TargetRPC

Different RPC's has different parameters to take in, like so:

[ServerRpc(RequireOwnership:false, RunLocally:true)]

Below you'll find the different RPC types with the parameters they each hold.

ServerRPC:

The ServerRPC will call Client -> Server meaning that the method which you execute on the client, will be ran on the Server instead.

It holds the following parameters:

  • RunLocally - By default, this is false, however, if you override it to true it will mean that the called will run the logic as well as the server.

ObserversRPC:

It holds the following parameters:

  • RunLocally - If set to true, the caller will run the method logic locally, avoiding the networking route. If the server calls it, the server will also run the method. If a client calls it, the client will run the method immediately, instead of awaiting the servers call.

TargetRPC:

It holds the following parameters:

  • RunLocally - If set to true, the caller will run the method logic locally, avoiding the networking route. If the server calls it, the server will also run the method. If a client calls it, the client will run the method immediately, instead of awaiting the servers call.

RPC Info

RPC Info is a super useful tool to get information about an RPC that has just been sent.

You simply add the RPCInfo as a parameter of your RPC and default the value to default and it will auto populate upon receiving the RPC

[ServerRpc]
private void TestRpc(int myValue, RPCInfo info = default)
{
    //Here we now have the RPC info
    Debug.Log($"Sender: {info.sender}")
}

This value is mostly used to get the sender.

In order to call RPC's, you have to make sure your script inherits from at least , but it is recommended that you inherit from to get more functionality.

The RPC logic can depend on ownership, and this is modifiable in your .

RequireOwnership - This will override any settings within the or the inspector. With this, you can modify whether ownership is required to call the RPC.

The ObserversRPC will call Server -> All clients, or Client -> All clients if your allow it. This means that the method being called will trigger for every client.

RequireServer - This will override any settings within the as to whether clients can call the ObserversRpc directly.

BufferLast - If set to true, when a new joins, they will get the most recent call of the method with the data within the parameters.

The TargetRPC will call Server -> Client, or Client -> Client if your allow it. This means that the method called, will only be triggered on the client which is given.

RequireServer - This will override any settings within the as to whether clients can call the ObserversRpc directly.

BufferLast - If set to true, when the target joins again (we can only hold their data if we've seen them before), they will get the most recent call of the method with the data within the parameters.

🖥️
Network Identity
Network Behaviour
Network Rules
Network Rules
Network Identity
network rules
Network Rules
client
network rules
PlayerID
Network Rules
client