# Multiple Connections within a Game Instance

In some cases, you might want your game to create multiple individual connections within the same game instance. This could be the case when you want to support [split-screen scenarios](#user-content-fn-1)[^1], or to create development environments (interpolation visualization, stress-testing, debug network scenarios, quick prototyping within the Editor, etc.).

To understand how to achieve this, it's important you understand how **coherence** establishes a connection.

## What Makes a Network Connection

[CoherenceBridge](https://docs.coherence.io/1.6/manual/components/coherence-bridge) is the core component that handles a network connection with coherence. This component is responsible of establishing the connection, and also to keep in sync [network state](#user-content-fn-2)[^2] and [scene state](#user-content-fn-3)[^3].

One CoherenceBridge is able to handle one network connection, and keep in sync the network state within one scene: the one it was instantiated on.

{% hint style="success" %}
1 CoherenceBridge = 1 network connection = 1 scene.
{% endhint %}

Knowing this crucial rule, if we want N connections, we will need N scenes, each of them with a bridge. Each bridge can establish a connection with any endpoint. So technically, a game instance can simultaneously connect to a combination of Rooms and Worlds. Including several connections to the same Rooms or Worlds, as it would be the case for a split-screen setup.

You can create this scenario by your own means. However, we do provide a solution out-of-the-box that helps handle connections: the CoherenceSceneLoader and CoherenceScene component duo.

## Simplifying Connections: CoherenceSceneLoader & CoherenceScene

At its core, what these components orchestrate is loading scenes additively, and associate them with network connections.

<table><thead><tr><th width="209">Unity Component</th><th>Description</th></tr></thead><tbody><tr><td>CoherenceSceneLoader</td><td>Handles loading one scene additively, and holds instructions for how that loaded scene should connect to the network.</td></tr><tr><td>CoherenceScene</td><td>Reacts when the scene this component is on is loaded via CoherenceSceneLoader. When it does, establishes a connection using the <a href="components/coherence-bridge">CoherenceBridge</a> on said scene. The connection data used (EndpointData) is facilitated by the CoherenceSceneLoader that triggered the scene load.</td></tr></tbody></table>

{% hint style="info" %}
One CoherenceSceneLoader can only load/unload one scene at a time. If you want to load N scenes, you will need N CoherenceSceneLoaders.
{% endhint %}

In the following section we explore a practical use case of this feature.

## Tutorial — 4 players, 1 Editor

Let's say we want to prototype a scenario where there's 4 players connected to the network. And we want to do this within one game instance (in this case, within the Editor), to serve as a very quick and rough development environment where we can observe how the network reacts to anything we throw at it.

We can achieve that with CoherenceSceneLoader and CoherenceScene.&#x20;

We need a total of 3 CoherenceSceneLoaders — these will be our extra connections, so that we have a total of 4 players connecting from within our game instance.

By the end of this tutorial, we will have a setup that looks like this:

<figure><img src="https://3473996028-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8utFtIPIKpqSCWkfmEu3%2Fuploads%2FhWM50cJLWo1TuLUZLHhG%2Fimage.png?alt=media&#x26;token=45d5b91a-9018-436d-b2b6-3ef6c088206d" alt=""><figcaption></figcaption></figure>

Let's get to it. I'm starting with an empty scene that has a Camera, a [CoherenceBridge](https://docs.coherence.io/1.6/manual/components/coherence-bridge) and a [CoherenceLiveQuery](https://docs.coherence.io/1.6/manual/components/coherence-live-query).

First, let's create the CoherenceSceneLoaders on our scene. Remember, we're creating 3 of them.

<figure><img src="https://3473996028-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8utFtIPIKpqSCWkfmEu3%2Fuploads%2FlMoSK4faFnymTNWj0wA1%2Fimage.png?alt=media&#x26;token=d20fd028-7129-4161-a357-4f50570bcbba" alt=""><figcaption><p><strong>GameObject > coherence > Scene Loading > Coherence Scene Loader</strong> menu item</p></figcaption></figure>

<figure><img src="https://3473996028-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8utFtIPIKpqSCWkfmEu3%2Fuploads%2FyJlmKxLDfPFQke6GwVt9%2Fcoherencesceneloader.png?alt=media&#x26;token=68b827f8-7bff-472c-b4c5-c441e1575226" alt=""><figcaption><p>CoherenceSyncLoader Inspector</p></figcaption></figure>

Select the 3 of them, and on the Inspector:

* Enable **Attach**
* Set the **Connection Type** to **Client**
* Set the **Scene Name** to the name of the current scene (if it's not already)

<figure><img src="https://3473996028-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8utFtIPIKpqSCWkfmEu3%2Fuploads%2Fq4AZ1t3tBifDkDtiS1BT%2Fimage.png?alt=media&#x26;token=8591b9e9-3523-4595-8bac-3edc2a6d121f" alt=""><figcaption><p>Values set on the Inspector, as described above</p></figcaption></figure>

Let's break down why we're doing this:

In this example, we're going to be loading the same scene we're already working on, 3 additional times (one time per CoherenceSceneLoader component). We set Scene Name accordingly (in this case, it's **Main**).

This is, however, not the only way you can think of setting this environment up. A different approach could be to have 4 CoherenceSceneLoaders, and start from a **Menu** scene. But in this example I didn't go that route — we're working with our only scene.

{% hint style="info" %}
Make sure the scene you're working on is part of the Build Profile's Scene List.&#x20;
{% endhint %}

With this setup, every time this scene connects to the network, 3 additional scenes will be additively loaded and connected (as Clients, as specified in the Connection Type) to the same space (room or world), meaning there will be a total of 4 players coexisting.&#x20;

I'm going to use the **Rooms Connect Dialog** to not care about the connection logic. You can find it in the **Explore Samples** window (**coherence > Explore Samples** menu item).

<figure><img src="https://3473996028-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8utFtIPIKpqSCWkfmEu3%2Fuploads%2FJOQcb1XyHLbYqQ1aRGqJ%2Fimage.png?alt=media&#x26;token=00ce2c57-f675-4cdf-a785-b2d62864eee4" alt=""><figcaption></figcaption></figure>

{% hint style="warning" %}
At this point, make sure your baked data is up-to-date.

If using coherence Cloud, make sure the schemas are uploaded.\
You can do this from the Hub (**coherence > Hub** menu item).&#x20;

If using a local Replication Server, make sure it's stopped/started after baking, so it uses the latest generated schema. You can start a local Replication Server from the Hub as well.
{% endhint %}

Enter Play Mode and connect to coherence using the in-game connection dialog. When creating the room, make sure **Max Players** is set to **4 or more** (default is 10).

Once connected to the room (i.e., the CoherenceBridge establishes connection), the 3 CoherenceSceneLoaders will trigger — as they are **attached** to the scene's CoherenceBridge — and load 3 additional scenes, that will automatically connect to the network as well.

<figure><img src="https://3473996028-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8utFtIPIKpqSCWkfmEu3%2Fuploads%2F571sUVPC34Sm2gmrPgQZ%2Fimage.png?alt=media&#x26;token=3ffdd134-3908-4ffe-93f0-6c29cf352a7c" alt=""><figcaption><p>Hierarchy after entering the room — 4 client connections established!</p></figcaption></figure>

You can click on the Unity icon at the right side of the GameObject, in the Hierarchy Window, to ping which CoherenceScene is associated with each CoherenceSceneLoader.

<figure><img src="https://3473996028-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8utFtIPIKpqSCWkfmEu3%2Fuploads%2Fc3RLH69bX6v10vKNDOvw%2Fimage.png?alt=media&#x26;token=ede8e073-d755-4418-bb89-40983737d532" alt=""><figcaption><p>Clicking on the Unity icon on CoherenceScene or CoherenceSceneLoader pings its counterpart</p></figcaption></figure>

The 3 CoherenceSceneLoaders are reacting to the CoherenceBridge getting connected/disconnected. This is because they are **attached** to CoherenceBridge. If we didn't attach, we'd need to manually call `CoherenceSceneLoader.Load(EndpointData)` and pass the data needed to perform the operation.

Notice also how the CoherenceScene component has been automatically added for us on each of the loaded scenes. Warnings will appear on the Console window, telling us that a CoherenceScene has been created on-the-fly.

We want to control what happens when we load a scene via CoherenceSceneLoader, so creating a CoherenceScene on our own, and configuring it our way, is what we should do next.

Let's exit Play Mode and add a CoherenceScene:

<figure><img src="https://3473996028-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8utFtIPIKpqSCWkfmEu3%2Fuploads%2FdihyvKuxTwMgRFVneY1E%2Fimage.png?alt=media&#x26;token=efff8402-1433-4258-bf0c-8d923314f6c8" alt=""><figcaption><p><code>GameObject / coherence / Scene Loading / Coherence Scene</code> menu item</p></figcaption></figure>

<figure><img src="https://3473996028-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8utFtIPIKpqSCWkfmEu3%2Fuploads%2FwHWDYGEk57O9jd2Dt5ed%2Fimage.png?alt=media&#x26;token=26efcdcf-e96c-4f96-8ba8-21aaf948ac2a" alt=""><figcaption><p>CoherenceScene Inspector</p></figcaption></figure>

By default, this component will try to establish a connection once loaded (**Connect** toggle is on), and also handles reconnects out-of-the-box.&#x20;

What's interesting for us at this point though, is disabling some logic from the scenes after they are loaded. Specifically, we want the Camera, the EventSystem and the Connection Dialog on the loaded scenes to be disabled, so that they don't interfere with the ones present in the main scene.

CoherenceScene provides a handy **Deactivate On Load** list that you can use for this matter, but you can also use the On Loaded event to script any logic you might want.

The Deactive On Load list also comes with a **Smart Select** button, that selects GameObjects found that are generally good candidates to be disabled when working with this feature.

By clicking on it, Camera and EventSystem are populated, and will be disabled after the scene is loaded. We also want to disable the Rooms Connection Dialog, so we reference it there manually.

<figure><img src="https://3473996028-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8utFtIPIKpqSCWkfmEu3%2Fuploads%2FRD7XrpRbfVgI684loFX8%2Fimage.png?alt=media&#x26;token=ca4182c2-d673-44e7-b5af-f574a5edc46e" alt=""><figcaption></figcaption></figure>

Let's save the changes and see what's happening now once we enter Play Mode.

1. Via the in-game Connection Dialog, create or join a room.
2. Once joined, notice the [4 active connections](#user-content-fn-4)[^4] on the **Hierarchy** window.
3. Now, click on the **Disconnect** button of the Connection Dialog — it should be clickable after we've disabled the EventSystems, Cameras and Connection Dialogs from the additional scenes.\
   ![](https://3473996028-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8utFtIPIKpqSCWkfmEu3%2Fuploads%2FPNnQxmSfTTRSct1g6PfY%2Fimage.png?alt=media\&token=dcb76e74-aa93-4a1b-83eb-fe939ae0ed69)
4. After disconnecting, you'll notice the other 3 connection have also disappeared — attaching loads **and also unloads** when, CoherenceBridge connects/disconnects.

So far we haven't seen anything being networked. So that's what we're going to do next.

Let's create a Cube and network it.

<figure><img src="https://3473996028-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8utFtIPIKpqSCWkfmEu3%2Fuploads%2F6ubhHiioYWNfwMg6wkcR%2Fimage.png?alt=media&#x26;token=f10de691-fee1-4944-bf15-a46a29fb83f0" alt=""><figcaption><p>Let's create a Cube</p></figcaption></figure>

<figure><img src="https://3473996028-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8utFtIPIKpqSCWkfmEu3%2Fuploads%2F6vwMDkTYcrSsXaN4mbmJ%2Fimage.png?alt=media&#x26;token=f74a5430-31d6-4320-9f96-6f2c60197b7a" alt=""><figcaption><p>Cube created, now let's network it by clicking <strong>Sync with coherence</strong> on the Inspector.</p></figcaption></figure>

<figure><img src="https://3473996028-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8utFtIPIKpqSCWkfmEu3%2Fuploads%2FCjijl4TKhb3WOQDShFSG%2Fimage.png?alt=media&#x26;token=0a1a50c6-9b8b-4db6-aa47-d23d56b46380" alt=""><figcaption><p>All set! Our Cube is now networkable. It's also part of the scene right now.</p></figcaption></figure>

{% hint style="warning" %}
At this point, make sure you bake, upload schemas and or (re)start the Replication Server, given we've created a new Networkable Prefab. You can do this from within the Hub, from the **coherence menu**, or from the icon shown in the **Project** window, next to the `Assets/coherence` folder.
{% endhint %}

Now, let's enter Play Mode again.

<figure><img src="https://3473996028-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8utFtIPIKpqSCWkfmEu3%2Fuploads%2FIIE8GYbcHMOfmCtZ7GoM%2Fimage.png?alt=media&#x26;token=8549c6cd-c499-419e-bc11-a79189917d41" alt=""><figcaption></figcaption></figure>

We now see 4 cubes — one per connection. You can differentiate which one you have authority over, and which ones are the replicated ones (solid coherence logo represents authority, outlined coherence logo represents non-authority / remote).

Now, let's spice it up — let's use this setup to **visualize interpolation at runtime**.

1. Create two materials. We will use these to differentiate authority and remote. I'll be using red and green.

<figure><img src="https://3473996028-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8utFtIPIKpqSCWkfmEu3%2Fuploads%2FdOcHY7ZxQDJTz4LfJvto%2Fimage.png?alt=media&#x26;token=9ef37662-7cd9-4d59-987a-f00a0da75253" alt=""><figcaption></figcaption></figure>

2. Next, let's assign apply these materials on our Cube, based on authority.
3. Go to the Cube Prefab, and hit **Configure** on the **CoherenceSync Inspector**.
4. Once there, click the **Components tab**.

<figure><img src="https://3473996028-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8utFtIPIKpqSCWkfmEu3%2Fuploads%2FmjJm6FRXsP9euRtd6fZf%2Fimage.png?alt=media&#x26;token=be8dca6a-81b7-43bc-b4a8-28b4bd648fab" alt=""><figcaption></figcaption></figure>

5. Open the **Action** dropdown, under **Mesh Renderer**, and select **Handle Material**:

<figure><img src="https://3473996028-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8utFtIPIKpqSCWkfmEu3%2Fuploads%2FD0S8s4EaQWddiDcxgLW6%2Fimage.png?alt=media&#x26;token=6aa2d40a-992e-41eb-b594-196f5aa10bbb" alt=""><figcaption></figcaption></figure>

6. Use our previously created materials, as such:

<figure><img src="https://3473996028-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8utFtIPIKpqSCWkfmEu3%2Fuploads%2FFmiYKbzZklgPlCdmCba0%2Fimage.png?alt=media&#x26;token=fdeacca7-5ad3-411c-92b8-774d8b329e1c" alt=""><figcaption></figcaption></figure>

We're almost there. Let's enter Play Mode again.

Once connected, select a Cube you [have authority over](#user-content-fn-5)[^5] from any of the loaded scenes.

<figure><img src="https://3473996028-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8utFtIPIKpqSCWkfmEu3%2Fuploads%2F9ILhBb2ilBOLUmMAXqCF%2Fimage.png?alt=media&#x26;token=de5ba758-dc4b-41f2-9958-1e47b76282d7" alt=""><figcaption><p>Keep the Scene view active, we will use it to visualize what's going on</p></figcaption></figure>

Drag that Cube around, so that it doesn't overlap with the other (replicated) Cubes.

Do you see those eye icons on the scene headers, on the Hierarchy window? They control what your Scene window is rendering. While the Cube you dragged is selected, let's visualize what's happening on the other connections. Enable rendering on any of the other scenes:

<figure><img src="https://3473996028-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8utFtIPIKpqSCWkfmEu3%2Fuploads%2Fc1S5IbX27ftiTGGizAwS%2Fimage.png?alt=media&#x26;token=fcdc1d7d-c062-4a81-9485-be29e36c5b61" alt=""><figcaption></figcaption></figure>

Now, drag the Cube again. Do you see a red Cube following it along? That's the replicated copy, catching up and being interpolated!

<figure><img src="https://3473996028-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8utFtIPIKpqSCWkfmEu3%2Fuploads%2FzHcZGnVvTiKbhIdfBMtN%2Finterp.gif?alt=media&#x26;token=22f079d2-f850-4cb4-b77f-b0e5b79bf878" alt=""><figcaption></figcaption></figure>

Now you can try out how interpolation behaves when using different settings.

<figure><img src="https://3473996028-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8utFtIPIKpqSCWkfmEu3%2Fuploads%2FWynvGbdj45HkuTGpyY8x%2Fimage.png?alt=media&#x26;token=8341a242-5b01-40c0-bd10-aaac66b32ccb" alt=""><figcaption></figcaption></figure>

Now you have a sandbox you can use to toy around with. Drag and drop Networked Prefabs on the scene, delete them, change authority on-the-fly... and all of that within one Scene view, with the debugging advantages that it brings.

If you've made it all the way here, congratulations! Now you grasp the basics needed to leverage CoherenceSceneLoader / CoherenceScene to create multiple connections within one game instance.

One final note — building a game where different connections coexist on the same build can be challenging, and requires strict rules on which scene can access what. Static access (GameManager and other singletons), physics, graceful error handling...&#x20;

While this feature is powerful to provide quick development scenarios, it's not necessarily a good foundational feature to build upon to playtest a game itself. For that, we recommend you to either make builds, use Unity's Multiplayer Play Mode or ParrelSync. Read more about our recommendations on [how to test your game locally](https://docs.coherence.io/1.6/getting-started/setup-a-project/local-development).

[^1]: Multiple Connections within a game instance is not the only way to solve this. You might want to simplify the problem by having your one connection handle multiple players.

[^2]: what the Replication Server sees

[^3]: what Unity sees

[^4]: 4 `Main` scenes loaded, 3 of them loaded via CoherenceSceneLoader. All of them connected to the same room.

[^5]: The one with the coherence **solid logo** on the Hierarchy window.
