MyceliumNetworking
An easy to use networking library for sending custom RPCs through Steam.
Screenshots 2 screenshots
About this mod Author’s original text
MyceliumNetworking For Straftat
This is a port of the library MyceliumNetworking For Content Warning
for use in Straftat mods, to allow RPC use.
See the original readme below for a usage reference.
Original Readme
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 your own custom types you will need to convert
Install & use Install: follow the bundled instructions
- Unzip the archive from playbuff and read the bundled readme / instructions first.
- Most mods rely on the game's mod loader — the usual route is a community mod manager (such as r2modman): pick this game, install, and it handles the loader and dependencies.
- For a manual install, put the mod files into the matching Mod / plugins folder inside the game directory.
- Launch the game to confirm it loaded; mods with listed dependencies need those installed as well.
Install locations vary a lot between mods — the bundled readme is the most reliable guide.
Install these first, or the mod will not work: