Table of Contents

Class SteamNetworkClient

Namespace
SteamNetworkLib
Assembly
SteamNetworkLib.dll

Main entry point for SteamNetworkLib - provides simplified access to all Steam networking features. Perfect for use in MelonLoader mods that need Steam lobby and P2P functionality.

public class SteamNetworkClient : IDisposable
Inheritance
SteamNetworkClient
Implements
Inherited Members

Constructors

SteamNetworkClient()

Initializes a new instance of the SteamNetworkClient class. Call Initialize() before using any other methods.

public SteamNetworkClient()

SteamNetworkClient(NetworkRules)

Initializes a new instance of the SteamNetworkClient class with custom NetworkRules. Call Initialize() before using any other methods.

public SteamNetworkClient(NetworkRules rules)

Parameters

rules NetworkRules

Properties

CurrentLobby

Gets information about the current lobby, or null if not in a lobby.

public LobbyInfo? CurrentLobby { get; }

Property Value

LobbyInfo

HostPlayerId64

Gets the 64-bit Steam ID of the current host, or 0 when not in a lobby/session.

public ulong HostPlayerId64 { get; }

Property Value

ulong

Remarks

This mirrors CurrentLobby ownership using a runtime-neutral primitive value suitable for serialization and comparison.

IsHost

Gets whether the local player is the host of the current lobby.

public bool IsHost { get; }

Property Value

bool

IsInLobby

Gets whether the local player is currently in a lobby.

public bool IsInLobby { get; }

Property Value

bool

IsInitialized

Gets whether this client has successfully initialized Steam networking.

public bool IsInitialized { get; }

Property Value

bool

Remarks

This becomes true only after Initialize() or TryInitialize() succeeds. It remains false when the game is launched without Steamworks, when SteamAPI.Init() has not run for the current process, or when initialization fails for any other reason.

Use this before calling lobby, member-data, SyncVar, or P2P methods in mods that support optional multiplayer or delayed Steamworks startup.

LibraryVersion

Gets the current version of SteamNetworkLib.

public static string LibraryVersion { get; }

Property Value

string

LobbyData

Gets the lobby data manager for handling lobby-wide data.

public SteamLobbyData? LobbyData { get; }

Property Value

SteamLobbyData

LobbyManager

Gets the lobby manager for handling Steam lobby operations.

public SteamLobbyManager? LobbyManager { get; }

Property Value

SteamLobbyManager

LocalPlayerId

Gets the Steam ID of the local player.

public CSteamID LocalPlayerId { get; }

Property Value

CSteamID

LocalPlayerId64

Gets the 64-bit Steam ID of the local player, or 0 when unavailable.

public ulong LocalPlayerId64 { get; }

Property Value

ulong

Remarks

Prefer this value for logs, dictionaries, config files, JSON payloads, and cross-runtime DTOs. Use LocalPlayerId only when calling APIs that require the Steamworks CSteamID type.

MemberData

Gets the member data manager for handling player-specific data.

public SteamMemberData? MemberData { get; }

Property Value

SteamMemberData

NetworkRules

Current network rules applied to P2P behavior.

public NetworkRules NetworkRules { get; }

Property Value

NetworkRules

P2PManager

Gets the P2P manager for handling peer-to-peer communication.

public SteamP2PManager? P2PManager { get; }

Property Value

SteamP2PManager

SessionMode

Gets the currently active networking session mode.

public NetworkSessionMode SessionMode { get; }

Property Value

NetworkSessionMode

VersionCheckEnabled

Gets or sets whether automatic version checking is enabled. When enabled, the library will automatically check for version compatibility between players.

public bool VersionCheckEnabled { get; set; }

Property Value

bool

Remarks

IMPORTANT: Version checking is crucial for ensuring proper data transfer and synchronization between players.

Disabling this feature or ignoring version mismatches may result in:

  • Data serialization/deserialization failures
  • Message format incompatibilities
  • Synchronization errors
  • Unexpected networking behavior or crashes

It is strongly recommended to keep this enabled and ensure all players use the same SteamNetworkLib version.

Methods

BroadcastMessage(P2PMessage)

Sends a message to all players in the lobby. This is a non-async wrapper around BroadcastMessageAsync.

public void BroadcastMessage(P2PMessage message)

Parameters

message P2PMessage

The message to broadcast.

Remarks

This method is provided for backward compatibility. For new code, use BroadcastMessageAsync instead.

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

P2PException

Thrown when the message cannot be sent.

BroadcastMessageAsync(P2PMessage)

Sends a message to all players in the lobby.

public Task BroadcastMessageAsync(P2PMessage message)

Parameters

message P2PMessage

The message to broadcast.

Returns

Task

Remarks

This method sends the message to each player individually.

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

P2PException

Thrown when the message cannot be sent.

BroadcastTextMessage(string)

Broadcasts a simple text message to all players. This is a non-async wrapper around BroadcastTextMessageAsync.

public void BroadcastTextMessage(string text)

Parameters

text string

The text message to broadcast.

Remarks

This method is provided for backward compatibility. For new code, use BroadcastTextMessageAsync instead.

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

P2PException

Thrown when the message cannot be sent.

BroadcastTextMessageAsync(string)

Broadcasts a simple text message to all players.

public Task BroadcastTextMessageAsync(string text)

Parameters

text string

The text message to broadcast.

Returns

Task

A task that represents the asynchronous operation.

Remarks

This is a convenience method that creates a TextMessage internally.

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

P2PException

Thrown when the message cannot be sent.

CheckLibraryVersionCompatibility()

Performs a comprehensive version check and fires the OnVersionMismatch event if incompatibilities are found.

public bool CheckLibraryVersionCompatibility()

Returns

bool

True if all versions are compatible, false if mismatches were detected.

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

LobbyException

Thrown when not in a lobby.

CreateClientSyncVar<T>(string, T, NetworkSyncOptions?, ISyncValidator<T>?)

Creates a client-owned synchronized variable where each client can set their own value.

public ClientSyncVar<T> CreateClientSyncVar<T>(string key, T defaultValue, NetworkSyncOptions? options = null, ISyncValidator<T>? validator = null)

Parameters

key string

A unique key for this sync variable.

defaultValue T

The default value for clients who haven't set a value.

options NetworkSyncOptions

Optional configuration options.

validator ISyncValidator<T>

Optional validator for value constraints.

Returns

ClientSyncVar<T>

A new ClientSyncVar<T> instance.

Type Parameters

T

The type of value to synchronize.

Remarks

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

Storage: Uses Steam lobby member data, automatically synced by Steam.

Use Cases: Ready status, player loadouts, per-client preferences.

Validation: Optional validator can enforce constraints on values.

// Create a client-owned sync var
var isReady = client.CreateClientSyncVar("Ready", false);

// With validation var teamValidator = new RangeValidator<int>(1, 4); var team = client.CreateClientSyncVar("Team", 1, null, teamValidator);

// 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 allReady = isReady.GetAllValues(); bool everyoneReady = allReady.Values.All(r => r);

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

ArgumentException

Thrown when key is null or empty.

SyncSerializationException

Thrown when the type T cannot be serialized.

CreateHostSyncVar<T>(string, T, NetworkSyncOptions?, ISyncValidator<T>?)

Creates a host-authoritative synchronized variable.

public HostSyncVar<T> CreateHostSyncVar<T>(string key, T defaultValue, NetworkSyncOptions? options = null, ISyncValidator<T>? validator = null)

Parameters

key string

A unique key for this sync variable.

defaultValue T

The default value when no synced value exists.

options NetworkSyncOptions

Optional configuration options.

validator ISyncValidator<T>

Optional validator for value constraints.

Returns

HostSyncVar<T>

A new HostSyncVar<T> instance.

Type Parameters

T

The type of value to synchronize.

Remarks

Authority: Only the lobby host can modify this value. Non-host writes are silently ignored (or logged if WarnOnIgnoredWrites is enabled).

Storage: Uses Steam lobby data, automatically synced by Steam to all lobby members.

Use Cases: Game settings, round numbers, match state, or any host-controlled state.

Validation: Optional validator can enforce constraints on values (e.g., ranges, formats).

// Create a host-authoritative sync var
var roundNumber = client.CreateHostSyncVar("Round", 1);

// With validation var scoreValidator = new RangeValidator<int>(0, 1000); var score = client.CreateHostSyncVar("Score", 0, null, scoreValidator);

// Subscribe to changes roundNumber.OnValueChanged += (oldVal, newVal) => MelonLogger.Msg($"Round: {oldVal} -> {newVal}");

// Only host can modify - silently ignored for non-hosts roundNumber.Value = 2;

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

ArgumentException

Thrown when key is null or empty.

SyncSerializationException

Thrown when the type T cannot be serialized.

CreateLobbyAsync(ELobbyType, int)

Creates a new lobby with the specified settings.

public Task<LobbyInfo> CreateLobbyAsync(ELobbyType lobbyType = 1, int maxMembers = 4)

Parameters

lobbyType ELobbyType

The type of lobby to create.

maxMembers int

The maximum number of members allowed in the lobby.

Returns

Task<LobbyInfo>

A task that represents the asynchronous operation. The task result contains the lobby information.

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

LobbyException

Thrown when lobby creation fails.

Dispose()

Releases all resources used by the SteamNetworkClient.

public void Dispose()

Remarks

This method disposes all component managers and releases any unmanaged resources.

GetDataForAllPlayers(string)

Gets the same data key for all players in the lobby.

public Dictionary<CSteamID, string> GetDataForAllPlayers(string key)

Parameters

key string

The data key.

Returns

Dictionary<CSteamID, string>

A dictionary mapping player Steam IDs to their data values.

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

LobbyException

Thrown when not in a lobby.

GetHostMember()

Gets the current host member, or null when no host can be identified.

public MemberInfo? GetHostMember()

Returns

MemberInfo

GetLobbyData(string)

Gets lobby-wide data.

public string? GetLobbyData(string key)

Parameters

key string

The data key.

Returns

string

The data value, or null if not found.

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

LobbyException

Thrown when not in a lobby.

GetLobbyMembers()

Gets all members in the current lobby.

public List<MemberInfo> GetLobbyMembers()

Returns

List<MemberInfo>

A list of MemberInfo objects for all players in the lobby.

Remarks

Returns an empty list if not currently in a lobby.

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

GetLocalMember()

Gets the local member entry, or null when it cannot be found.

public MemberInfo? GetLocalMember()

Returns

MemberInfo

GetMember(ulong)

Gets a lobby member by their 64-bit Steam ID.

public MemberInfo? GetMember(ulong steamId64)

Parameters

steamId64 ulong

The 64-bit Steam ID to find.

Returns

MemberInfo

The matching member, or null when no member matches.

GetMyData(string)

Gets data for the local player.

public string? GetMyData(string key)

Parameters

key string

The data key.

Returns

string

The data value, or null if not found.

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

LobbyException

Thrown when not in a lobby.

GetPlayerData(CSteamID, string)

Gets data for a specific player.

public string? GetPlayerData(CSteamID playerId, string key)

Parameters

playerId CSteamID

The Steam ID of the player.

key string

The data key.

Returns

string

The data value, or null if not found.

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

LobbyException

Thrown when not in a lobby or the player is not in the lobby.

GetPlayerLibraryVersions()

Gets the SteamNetworkLib versions of all players in the lobby.

public Dictionary<CSteamID, string> GetPlayerLibraryVersions()

Returns

Dictionary<CSteamID, string>

A dictionary mapping player Steam IDs to their SteamNetworkLib versions. Players without version data are excluded.

Remarks

Use this method to identify which players have different library versions that could cause data transfer issues.

Players with missing version data may be using older versions of SteamNetworkLib that don't support version checking,

which could result in unpredictable networking behavior and synchronization failures.

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

LobbyException

Thrown when not in a lobby.

GetRemoteMembers()

Gets all non-local members in the current lobby/session.

public List<MemberInfo> GetRemoteMembers()

Returns

List<MemberInfo>

A list of remote lobby members. Returns an empty list when there are no remote members.

Initialize()

Initializes the Steam networking client. Must be called before using any other methods.

public bool Initialize()

Returns

bool

True if initialization was successful.

Exceptions

SteamNetworkException

Thrown when Steam is not available or when initialization fails.

InviteFriend(CSteamID)

Invites a friend to the current lobby.

public void InviteFriend(CSteamID friendId)

Parameters

friendId CSteamID

The Steam ID of the friend to invite.

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

LobbyException

Thrown when not in a lobby or invitation fails.

IsModDataCompatible(string)

Checks if all players have compatible mod data for a given key.

public bool IsModDataCompatible(string dataKey)

Parameters

dataKey string

The data key to check for compatibility.

Returns

bool

True if all players have the same data value, false otherwise.

Remarks

This method is useful for verifying that all players are using the same mod version.

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

LobbyException

Thrown when not in a lobby.

JoinLobbyAsync(CSteamID)

Joins an existing lobby by ID.

public Task<LobbyInfo> JoinLobbyAsync(CSteamID lobbyId)

Parameters

lobbyId CSteamID

The Steam ID of the lobby to join.

Returns

Task<LobbyInfo>

A task that represents the asynchronous operation. The task result contains the lobby information.

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

LobbyException

Thrown when joining the lobby fails.

LeaveLobby()

Leaves the current lobby.

public void LeaveLobby()

Remarks

This method has no effect if the local player is not currently in a lobby.

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

OpenInviteDialog()

Opens the Steam overlay invite dialog.

public void OpenInviteDialog()

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

LobbyException

Thrown when not in a lobby or the overlay fails to open.

ProcessIncomingMessages()

Processes incoming P2P packets. Call this regularly (e.g., in Update()).

public void ProcessIncomingMessages()

Remarks

This method should be called frequently to ensure timely processing of incoming messages. If not called regularly, messages may be delayed or dropped.

RegisterMessageHandler<T>(Action<T, CSteamID>)

Registers a handler for a specific message type.

public void RegisterMessageHandler<T>(Action<T, CSteamID> handler) where T : P2PMessage, new()

Parameters

handler Action<T, CSteamID>

The handler function that will be called when messages of this type are received.

Type Parameters

T

The type of message to handle.

Remarks

The handler will be called with the message and the sender's Steam ID.

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

SendDataSyncAsync(CSteamID, string, string, string)

Sends a data synchronization message to a player.

public Task<bool> SendDataSyncAsync(CSteamID playerId, string key, string value, string dataType = "string")

Parameters

playerId CSteamID

The Steam ID of the target player.

key string

The data key.

value string

The data value.

dataType string

The data type identifier.

Returns

Task<bool>

A task that represents the asynchronous operation. The task result indicates whether the message was sent successfully.

Remarks

This is a convenience method that creates a DataSyncMessage internally.

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

P2PException

Thrown when the message cannot be sent.

SendLargeDataToPlayerAsync(CSteamID, string, byte[], int, int?)

Sends a large byte payload to a specific player using reliable file-transfer chunks.

public Task<bool> SendLargeDataToPlayerAsync(CSteamID playerId, string transferName, byte[] data, int channel = 0, int? chunkSize = null)

Parameters

playerId CSteamID

The Steam ID of the target player.

transferName string

A developer-facing name for the transfer.

data byte[]

The bytes to send.

channel int

The communication channel to use.

chunkSize int?

Optional chunk payload size. When null, SteamNetworkLib calculates the largest safe reliable payload size.

Returns

Task<bool>

A task whose result indicates whether every chunk was accepted by Steam for sending.

SendMessageToPlayerAsync(CSteamID, P2PMessage)

Sends a message to a specific player.

public Task<bool> SendMessageToPlayerAsync(CSteamID playerId, P2PMessage message)

Parameters

playerId CSteamID

The Steam ID of the target player.

message P2PMessage

The message to send.

Returns

Task<bool>

A task that represents the asynchronous operation. The task result indicates whether the message was sent successfully.

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

P2PException

Thrown when the message cannot be sent.

SendTextMessageAsync(CSteamID, string)

Sends a simple text message to a player.

public Task<bool> SendTextMessageAsync(CSteamID playerId, string text)

Parameters

playerId CSteamID

The Steam ID of the target player.

text string

The text message to send.

Returns

Task<bool>

A task that represents the asynchronous operation. The task result indicates whether the message was sent successfully.

Remarks

This is a convenience method that creates a TextMessage internally.

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

P2PException

Thrown when the message cannot be sent.

SetLobbyData(string, string)

Sets lobby-wide data that is accessible to all players.

public void SetLobbyData(string key, string value)

Parameters

key string

The data key.

value string

The data value.

Remarks

Only the lobby owner can set lobby data. This method will fail silently if called by a non-owner.

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

LobbyException

Thrown when not in a lobby or not the lobby owner.

SetMyData(string, string)

Sets data for the local player that is visible to all players.

public void SetMyData(string key, string value)

Parameters

key string

The data key.

value string

The data value.

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

LobbyException

Thrown when not in a lobby.

SetMyDataBatch(Dictionary<string, string>)

Sets multiple data values at once for the local player.

public void SetMyDataBatch(Dictionary<string, string> data)

Parameters

data Dictionary<string, string>

A dictionary containing key-value pairs to set.

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

LobbyException

Thrown when not in a lobby.

SyncModDataWithAllPlayers(string, string, string)

Synchronizes data with all players in the lobby. This is a non-async wrapper around SyncModDataWithAllPlayersAsync.

public void SyncModDataWithAllPlayers(string dataKey, string dataValue, string dataType = "string")

Parameters

dataKey string

The data key to synchronize.

dataValue string

The data value to synchronize.

dataType string

The data type identifier.

Remarks

This method is provided for backward compatibility. For new code, use SyncModDataWithAllPlayersAsync instead.

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

LobbyException

Thrown when not in a lobby.

SyncModDataWithAllPlayersAsync(string, string, string)

Synchronizes data with all players in the lobby. Useful for mod compatibility checks and data synchronization.

public Task SyncModDataWithAllPlayersAsync(string dataKey, string dataValue, string dataType = "string")

Parameters

dataKey string

The data key to synchronize.

dataValue string

The data value to synchronize.

dataType string

The data type identifier.

Returns

Task

A task that represents the asynchronous operation.

Remarks

This method both sets the local player's data and broadcasts it to all other players.

Exceptions

InvalidOperationException

Thrown when the client is not initialized.

LobbyException

Thrown when not in a lobby.

TryGetHostMember(out MemberInfo?)

Attempts to get the current host member.

public bool TryGetHostMember(out MemberInfo? host)

Parameters

host MemberInfo

The host member when found; otherwise, null.

Returns

bool

True when a host member was found.

TryGetLocalMember(out MemberInfo?)

Attempts to get the local member entry.

public bool TryGetLocalMember(out MemberInfo? localMember)

Parameters

localMember MemberInfo

The local member when found; otherwise, null.

Returns

bool

True when the local member was found.

TryGetMember(ulong, out MemberInfo?)

Attempts to get a lobby member by their 64-bit Steam ID.

public bool TryGetMember(ulong steamId64, out MemberInfo? member)

Parameters

steamId64 ulong

The 64-bit Steam ID to find.

member MemberInfo

The matching member when found; otherwise, null.

Returns

bool

True when a matching member was found.

TryInitialize()

Attempts to initialize Steam networking without throwing when Steamworks is unavailable.

public bool TryInitialize()

Returns

bool

True when initialization succeeded; otherwise, false.

Remarks

This is the preferred path for consumer mods that can run in single-player mode, retry initialization from a later game lifecycle hook, or tolerate launches where Steamworks never attaches to the process.

When this method returns false, leave local-only gameplay active and retry later instead of calling networking methods. Use TryInitialize(out SteamNetworkException?) when the caller needs the failure reason for logging or diagnostics.

TryInitialize(out SteamNetworkException?)

Attempts to initialize Steam networking without throwing when Steamworks is unavailable.

public bool TryInitialize(out SteamNetworkException? error)

Parameters

error SteamNetworkException

The initialization error, or null when initialization succeeded.

Returns

bool

True when initialization succeeded; otherwise, false.

Remarks

This method wraps Initialize() and converts initialization failures into a boolean result. It does not partially initialize managers on failure, so callers can safely retry after Steamworks becomes available.

UpdateNetworkRules(NetworkRules)

Updates network rules at runtime and propagates to managers.

public void UpdateNetworkRules(NetworkRules rules)

Parameters

rules NetworkRules

Events

OnLobbyCreated

Occurs when a new lobby is created.

public event EventHandler<LobbyCreatedEventArgs>? OnLobbyCreated

Event Type

EventHandler<LobbyCreatedEventArgs>

OnLobbyDataChanged

Occurs when lobby data is changed.

public event EventHandler<LobbyDataChangedEventArgs>? OnLobbyDataChanged

Event Type

EventHandler<LobbyDataChangedEventArgs>

OnLobbyJoined

Occurs when the local player joins a lobby.

public event EventHandler<LobbyJoinedEventArgs>? OnLobbyJoined

Event Type

EventHandler<LobbyJoinedEventArgs>

OnLobbyLeft

Occurs when the local player leaves a lobby.

public event EventHandler<LobbyLeftEventArgs>? OnLobbyLeft

Event Type

EventHandler<LobbyLeftEventArgs>

OnMemberDataChanged

Occurs when member data is changed.

public event EventHandler<MemberDataChangedEventArgs>? OnMemberDataChanged

Event Type

EventHandler<MemberDataChangedEventArgs>

OnMemberJoined

Occurs when a new member joins the current lobby.

public event EventHandler<MemberJoinedEventArgs>? OnMemberJoined

Event Type

EventHandler<MemberJoinedEventArgs>

OnMemberLeft

Occurs when a member leaves the current lobby.

public event EventHandler<MemberLeftEventArgs>? OnMemberLeft

Event Type

EventHandler<MemberLeftEventArgs>

OnP2PMessageReceived

Occurs when a P2P message is received from another player.

public event EventHandler<P2PMessageReceivedEventArgs>? OnP2PMessageReceived

Event Type

EventHandler<P2PMessageReceivedEventArgs>

OnVersionMismatch

Occurs when a version mismatch is detected between players in the lobby.

public event EventHandler<VersionMismatchEventArgs>? OnVersionMismatch

Event Type

EventHandler<VersionMismatchEventArgs>

Remarks

CRITICAL: This event indicates that players are using incompatible versions of SteamNetworkLib.

Version mismatches can cause serious issues including data corruption, synchronization failures, and networking errors.