MyceliumNetworking
An easy to use networking library for sending custom RPCs through Steam.
Screenshots 2 screenshots
About this mod Author’s original text
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
Install & use Install: BepInEx plugin
- The archive from playbuff is the mod itself — usually a BepInEx plugin (a .dll or a plugin folder once unzipped).
- Recommended: use a community mod manager (such as r2modman) — pick this game, import the mod, and the manager sets up BepInEx and its dependencies for you.
- Manual route: install the BepInEx loader in the game folder, then drop the unzipped .dll or plugin folder into BepInEx/plugins/.
- Launch the game — the BepInEx console logs every plugin it loads; seeing this mod in the log means it is installed.
Mods with listed dependencies need those installed too, or they will not load. Options are usually changed in-game or in the plugin's config file.
Install these first, or the mod will not work: