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
  2. Remote Procedure Call (RPC)

Awaitable RPC

PreviousStatic RPCNextDirect Local Execution of RPCs

Last updated 5 months ago

Awaitable RPCs is a gamechanger for networking and optimal development properly integrating with the natural workflow of C#

They utilize Tasks and also support UniTask. optionally they can be made async as well.

It not only allows for you to wait till RPC logic is finished, but allows you to return values from the receiving end of the RPC.

Below is a real use-case example of a way for the player to set ready on the server and it will return true if all players are ready, and false if not all players are ready (handled by the CheckForReady method)

//The asyncTimeoutInSec is how long the caller should await a response. 
//In this case it would fail after 10 seconds (default: 5)
[ServerRpc(requireOwnership:false, asyncTimeoutInSec:10)]
public async Task<bool> SetReady(RPCInfo info = default)
{
    if (machine.currentStateNode != this) 
        return false;
    
    if(!_readyPlayers.Contains(info.sender))
        _readyPlayers.Add(info.sender);

    return CheckForReady();
}
private async void OnReady()
{
    var allReady = await shopPhase.SetReady();

    //If everyone is ready, the server handles switching state and we don't do any more
    if (allReady)
        return;
    
    ShowWaitingScreen();
}

Bare bones example

Here is the bare minimum of using this setup

private async void MyLocalMethod()
{
    var myBool = await TestBool();

    if (!myBool)
        return;
    
    Debug.Log("Yay we made it!");
}

[ServerRpc(requireOwnership:false)]
public async Task<bool> TestBool()
{
    return true;
}

It also has a fail safe in with the setup in case this isn't the game state were in:

And for the client all that needs to be done is have an async method which can await for the result of the RPC. You can read more about the. Essentially it will allow the method to run asynchronous of your otherwise fully sequential code, allowing it to halt execution until it has received a response.

🖥️
networked State Machine
async keyword here