Client messages are a form of commands that are used for client-to-client communication rather than client-to-entity. To achieve this type of communication a special connection entity is automatically created by the replication server for each connected client. Those entities are subject to a different rule set than standard entities. Connection entities:
Can't be created or destroyed by the client - they are always replication server-driven
Are global - they are replicated across clients regardless of the in-simulation distance or live query radius
Client messages shine whenever there's a need to communicate something to all the connected players. Usage examples:
Global chat
Game state changes: game started, game ended, map changed
Server announcements
Server-wide leaderboard
Server-wide events
The globality of client messages doesn't fit all game types - for example, it rarely makes sense to keep every client informed about the presence of all players on the server in an MMORPG (think World Of Warcraft). Due to this reason, client messages are turned off by default. To enable client messages:
Global Query On
toggle)Global query enables querying for entities containing a global component, including a connection entity.
Disabling global query on one client doesn't affect other clients, i.e. connection entity of this client will still be visible to other clients that have the global query turned on.
CoherenceClientConnection
classUnder the hood, client messages are nothing but commands sent between the connection entities. Each such entity is represented on the client side by an instance of a CoherenceClientConnection
class. Implementing client messages is as simple as adding a new command to our custom connection class:
Next, we have to create a binding:
This step is described in detail in the Prefab setup section. In short, a prefab with CoherenceSync
and our PlayerConnection
must be created and placed in the Resources directory:
For the system to know which object to create for every new client connection, we have to link our prefab to the Coherence MonoBridge. Simply drag the prefab to the Client Connection Prefab
field in the MonoBridge inspector:
Once we have everything set up, whenever a new client joins an instance of the connection prefab will be created. The instantiated object resembles a client connection and will be kept alive for as long as that client remains connected to the replication server.
To get notified about the creation and destruction of connections we can use the following events:
The connection ID is simply an EntityID of the connection entity.
To get local connection or connection of any client by the ID use the following MonoBridge methods:
A null may be returned by both methods if client messages are not enabled or the connection with a given ID doesn't exist.
The CoherenceClientConnection
class exposes the following properties:
Client messages are sent using the SendClientMessage
method of the CoherencePlayerConnection
class:
The first message will be delivered only to the player that owns the created connection. The other message will be delivered to all instances of this connection, that is, every player (including the sender) that knows about this connection will receive this message.
If the connection ID of the message recipient is known we can use the MonoBridge to send a client message: