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

Triggering Logic Across Network Boundaries with RPCs

Understand how to invoke functions on remote machines to synchronize gameplay events that fall outside standard variable replication.

In this lesson, you will learn how to use Remote Procedure Calls (RPCs) to execute specific, one-time logic on remote machines when variable replication alone isn't sufficient for your gameplay needs.

Beyond State: The Need for RPCs

While variable replication is excellent for keeping states (like health or ammo) synchronized, it is a poor choice for transient events. If you want to play a sound, trigger a particle effect, or fire a weapon projectile, you don't need a persistent variable that changes over time; you need an immediate "command" to execute a function. That is where RPCs come in.

An RPC allows an actor to "ask" a remote machine to run a function. Unlike variables, which represent what something is, RPCs represent what something is doing right now.

Implementing an RPC

To define an RPC in C++, you declare a function with the UFUNCTION macro using the Server, Client, or NetMulticast specifier. For example, if a player performs a melee attack, the client must notify the server to validate the hit:

UFUNCTION(Server, Reliable, WithValidation)
void Server_PerformMeleeAttack();

When you call Server_PerformMeleeAttack() on the client, the engine packages the function request and sends it over the network to the server. The server receives the packet, executes the function body, and ensures that the action occurs within the authoritative environment.

Understanding the Boundaries

The key to using RPCs effectively is understanding the "flow" of authority. A Server RPC is sent from a client to the server; it is the primary way clients communicate actions like "interact" or "fire weapon" to the server. A Multicast RPC is sent from the server to all clients; it is perfect for visual effects that should happen everywhere, such as an explosion.

Remember, RPCs are "fire-and-forget." If a client is disconnected when the RPC is sent, the message is lost. Because they don't persist like variables, they should be used exclusively for discrete, momentary events rather than state synchronization.

Key Takeaway: Use Variable Replication to keep state in sync, and use RPCs to trigger immediate, one-time gameplay events across network boundaries.