In this lesson, you will learn how to bridge the gap between local responsiveness and server authority by implementing input prediction and validation.
In a high-latency multiplayer environment, waiting for the server to confirm every movement input makes your game feel "sluggish." To solve this, we use Client-Side Prediction. When a player presses a key, the client immediately applies the movement locally while simultaneously sending an RPC (Remote Procedure Call) to the server to request that same action.
The Prediction Loop
The client acts as if the server has already approved the move. Meanwhile, the server receives the input, validates it against its own authoritative state, and then broadcasts the result. If the server’s calculated position differs significantly from the client’s prediction, the server sends a correction, and the client "snaps" to the server's version of reality.
To ensure this stays secure, you must use Server-Side Validation. You never let the client set its own position directly; instead, the client sends "Input Data" (like MoveForward or TurnRight values), and the server re-runs that logic to verify the move is legal (e.g., checking if the player is colliding with walls or moving faster than the MaxWalkSpeed).
Implementation Strategy
You define a struct to bundle your input data, which is then passed through an RPC:
USTRUCT()
struct FClientInputPacket {
GENERATED_BODY()
float ForwardValue;
float RightValue;
uint32 SequenceNumber; // Used to track and resolve reconciliation
};
// Inside your Character class
UFUNCTION(Server, Reliable, WithValidation)
void Server_SendInput(FClientInputPacket InputData);
By sending a SequenceNumber with each input packet, the server can acknowledge exactly which inputs it has processed. If the server detects a discrepancy, it sends a correction back to the client. The client then "replays" any pending inputs that occurred after the corrected timestamp, creating a seamless experience despite the latency. This keeps your game feeling responsive locally while preventing cheating by keeping the final authority on the server.
Key Takeaway: Client-side prediction provides immediate feedback by simulating inputs locally, while server-side validation ensures all movement remains authoritative and secure against tampering.