Game networking

From makifbaysal/tasktrooper · View interactive version

Game networking is an AI agent skill from the GitHub repository makifbaysal/tasktrooper. Use when touching multiplayer code - authoritative server, client prediction and reconciliation, interpolation, lockstep and rollback, server-side validation of every RPC per engine (Netcode, Unreal replication, Godot multiplayer, Colyseus, Bevy crates), bandwidth and versioned messages.

How to use this skill

Skill file

Game Networking

Overview

In a multiplayer game every client is running on a machine you do not control. Whatever a client can send, a cheater will send — teleports, infinite ammo, a hit on someone behind a wall, a purchase with money they do not have. The server (or host) owns the truth; clients send what the player wants to do and render what the server says happened, predicting locally so it still feels instant.

Core principle: Clients send inputs and intents, never outcomes. The authority validates every message before it changes state.

Pick the model the design needs (the repository has usually picked already)

| Model | Fits | Cost |

|---|---|---|

| Authoritative server + client prediction + reconciliation + interpolation of others | Shooters, action, most online games | Server hosting; prediction code |

| Deterministic lockstep (send only inputs) | RTS with thousands of units | Every peer must simulate bit-identically; latency = slowest peer |

| Rollback (predict remote inputs, rewind and re-simulate on correction) | Fighting games, precise 1v1 | Fully deterministic, cheap-to-clone state |

| Listen server / host-authoritative | Co-op, small sessions | Host can cheat; host migration |

Stacks: Unity — Netcode for GameObjects, Netcode for Entities, or the Mirror/FishNet/Photon the repo already uses; Unreal — built-in replication (Iris on newer projects that enabled it); Godot — high-level multiplayer (@rpc, MultiplayerSpawner, MultiplayerSynchronizer); web — Colyseus, or WebSocket/WebRTC servers; Bevy — lightyear, bevy_replicon, bevy_ggrs (rollback). Never add a second networking stack beside the one in use.

Validate on the authority — per engine

// Unreal: server RPC with validation; returning false from _Validate disconnects the caller
UFUNCTION(Server, Reliable, WithValidation)
void ServerFire(FVector_NetQuantize Origin, FVector_NetQuantizeNormal Direction, float ClientTime);

bool AShooterCharacter::ServerFire_Validate(FVector_NetQuantize Origin, FVector_NetQuantizeNormal Direction, float ClientTime) {
    return Direction.IsNormalized() && FMath::IsFinite(ClientTime);
}
void AShooterCharacter::ServerFire_Implementation(FVector_NetQuantize Origin, FVector_NetQuantizeNormal Direction, float ClientTime) {
    if (!Weapon->CanFire(GetWorld()->GetTimeSeconds())) return;                       // server's cooldown
    if (FVector::Dist(Origin, GetPawnViewLocation()) > MaxOriginError) return;        // origin near the server's view
    Weapon->FireServerAuthoritative(Origin, Direction);                               // server traces the hit
}
// Unity Netcode for GameObjects: only the owner may ask, and the server decides
[Rpc(SendTo.Server)]
void RequestMoveRpc(Vector2 input, RpcParams rpcParams = default) {
    if (rpcParams.Receive.SenderClientId != OwnerClientId) return;
    if (float.IsNaN(input.x) || float.IsNaN(input.y)) return;
    _pendingInput = Vector2.ClampMagnitude(input, 1f);                     // speed applied server-side in FixedUpdate
}
# Godot 4: any peer may call it, so check who did
@rpc("any_peer", "call_remote", "reliable")
func request_buy(item_id: StringName) -> void:
    if not multiplayer.is_server():
        return
    var sender := multiplayer.get_remote_sender_id()
    var player := players.get(sender) as PlayerState
    if player == null or not Shop.has_item(item_id):
        return
    Shop.try_purchase(player, item_id)    # server checks its own balance, not the client's
// Colyseus: validate shape, range and rate before touching state
this.onMessage("move", (client, msg: unknown) => {
  const m = MoveSchema.safeParse(msg);                    // zod: finite numbers, |dir| <= 1
  if (!m.success || !this.rate.allow(client.sessionId)) return;
  this.state.players.get(client.sessionId)?.queueInput(m.data);
});

What "validate" covers: the sender owns the entity; numbers are finite (NaN and Infinity are classic exploits) and in range; enums are known values; array/string lengths are capped; message rate per client is limited; the action is legal now (cooldown, ammo, position, line of sight) according to server state.

Prediction and reconciliation (the local player)

  1. Client samples input each tick, tags it with a sequence number, applies it locally (prediction), stores it, and sends it.
  2. Server applies inputs in order on its fixed step and sends back the authoritative state plus the last processed sequence number.
  3. Client sets its state to the server's, drops acknowledged inputs, and re-applies the unacknowledged ones (reconciliation). Smooth small corrections visually; snap large ones.

Other players are rendered interpolated between two past server snapshots (about 100 ms behind), never extrapolated far. Hit detection on the server rewinds targets to what the shooter saw (lag compensation), within a capped window.

Bandwidth and messages

Testing

Common Mistakes

Red Flags

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.