MyceliumNetworking
An easy to use networking library for sending custom RPCs through Steam.
截圖 共 2 張
模組介紹 作者原文
An easy to use networking library for sending custom RPCs through Steam.
A note
Landfall has asked mod developers not to send lots of data through Photon, which is the networking solution mainly used by the game. This is because they have to pay for all the bandwidth that modders use. To solve this issue, this mod was created to be used as an alternative to Photon. With MyceliumNetworking, RPCs can be used in a very similar fashion to Photon without compromising on features.
Setup
- Add MyceliumNetworkingForCW.dll as a reference in your Visual Studio solution.
- Add using MyceliumNetworking; at the top of your script.
Using RPCs
Defining RPCs
To define an RPC, simply add the [CustomRPC] attribute to a method:
[CustomRPC]
void ChatMessage(string message)
{
Debug.Log("Received chat message: " + message);
}
To get the player who sent an RPC, add a RPCInfo as the final parameter in your RPC. You don't need to send the info yourself, it will be provided automatically if you include the parameter.
[CustomRPC]
void ChatMessage(string message, RPCInfo info)
{
CSteamID sender = info.SenderSteamID;
string username = SteamFriends.GetFriendPersonaName(sender);
Debug.Log("Received chat message from " + username + ": " + message);
}
You can add as many arbitrary parameters to an RPC as you want, and they will automatically be synced when used.
[CustomRPC]
void DoSomething(string message, int num, ulong id)
{
Debug.Log("The message is " + message);
Debug.Log("The num is " + num);
Debug.Log("The id is " + id);
}
Calling RPCs
To call an RPC, first register the object with the network. Each mod needs a unique ID, which is just a random number you define unique to your mod. This is to make sure mods don't accidentally call an RPC on a separate mod. I recommend you store the modId as a constant somewhere in your program. Deregister the object when it is destroyed (for singletons or static classes, it's fine to never deregister).
const uint modId = 12345;
void Awake()
{
MyceliumNetwork.RegisterNetworkObject(this, modId);
}
void OnDestroy()
{
MyceliumNetwork.DeregisterNetworkObject(this, modId);
}
To call the RPC, use MyceliumNetwork.RPC. Pass in the mod ID, the name of the RPC you are calling, what kind of reliability you want the RPC to have (leave it at Reliable if you are unsure), and the parameters to pass in the RPC:
// This will call the RPC named ChatMessage on all clients, with the parameter "Hello World!" being sent.
MyceliumNetwork.RPC(modId, nameof(ChatMessage), ReliableType.Reliable, "Hello World!");
You can also call an RPC on a specific player using MyceliumNetwork.RPCTarget.
// This will call the RPC named ChatMessage on only the specified player, with the parameter "Hello World!" being sent.
MyceliumNetwork.RPCTarget(modId, nameof(ChatMessage), targetSteamId, ReliableType.Reliable, "Hello World!");
Using LobbyData and PlayerData
LobbyData and PlayerData are Steam features that allow you to define synced variables associated with the lobby (perfect for config syncing) or individual players. Mycelium provides an easy way to interface with them.
Lobby Data
To use lobby data, you first need to register the key. This should happen when your mod starts for the first time. This step is optional but strongly recommended. It's required if you want the LobbyDataUpdated callback to fire when that key's value is changed.
void Awake()
{
MyceliumNetwork.RegisterLobbyDataKey("foo");
}
The host can set lobby data using MyceliumNetwork.SetLobbyData and passing in a registered key with a value.
MyceliumNetwork.SetLobbyData("foo", "bar");
Values are serialized as strings (Steam requires it), so any type that can be serialized as a string can be used. To serialize custom types, override the.ToString() method.
MyceliumNetwork.SetLobbyData("money", 123);
MyceliumNetwork.SetLobbyData("scoreMultiplier", 12.52f);
MyceliumNetwork.SetLobbyData("greeting", "Hello World!");
The LobbyDataUpdated callback will be fired whenever lo
安裝與使用 安裝:BepInEx 外掛
- 本站下載的壓縮包就是模組本體,通常是 BepInEx 外掛(解壓後得到 .dll 或外掛資料夾)。
- 建議流程:用社群模組管理器(如 r2modman)選中本遊戲後匯入該模組,管理器會自動裝好 BepInEx 前置並處理依賴。
- 手動流程:先在遊戲目錄裝好 BepInEx 載入器,再把解壓出的 .dll 或外掛資料夾放進 BepInEx/plugins/。
- 啟動遊戲,BepInEx 控制台會列印載入日誌;確認這個模組出現在日誌裡即為安裝成功。
帶「前置依賴」的模組需要一併安裝,否則不會載入。功能開關通常在遊戲內或外掛自帶的設定檔裡調整。
下面這些內容需要先安裝,否則該模組不會生效: