Server Messages
Messages send information over the network. Use-cases of Network Messages can be sending a specific sound id to play, or any other need send data over for a temporary or in-the-moment action.
Server Messages can only be used on the server/host.
WARNING
Any clients attempting to run actions/methods from the server message class will result in nothing happening.
Any clients attempting to send messages from the server message class will result in errors.
Constructor
Two things need to be specified for constructing messages, the identifier and the type of the data transmitted.
LethalServerMessage<TData> customServerMessage = new LethalServerMessage<TData>(identifier: "customIdentifier");TData is the data type of the message, which can be any serializable type - practically any type. Examples of serializable types are:
stringintVector3Color
You must replace TData in the above example with the data type you want to use.
NOTE
This identifier will be shared between server and client messages of the mod.
Any other mods with the same identifier or any events or variables of the same identifier will not interact with your message.
Methods
There are three available methods to use on your Server Message.
INFO
Anytime TData is used in this doc, it refers to the type specified in the message constructor.
Send to All Clients
The most commonly used method is .SendAllClients(TData data, bool receiveOnHost = true). This will send data to all Client Messages (including the host by default) with the same identifier. To use, simply do the following:
- (Optional)
receiveOnHostdefines whether the message should be received on the host client as well.
customServerMessage.SendAllClients(data);TIP
It is highly recommended to leave receiveOnHost as true and instead program your OnReceive event/method of Client Messages to be received on all clients instead of every client but the host.
Send to a Specific Client
A much less commonly used, but still important, method is .SendClient(TData data, ulong clientId). This will send data to the Client Message of the specified client. For example, to send data to the Client Message for a client from their PlayerControllerB, one can do the following:
customServerMessage.SendClient(data, playerController.actualClientId);Send to Specific Clients
The least commonly used method is to send data to specific clients: .SendClients(TData data, IEnumerable<ulong> clientIds). This will send data to the Client Messages of the specified clients.
customServerMessage.SendClients(data, new List<ulong> { 0, 1 })TIP
Because the parameter is an IEnumerable<ulong>, you can send any list, collection, or array as a parameter.
Event/Delegate
There is one event to subscribe to in order to receive messages from clients. OnReceived will have two parameters passed along, TData data and ulong originatorClientId. This is the client Id of the client who messaged the server via a Client Message of the same identifier.
customServerMessage.OnReceived += ReceiveFromClient;
private void ReceiveFromClient(TData data, ulong clientId)
{
//...
}TIP
You can get the PlayerControllerB from a client Id by using the GetPlayer extension method.