A simple text chat
Topics covered
Chat | Lobbies
Communication is an inherent part of online games and a chat, however simple, is a great way to enhance the range of expression for the players.
Our use case
![](https://docs.coherence.io/~gitbook/image?url=https%3A%2F%2F352971571-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FOnNWMLfYsbCzCnIbNrcP%252Fuploads%252FC9i2xZi2K5nIhjBUOX7w%252FChatComposer.png%3Falt%3Dmedia%26token%3Dd557aec7-28b8-451c-99f4-645cf68164e0&width=768&dpr=4&quality=100&sign=7b18a605&sv=1)
![](https://docs.coherence.io/~gitbook/image?url=https%3A%2F%2F352971571-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FOnNWMLfYsbCzCnIbNrcP%252Fuploads%252F4GlIflrCLOTNnB63e2Xp%252FChatBalloon.png%3Falt%3Dmedia%26token%3Df5d30b5d-2c12-4504-99c2-34382daa9534&width=768&dpr=4&quality=100&sign=dc8b4a61&sv=1)
We wanted to implement a very simple chat system. By pressing Enter, a small screen-space UI opens up and allows the player to compose a message. When they press Enter again, a balloon on top of their character displays the message to them, and to all connected Clients.
This is done in three parts.
The Chat
script on the player reads the input, requests ChatComposerUI
to display the chat composer that is part of the screen-space scene UI.
When the player sends a chat message, Chat
is informed by an event sent by ChatComposerUI
, and sends a Network Command SendChatMessage
to all other clients.
Finally, the received message is displayed in world-space over the player's head the script ChatVisualiserUI
present in the Player Prefab.
Getting extra mileage with byte[]
By default, coherence's Network Commands have a limit in the length that can be sent in one command. This is limited by the length of a UDP packet. While this limitation might be removed in the future, for now it means that chat messages can't be longer than a certain amount.
This amount, however, is quite different depending if you use a parameter of type string
or of type byte[]
(byte array). If you send a string
, you will be able to pass on around 50 characters. This is really not much for a chat system.
If you use byte[]
though, the number of characters goes up to (around) 500. Now we're talking!
So what we do in this demo is that first we convert the string
that the player has typed in the UI into a byte array, and we send that via Network Command:
Then, on the receiving side, we reconvert it back into a string
:
This simple trick allows us to send longer messages, or to send the same message generating less traffic.
Never miss a chat message
Because we are sending the chat messages on the CoherenceSync
that is on the Player Prefab, it means that if that particular player instance is not visible to a Client because it's outside of their LiveQuery, they won't receive the Network Command and thus the chat message. This is maybe desirable in this demo, where the chat is visualised on top of the player.
But if chat messages are shown in a UI panel and players should receive them all regardless, then it might make more sense to rely on a special type of CoherenceSync
: Client Connections. By sending the Network Command on that, it would ensure that the Command is sent and received regardless of LiveQuery ranges.
Read the Client Connections page for more info.
Long-form chats
This page talked about a simple chat system to use during gameplay, but keep in mind that coherence also has a solution for long-form chats as part of Lobby rooms. Players can be in a lobby before but also during gameplay.
For more information about Lobbies, read the specific page.
Last updated