OptionalNetworkAPI

版本 v1.3.2·更新於 2026-09-28

API for plugin developers to write networking for mods that don't require all players to have it.

模組介紹 作者原文

OptionalNetworkAPI

Adds an API for plugin developers to more easily write networking for mods that don't require all players to have the mod.

This primarily includes detecting other players with the mod or sending data to other players with the mod, and has numerous callbacks to facilitate custom behavior if needed.

API

The API is exposed under OptionalNetworking.OptionalNetworkAPI. The primary endpoint is RegisterMod:

ModInfo OptionalNetworkAPI.RegisterMod(string name);

You should only run this once for your mod on Load. This creates a ModInfo object that exposes callbacks and info. As some examples:

  • OtherPlayers returns a collection of nonlocal players with the mod installed.
  • OnPlayerJoined callbacks run when a player joins the lobby.
  • OnPlayerModSet callbacks run when a player's mod is set as enabled (or disabled, in some Bot cases).

Example code:

private static ModInfo _modInfo;

internal static void OnLoad()

{

_modInfo = OptionalNetworkAPI.RegisterMod("MyMod")

_modInfo.OnPlayerJoined += (player) => Logger.Log($"{player.NickName} joined the lobby")

_modInfo.OnPlayerModSet += (player, hasMod) => Logger.Log($"{player.NickName} mod installed: {hasMod}")

}

internal static void PrintModdedPlayers()

{

List names = new();

foreach (var player in _modInfo.Players)

names.Add(player.NickName);

Logger.Log($"Players with the mod: {string.Join(", ", names)}");

}

CustomData

ModInfo objects also expose CustomDataSender and CustomDataManager objects. These are handy objects that can send or sync your own custom data with other modded players.

CustomDataSender allows you to easily send data to all modded players, requiring only a function to call when data is received. You may provide a default data function to be sent to other players on initial connection. You can also specify on creation whether to include bots or yourself when sending data.

CustomDataManager operates as an extension of CustomDataSender that tracks data for each player. When host changes or a player leaves, it will automatically update or remove player/bot data from its internal storage. This abstracts away most of the player state tracking, only requiring you to add callbacks or retrieve the data when you need it.

Example code:

struct MyStruct

{

public float width;

public float height;

public MyStruct(float width, float height)

{

this.width = width;

this.height = height;

}

}

private static ModInfo _modInfo;

private static CustomDataManager _dataManager;

internal static void OnLoad()

{

_modInfo = OptionalNetworkAPI.RegisterMod("MyMod");

_modInfo.OnPlayerSpawned += OnPlayerSpawned;

_dataManager = _modInfo.AddCustomDataManager(DefaultData);

_dataManager.OnDataSet += OnDataSet;

}

private static MyStruct DefaultData(SNet_Player player)

{

return new MyStruct(player.NickName == "Dinorush"? 100: 1, 1);

}

private static void OnDataSet(SNet_Player player, MyStruct data)

{

if (data.width > data.height / 2)

Logger.Log($"{player.NickName} is wide");

else

Logger.Log($"{player.NickName} is normal");

}

private static void OnPlayerSpawned(PlayerAgent agent)

{

if (_dataManager.TryGetData(agent.Owner, out var data))

Logger.Log($"{agent.Owner.NickName} dimensions: {data.width} x {data.height}");

}

安裝與使用 安裝:按包內說明操作

  1. 解壓本站下載的壓縮包,先讀包內的 readme / 說明檔案。
  2. 多數模組依賴遊戲的模組載入器:常見做法是用社群模組管理器(如 r2modman)選中本遊戲安裝,由管理器自動處理載入器與依賴。
  3. 手動安裝時,把模組檔案放進遊戲目錄下對應的 Mod / plugins 資料夾。
  4. 啟動遊戲確認載入成功;帶「前置依賴」的模組需要把依賴一併安裝。

不同模組的安裝位置差異較大,包內說明是最可靠的依據。

前置依賴

下面這些內容需要先安裝,否則該模組不會生效:

BepInEx-BepInExPack_GTFO

檔案與校驗

檔案名稱
OptionalNetworkAPI-1.3.2.zip
檔案大小
80.3 KiB