TExcelTAD
Home › Unreal Engine 5 with C++ › RPCs & Authority

RPCs & Authority

Replicated properties carry state from the server to clients. Remote procedure calls (RPCs) carry requests and events: a client asks the server to fire, the server tells one client to show a message, the server tells everyone to play an explosion.

Specifier Called on Runs on Typical use
Server the owning client the server "I want to fire / interact / use this item"
Client the server the owning client "show this hit marker", "your respawn timer is 5 s"
NetMulticast the server the server and all relevant clients cosmetic one-shots: explosion, impact effect

Each RPC is Reliable (guaranteed, in order; use for gameplay-critical, infrequent calls) or Unreliable (may be dropped; use for frequent or cosmetic calls). Too many reliable RPCs overflow the reliable buffer and disconnect the client, so never send reliable RPCs every frame.

Declaring and implementing

cpp
// ArenaCharacter.h
UFUNCTION(Server, Reliable, WithValidation)
void ServerFire(FVector_NetQuantize Origin, FVector_NetQuantizeNormal Direction);

UFUNCTION(Client, Reliable)
void ClientConfirmHit(float Damage);

UFUNCTION(NetMulticast, Unreliable)
void MulticastPlayMuzzleFlash();
cpp
// ArenaCharacter.cpp: you implement the _Implementation (and _Validate) functions;
// UHT generates ServerFire() itself, which sends the call over the network.
bool AArenaCharacter::ServerFire_Validate(FVector_NetQuantize Origin, FVector_NetQuantizeNormal Direction)
{
    return Direction.IsNormalized();               // returning false DISCONNECTS the client: only for blatant cheats
}

void AArenaCharacter::ServerFire_Implementation(FVector_NetQuantize Origin, FVector_NetQuantizeNormal Direction)
{
    // Never trust the client: check what it is allowed to do, on the server.
    if (!CanFire()) return;                                        // cooldown, ammo, alive, not stunned
    if (FVector::Dist(Origin, GetPawnViewLocation()) > 200.f) return;  // origin must be near the player

    ConsumeAmmo();                                                 // replicated (COND_OwnerOnly) to the owner
    FHitResult Hit;
    if (TraceShot(Origin, Direction, Hit))
    {
        ApplyServerDamage(Hit);
        ClientConfirmHit(20.f);                                    // only the shooter sees the hit marker
    }
    MulticastPlayMuzzleFlash();                                    // everyone sees the flash
}

void AArenaCharacter::MulticastPlayMuzzleFlash_Implementation()
{
    if (IsLocallyControlled()) return;           // the shooter already played it when pressing the button
    PlayMuzzleFlashFX();
}

// Input handler on the owning client:
void AArenaCharacter::Fire()
{
    PlayMuzzleFlashFX();                         // instant local feedback (prediction lite)
    ServerFire(GetPawnViewLocation(), GetBaseAimRotation().Vector());
}

Ownership: who may call a Server RPC

A client can only call Server RPCs on actors it owns: its PlayerController, the pawn it possesses, the PlayerState, and actors whose owner chain leads to its PlayerController (a weapon spawned with Owner = Character). Calling a Server RPC on an unowned actor (a door in the level) is dropped, with a warning in the log. That's the most common "my RPC does nothing" bug. The fix: route the request through something the client owns.

cpp
// The door isn't owned by the client, so the character asks on its behalf:
void AArenaCharacter::Interact()               // client
{
    if (AArenaDoor* Door = FindDoorInFront())
    {
        ServerInteract(Door);
    }
}

void AArenaCharacter::ServerInteract_Implementation(AArenaDoor* Door)   // server
{
    if (IsValid(Door) && Door->GetDistanceTo(this) < 300.f)
    {
        Door->Open(this);                      // replicated state on the door
    }
}

Authority and roles

Check True when
HasAuthority() this machine is the authority over the actor (the server, for replicated actors; or the client for actors it spawned locally)
GetLocalRole() == ROLE_Authority same thing, spelled out
ROLE_AutonomousProxy the client's own pawn: locally controlled and predicted
ROLE_SimulatedProxy other players' pawns on a client: just displayed
IsLocallyControlled() this pawn is controlled by a player on this machine (true on the listen-server host for its own pawn)
GetNetMode() NM_Standalone, NM_DedicatedServer, NM_ListenServer, NM_Client

The common patterns: gameplay changes in if (HasAuthority()), local feedback and UI in if (IsLocallyControlled()), cosmetics everywhere.

A runnable model of RPC routing and authority

The model has a server and two clients, actors with owners, and a Call function that routes each RPC kind the way the engine does, including the ownership rule and validation.

cpp · try it yourself
#include <iostream>
#include <string>
#include <vector>

enum class Kind { Server, Client, Multicast };
struct Actor { std::string Name; int OwnerClient; };   // -1 = owned by nobody (server-only)

const char* Machine(int m) { return m == 0 ? "server" : m == 1 ? "client1" : "client2"; }

void Call(int caller, const Actor& a, Kind k, const std::string& fn, bool valid = true) {
    std::cout << Machine(caller) << " calls " << a.Name << "." << fn << ": ";
    switch (k) {
    case Kind::Server:
        if (caller == 0) { std::cout << "runs locally on server\n"; return; }
        if (a.OwnerClient != caller) { std::cout << "DROPPED (caller doesn't own " << a.Name << ")\n"; return; }
        if (!valid) { std::cout << "_Validate failed -> " << Machine(caller) << " DISCONNECTED\n"; return; }
        std::cout << "runs on server\n";
        return;
    case Kind::Client:
        if (caller != 0) { std::cout << "runs locally (only the server can send Client RPCs)\n"; return; }
        if (a.OwnerClient < 1) { std::cout << "runs on server (no owning client)\n"; return; }
        std::cout << "runs on " << Machine(a.OwnerClient) << "\n";
        return;
    case Kind::Multicast:
        if (caller != 0) { std::cout << "runs locally only (multicast from a client isn't sent)\n"; return; }
        std::cout << "runs on server, client1, client2\n";
        return;
    }
}

int main() {
    Actor hero1{"Hero1", 1}, hero2{"Hero2", 2}, door{"Door", -1};

    Call(1, hero1, Kind::Server, "ServerFire");
    Call(1, hero2, Kind::Server, "ServerFire");
    Call(1, door, Kind::Server, "ServerOpen");
    Call(1, hero1, Kind::Server, "ServerFire", /*valid*/ false);
    Call(0, hero1, Kind::Client, "ClientConfirmHit");
    Call(0, door, Kind::Client, "ClientShowPrompt");
    Call(0, hero2, Kind::Multicast, "MulticastPlayMuzzleFlash");
    Call(2, hero2, Kind::Multicast, "MulticastPlayMuzzleFlash");
}
client1 calls Hero1.ServerFire: runs on server
client1 calls Hero2.ServerFire: DROPPED (caller doesn't own Hero2)
client1 calls Door.ServerOpen: DROPPED (caller doesn't own Door)
client1 calls Hero1.ServerFire: _Validate failed -> client1 DISCONNECTED
server calls Hero1.ClientConfirmHit: runs on client1
server calls Door.ClientShowPrompt: runs on server (no owning client)
server calls Hero2.MulticastPlayMuzzleFlash: runs on server, client1, client2
client2 calls Hero2.MulticastPlayMuzzleFlash: runs locally only (multicast from a client isn't sent)

Test yourself

A client calls a Server RPC on a door placed in the level and nothing happens. Why?

  1. Doors can't replicate
  2. The client doesn't own the door, so the RPC is dropped; route it through the client's pawn or controller
  3. The RPC must be Unreliable
  4. RPCs only work in Blueprints

What happens when a _Validate function returns false?

  1. The call is retried
  2. The RPC is ignored silently
  3. The calling client is disconnected
  4. The server crashes

Which RPC type fits an explosion effect everyone should see once?

  1. Server, Reliable
  2. Client, Reliable
  3. NetMulticast, Unreliable
  4. No RPC; replicate a bool