In Unreal Engine's networking model, Remote Procedure Calls (RPCs) are functions that are replicated over the network and executed on remote machines. There are specific types of RPCs for different communication patterns:
- Server RPCs: Functions called on the client but executed on the server.
- Client RPCs: Functions called on the server but executed on a specific client.
- Multicast RPCs: Functions called on the server and executed on all clients.
Client RPCs are designed to be invoked by the server to execute code on a specific client. They are not intended to be called from the client itself. When you mark a function as a Client RPC (with Client specifier in C++ or by setting Replicates to "Run on Owning Client" in Blueprints), you're telling Unreal Engine that this function should be network-replicated from the server to the client.
Calling a Client RPC locally on the client is generally not recommended. Here's why:
-
Confusion and Readability: Marking a function as a Client RPC implies that it's meant to be called from the server. Calling it on the client can be confusing for anyone else reading your code (or even for you in the future). It goes against the intended use of RPCs and can make the code harder to understand and maintain.
-
Unintended Side Effects: While Unreal Engine might allow the function to execute locally, there might be underlying engine behaviors or future updates that could prevent it from working as expected. Relying on unintended behavior can lead to bugs that are hard to track down.
-
Networking Overhead: Although calling a Client RPC locally on the client doesn't send network traffic, the function is still marked for replication. This could introduce unnecessary overhead or conflicts with Unreal’s networking optimizations.
-
Potential for Errors: Unreal Engine may produce warnings or errors when a Client RPC is called from a client, as the engine expects these functions to be called from the server. This could clutter your logs with warnings and make debugging more difficult.
Recommended Approach:
-
Use a Local Function: Define a regular (non-RPC) function for code that needs to run locally on the client. Call this function directly from the client without any RPC specifiers.
void YourClass::YourLocalFunction()
{
// Local client-side code here
}
-
Server Calls Client RPC When Needed: If the server needs to trigger this function on the client, have the server call a Client RPC that, in turn, calls the local function.
UFUNCTION(Client, Reliable)
void Client_TriggerFunction();
void YourClass::Client_TriggerFunction()
{
YourLocalFunction();
}
-
Ensure Correct Context: Always ensure that RPCs are called in the appropriate context (server or client). Use checks like HasAuthority() to determine if the code is running on the server or client.
if (HasAuthority())
{
// Server-side logic
Client_TriggerFunction();
}
else
{
// Client-side logic
YourLocalFunction();
}
Conclusion:
While calling a Client RPC locally on the client might seem to work superficially, it is not the intended use of RPCs in Unreal Engine and can lead to code that is hard to maintain and potentially buggy. It's better practice to separate local functions from network-replicated functions and ensure that each is called from the appropriate context. This approach improves code clarity, maintenance, and adheres to Unreal Engine's networking conventions.
Summary: Calling a Client RPC locally on the client is not recommended because it goes against the intended use of RPCs, can confuse code maintenance, and might lead to unintended side effects. Instead, use a local function for client-only execution and reserve Client RPCs for server-to-client communication.