LogoLogo
⚠️ Outdated documentationGo to LatestHomeAPI
SDK 1.0
SDK 1.0
  • Welcome
  • Overview
    • What is coherence?
    • How does coherence work?
    • Rooms and Worlds
    • Features and Roadmap
    • Release Notes
    • Known Issues and Troubleshooting
  • Learning coherence
    • Beginner's Guide to Networking Games
    • First Steps tutorial
      • 1. Basic syncing
        • 1.2. Animation parameters
        • 1.3. Sending commands
      • 2. Physics / Authority transfer
      • 3. Areas of interest
      • 4. Parenting entities
      • 5. Complex hierarchies
      • 6. Persistence
    • Campfire project
      • Game mechanics
      • Leveraging object pooling
      • Remote interactions: Chairs
      • Remote interactions: Trees
      • A unique object with complex state
      • Custom instantiation and destruction
      • Running a server-side NPC
      • Playing audio and particles
      • A simple text chat
    • How to network...
      • Racing
      • Turn-based
      • First-Person Shooter
      • MMO
      • Fighting
  • Get started
    • Installation
    • Scene Setup
      • Samples
    • Prefab Setup: CoherenceSync
    • Local Development
      • Tips and Recommendations
    • coherence Cloud
      • Create a Free Account
      • Deploy a Replication Server
      • Share Builds
  • coherence SDK for Unity
    • Components
      • CoherenceSync
      • CoherenceBridge
      • CoherenceLiveQuery
      • CoherenceTagQuery
      • Order of execution
    • Asset Management
      • Using CoherenceSyncConfig to instantiate GameObjects locally
      • CoherenceSyncConfigRegistry Save Modes
    • Networking State Changes
      • Messaging with Commands
      • Hierarchies & Child Objects
        • Child GameObjects
        • Child CoherenceSyncs
        • Deep Child CoherenceSyncs
      • Animations
      • CoherenceSync References
      • [Sync] and [Command] Attributes
      • [OnValueSynced] Attribute
      • Supported Types
      • Creating your own syncable member
    • Baking (Code Generation)
    • Scene Management
    • Authority
      • Authority transfer
      • Server-authoritative setup
    • Lifetime
      • Persistence
      • Example – a global counter
    • Optimization
      • Simulation Frequency
      • Areas of Interest
      • Level of Detail (LOD)
    • Profiling
    • Interpolation
    • Rigid Bodies
    • Settings
    • Simulation Frame
    • Replication Server
    • Simulators
      • Scripting: Client vs Simulator
      • Local Development
      • World Simulators
      • Room Simulators
      • Simulator Slugs
      • Multi-Room Simulators
      • Build and Publish
      • Command-line arguments
      • Load Balancing
    • Client-Hosting
    • Client Connections
    • Rollback Networking Support
    • World Origin Shifting
    • CLI
    • Upgrading Unity SDK
      • Upgrading to coherence Unity SDK 1.0.0
      • Upgrading to coherence Unity SDK 0.9.0
  • coherence Cloud
    • Developer Portal
    • Dashboard
    • Worlds
    • Rooms
    • Lobbies
    • Game Services
      • Account
      • Key-Value Store
    • Using coherence Cloud in Unity
      • Worlds
      • Rooms
      • Lobbies
      • Game Services
        • Authentication Service (Player Accounts)
        • Key-value store
  • Schema explained
    • Overview
    • Specification
    • Field settings
    • Archetypes
  • coherence Scripting API
  • Additional resources
    • Community
    • Quick Samples
    • Continuous Integration
    • Unreal Engine Support
    • WebGL Support
    • Peer-to-Peer Support (P2P)
    • Pricing
    • SLA
    • Glossary
Powered by GitBook
On this page
  • Our use case
  • Getting extra mileage with byte[]
  • Never miss a chat message
  • Long-form chats

Was this helpful?

Export as PDF
  1. Learning coherence
  2. Campfire project

A simple text chat

Last updated 1 year ago

Was this helpful?

Topics covered

Chat |

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

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:

private void SendText(InputAction.CallbackContext _)
{
    string message = _chatComposer.GetText();
    if (message != string.Empty)
    {
        byte[] encodedMessage = Encoding.UTF8.GetBytes(message);
        sync.SendCommand<Chat>(nameof(SendChatMessage), MessageTarget.Other, encodedMessage);
    }
    // ...
}

Then, on the receiving side, we reconvert it back into a string:

[Command(defaultRouting = MessageTarget.Other)]
public void SendChatMessage(byte[] encodedMessage)
{
    string decodedMessage = Encoding.UTF8.GetString(encodedMessage);
    chatVisualiser.ShowText(decodedMessage);
    // ...
}

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.

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.

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: . By sending the Network Command on that, it would ensure that the Command is sent and received regardless of LiveQuery ranges.

Read the for more info.

For more information about Lobbies, read .

Client Connections
Client Connections page
the specific page
Lobbies
The chat composer
A chat balloon