Home Guides Tools Netcode generator

s&box netcode generator

Generate a host-authoritative C# Component for s&box. Only the host changes the value; a client asks, and the host checks who asked, how often and what was sent before it applies anything. Read the host-authoritative networking guide for the why.

Scoreboard.cs

C#
using System;
using System.Collections.Generic;
using Sandbox;

/// <summary>
/// Host-authoritative Score. Only the host changes it; clients ask with RequestScore.
/// Generated by sboxlist.com/tools/netcode-generator. Check the names against the Facepunch documentation.
/// </summary>
public sealed class Scoreboard : Component, Component.INetworkListener
{
    // Only the host writes it; clients receive the latest value. Needs NetworkMode.Object on the GameObject.
    [Sync( SyncFlags.FromHost )] public int Score { get; set; } = 0;

    // Rate limit: at most 5 requests per second per connection, for this component.
    const float MinInterval = 0.2f;
    readonly Dictionary<Guid, RealTimeSince> _lastRequest = new();

    // Called by a client. Runs on the host.
    [Rpc.Host]
    public void RequestScore( int value )
    {
        var caller = Rpc.Caller;                             // who sent it
        if ( caller is null ) return;
        if ( Network.OwnerId != Guid.Empty ? caller.Id != Network.OwnerId : !caller.IsHost ) return;   // the owner, or the host if nobody owns it
        if ( _lastRequest.TryGetValue( caller.Id, out var since ) && since < MinInterval ) return;   // too fast
        _lastRequest[caller.Id] = 0f;
        if ( value < 0 || value > 1000 ) return;   // out of range
        // Check the game state here too (cooldowns, round phase, cost).

        Score = value;                                   // the host applies the change
        OnScoreChanged( value );
    }

    // Runs on the host and every client. Effects only (sound, UI); never change game state here.
    [Rpc.Broadcast( NetFlags.Reliable | NetFlags.HostOnly )]
    void OnScoreChanged( int value )
    {
    }

    // Forget the rate state of a connection that left.
    void Component.INetworkListener.OnDisconnected( Connection channel )
    {
        _lastRequest.Remove( channel.Id );
    }
}
Reference: what each part of the component does
PartRuns onUse it for
[Sync( SyncFlags.FromHost )]Written on the host, read everywhereState the host owns: score, health, money. A client cannot set it.
[Rpc.Host]The hostA client asks for a change. Treat every argument as untrusted input.
Rpc.CallerInside an RPCThe connection that sent the call. Check it before anything else.
Rate limitThe hostDrops calls that come faster than the allowed rate from one connection.
[Rpc.Broadcast( NetFlags.Reliable | NetFlags.HostOnly )]The host and every clientEffects after the host accepted a change. Never game state.
Component.INetworkListenerThe hostConnection events; here it clears a leaving player's rate state.

Unofficial and general. The attribute and interface names follow the s&box networking API as we understand it; check them against the Facepunch documentation for the version you build on.