In this lesson, you will learn how to shift game simulation logic from the client to an authoritative server to neutralize common cheating vectors like speed-hacking or item spawning.
The Problem: Trusting the Client
In a non-authoritative model, the client tells the server, "I moved to coordinate (100, 100)" or "I dealt 500 damage to this boss." If a malicious user intercepts these packets, they can send arbitrary coordinates or inflated damage values. Because the server blindly accepts these updates, the integrity of your game state collapses.
The Solution: Server-Side Validation
To prevent cheating, the client must only send intent (e.g., "I am pressing the Move Forward key"), while the server remains the sole entity that calculates the result (e.g., "Given your current position and movement speed, you are now at coordinate (10, 10)").
The server must perform its own simulation of the game physics and logic. If the client’s reported position deviates significantly from what the server calculated, the server rejects the client's state and forces a "reconciliation" (resetting the client to the server's authoritative position).
Implementation Pattern
Instead of letting the client set variables directly, you use the server to validate incoming inputs. Here is a simplified logic flow for a movement command:
// Server-side logic snippet
public void HandleMoveInput(Player player, Vector3 inputDirection, float deltaTime)
{
// Validate: Calculate the maximum distance the player could move based on speed
float maxDistance = player.BaseSpeed * deltaTime;
Vector3 requestedMovement = inputDirection * maxDistance;
// Apply the movement only on the server simulation
player.Position += requestedMovement;
// Sync the validated position back to all clients
SyncPositionToClients(player.Id, player.Position);
}
By strictly restricting the client to sending inputs rather than state changes, you ensure that the server acts as the "source of truth." Even if a player modifies their local game code to travel at 10x speed, the server’s simulation will detect that the input exceeds the maxDistance threshold and prevent the server-side coordinate from updating.
Key Takeaway: Never trust the client; treat all incoming data as untrusted input and re-run all critical game logic on the server to maintain a cheat-proof game state.