Show / Hide Table of Contents

Class ModNetworking

Networking system for mods.

Inheritance
Object
ModNetworking
Namespace: Modding
Assembly: Assembly-CSharp.dll
Syntax
public static class ModNetworking
Remarks

Defining MessageTypes

To use network messages from your mod, you first need to define the possible message types. To define a message type, call CreateMessageType. If you want to attach data to your messages, pass corresponding DataType parameters. The function returns a MessageType object which you will need to keep around.

Sending Messages

To create messages, call CreateMessage on the corresponding MessageType object. Pass along values for all data parameters you have defined when creating the message type. To send a message once you have created it, call SendTo or SendToAll and pass the Message object. The receive callbacks will not be called on the instance sending the message.

Receiving Messages

There's two ways to act on received messages:

  1. You can add a handler to the MessageReceived event. It will get called for every message your mod receives.
  2. You can add per-MessageType event handlers using the Callbacks property. The Player property of the Message object you receive is the sender of the message.
Examples

Create a message type:

	public static MessageType TestType;
	void OnLoad() {
		TestType = ModNetworking.CreateMessageType(DataType.Single);
	}

Register event handlers:

	ModNetworking.MessageReceived += message => {
		// This will get called for all message types.
	};
	ModNetworking.Callbacks[TestType] += message => {
		// This will only get called for TestType messages.
	};

Send a message:

	var message = TestType.CreateMessage(20f);
	ModNetworking.SendToAll(message);

Fields

Callbacks

A set of events for each registered message type. Index by message type, then use the result like a normal event, i.e. += someHandler;.

Declaration
public static ModNetworking.CallbacksWrapper Callbacks
Field Value
Type Description
Modding.ModNetworking.CallbacksWrapper

Properties

IsNetworkingReady

Whether the connection has been initialized far enough so that mod networking can work. If any networking methods are used while this is false, they will not have any effect. (This does not affect methods such as registering message types or callbacks.))

Declaration
public static bool IsNetworkingReady { get; }
Property Value
Type Description
Boolean

Methods

CreateMessageType(DataType[])

Creates a new message type with the specified data attached. Note that order matters in creating message type, if the message types are created in different order on different instances, receiving messages will not work correctly.

Declaration
public static MessageType CreateMessageType(params DataType[] types)
Parameters
Type Name Description
DataType[] types
Returns
Type Description
MessageType

SendInSimulation(Message)

Sends a message to every instance affected by the current simulation.

Declaration
public static void SendInSimulation(Message message)
Parameters
Type Name Description
Message message
Remarks

Does not do anything when the local instance is in local simulation. Otherwise, sends the message to every player that is participating in global simulation or is in build mode.

SendTo(Player, Message)

Sends a message to a specific player.

Declaration
public static void SendTo(Player player, Message message)
Parameters
Type Name Description
Player player
Message message

SendToAll(Message)

Sends a message to all instances in the current multiplayer game.

Declaration
public static void SendToAll(Message message)
Parameters
Type Name Description
Message message

SendToHost(Message)

Sends a message to the host.

Declaration
public static void SendToHost(Message message)
Parameters
Type Name Description
Message message

Events

MessageReceived

Event that fires whenever a message is received. Called for all message types.

Declaration
public static event Action<Message> MessageReceived
Event Type
Type Description
Action<Message>
Back to top Generated by DocFX