Table of Contents

Class ClientSyncVar<T>

Namespace
SteamNetworkLib.Sync
Assembly
SteamNetworkLib.dll

A client-owned synchronized variable where each client can set their own value, and all values are visible to all lobby members via Steam member data.

public class ClientSyncVar<T> : IDisposable

Type Parameters

T

The type of value to synchronize.

Inheritance
ClientSyncVar<T>
Implements
Inherited Members

Remarks

Authority Model: Each client owns and can modify their own value. All clients can read all other clients' values.

Storage: Uses Steam lobby member data for storage, which is automatically synchronized to all lobby members by Steam.

Use Cases:

  • Ready status for each player
  • Player loadouts or preferences
  • Per-player scores or statistics
  • Player customization options visible to others
// Create a client-owned sync var
var isReady = client.CreateClientSyncVar("Ready", false);

// Subscribe to any client's changes
isReady.OnValueChanged += (playerId, oldVal, newVal) => 
    MelonLogger.Msg($"Player {playerId} ready: {newVal}");

// Set my own value
isReady.Value = true;

// Read another player's value
bool player2Ready = isReady.GetValue(player2Id);

// Get all players' values
var allReadyStates = isReady.GetAllValues();

Properties

FullKey

Gets the full key including any prefix.

public string FullKey { get; }

Property Value

string

IsDirty

Gets whether there is a pending value waiting to be synced.

public bool IsDirty { get; }

Property Value

bool

Remarks

This is true when AutoSync is disabled or when rate limiting has deferred a sync.

Key

Gets the sync key used for this variable.

public string Key { get; }

Property Value

string

Value

Gets or sets the local player's value.

public T Value { get; set; }

Property Value

T

Remarks

This is a shorthand for getting/setting the value for the local player.

Use GetValue(CSteamID) to get other players' values.

Methods

Dispose()

Releases all resources used by the ClientSyncVar<T>.

public void Dispose()

FlushPending()

Forces immediate sync of any pending value, bypassing rate limit.

public void FlushPending()

Remarks

Use this when AutoSync is disabled to manually trigger sync operations, or to force a rate-limited pending value to sync immediately.

GetAllValues()

Gets the values for all players in the lobby.

public Dictionary<CSteamID, T> GetAllValues()

Returns

Dictionary<CSteamID, T>

A dictionary mapping player Steam IDs to their values.

GetValue(CSteamID)

Gets the value for a specific player.

public T GetValue(CSteamID playerId)

Parameters

playerId CSteamID

The Steam ID of the player.

Returns

T

The player's value, or the default value if not set.

Refresh()

Forces a refresh of all values from Steam member data.

public void Refresh()

Events

OnMyValueChanged

Occurs when the local player's value changes.

public event Action<T, T>? OnMyValueChanged

Event Type

Action<T, T>

Remarks

This is a convenience event that filters OnValueChanged to only fire for the local player.

Parameters: (oldValue, newValue)

OnSyncError

Occurs when a sync operation fails (serialization error, etc.).

public event Action<Exception>? OnSyncError

Event Type

Action<Exception>

OnValueChanged

Occurs when any player's value changes.

public event Action<CSteamID, T, T> OnValueChanged

Event Type

Action<CSteamID, T, T>

Remarks

Parameters: (playerId, oldValue, newValue)