Course contentsShow
← Unreal Engine Game Development
Lesson 717 of 88010. Networking and ReplicationFree lesson

Controlling RPC Execution Based on Network Roles

Use the GetLocalRole and GetRemoteRole functions to conditionally execute logic based on whether the actor is a proxy or authority.

In this lesson, you will learn how to use GetLocalRole and GetRemoteRole to dynamically gate your RPC logic, ensuring your code only executes when the actor has the necessary network authority.

The Role-Based Guard

When writing multiplayer code, an RPC might be triggered from a client or a server, but it often needs to behave differently depending on who is calling it. While RPC declarations define where the function runs, checking GetLocalRole and GetRemoteRole allows you to define if your logic should proceed based on the actor's current network state.

GetLocalRole returns the ENetRole of the actor on the machine currently running the code (e.g., ROLE_Authority or ROLE_AutonomousProxy), while GetRemoteRole tells you how that same actor is perceived by the other side of the connection.

Implementation Example

Imagine an interaction system where a client requests to open a door. You might want to validate the request locally before firing the RPC to save bandwidth, or prevent a "ghost" execution if the actor is not currently relevant to the client.

void AMyDoorActor::RequestOpenDoor()
{
    // Check if we are the client (AutonomousProxy) and have a valid connection to server
    if (GetLocalRole() == ROLE_AutonomousProxy)
    {
        // Only proceed if the server is actually our remote peer
        if (GetRemoteRole() == ROLE_Authority)
        {
            Server_OpenDoor();
        }
    }
}

By verifying roles, you prevent logic errors where a client might attempt to execute server-side commands on an actor they do not own, or accidentally trigger logic meant only for the server's authoritative simulation.

Why This Matters

If you trigger an RPC regardless of the actor's role, you risk spamming the network with invalid requests or creating "desyncs" where clients execute logic they aren't authorized to control. Always pair your RPC calls with a role check to ensure that the code block is only executing in the intended network context.

Key Takeaway: Always validate your GetLocalRole and GetRemoteRole before firing an RPC to ensure the caller has the appropriate network authority to perform the requested action.