Class HostSyncVar<T>
- Namespace
- SteamNetworkLib.Sync
- Assembly
- SteamNetworkLib.dll
A host-authoritative synchronized variable that automatically keeps its value in sync across all lobby members using Steam lobby data.
public class HostSyncVar<T> : IDisposable
Type Parameters
TThe type of value to synchronize.
- Inheritance
-
HostSyncVar<T>
- Implements
- Inherited Members
Remarks
Authority Model: Only the lobby host can modify this value. Non-host clients can read the value and observe changes, but writes are silently ignored.
Storage: Uses Steam lobby data for storage, which is automatically synchronized to all lobby members by Steam.
Supported Types:
- Primitives: int, float, double, bool, string, long, etc.
- Enums: Serialized as underlying integer values
- Collections: List<T>, Dictionary<string, T>, arrays
- Custom types: Classes with parameterless constructor and public properties
// Create a host-authoritative sync var
var roundNumber = client.CreateHostSyncVar("Round", 1);
// Subscribe to changes
roundNumber.OnValueChanged += (oldVal, newVal) =>
MelonLogger.Msg($"Round changed: {oldVal} -> {newVal}");
// Only host can modify - silently ignored for non-hosts
roundNumber.Value = 2;
Properties
CanWrite
Gets whether the local player can modify this value (i.e., is host).
public bool CanWrite { get; }
Property Value
FullKey
Gets the full key including any prefix.
public string FullKey { get; }
Property Value
IsDirty
Gets whether there is a pending value waiting to be synced.
public bool IsDirty { get; }
Property Value
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
Value
Gets or sets the synchronized value.
public T Value { get; set; }
Property Value
- T
Remarks
Reading: Always returns the current synchronized value.
Writing: Only takes effect when called by the lobby host. Non-host writes are silently ignored (or logged if WarnOnIgnoredWrites is enabled).
Methods
Dispose()
Releases all resources used by the HostSyncVar<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.
Refresh()
Forces a refresh of the value from the lobby data.
public void Refresh()
Remarks
Normally not needed as changes are automatically detected, but useful after joining a lobby or when debugging synchronization issues.
Events
OnSyncError
Occurs when a sync operation fails (serialization error, etc.).
public event Action<Exception>? OnSyncError
Event Type
OnValueChanged
Occurs when the value changes from any source (local or remote).
public event Action<T, T>? OnValueChanged
Event Type
- Action<T, T>
Remarks
Parameters: (oldValue, newValue)
OnWriteIgnored
Occurs when a non-host attempts to write a value.
public event Action<T>? OnWriteIgnored
Event Type
- Action<T>
Remarks
This event is primarily for debugging purposes.
Parameter: the attempted value that was ignored