# Simulator Query Transfer

In an advanced Simulator setup, where entity creation is restricted to the Simulator, it is not possible for a Client to create their own queries. In [an unrestricted setup](https://docs.coherence.io/authority/server-authoritative-setup#interaction-with-livequeries) you can create a live query Prefab that has a [CoherenceSync ](https://docs.coherence.io/manual/components/coherence-sync)behaviour with the *Simulate On* property set to *Server Side with Client Input*. However, in order to provide a Client with a live query in an entity-restricted setup, the Simulator has to create the live query on behalf of the Clients and transfer the input authority back.

<pre class="language-csharp"><code class="lang-csharp"><strong>using UnityEngine;
</strong>using System.Collections.Generic;
using Coherence.Connection;
using Coherence.Toolkit;

public class SimulatorConnectionHandler : MonoBehaviour
{
    public CoherenceBridge bridge;
    public CoherenceSync liveQueryPrefab;

    private Dictionary&#x3C;ClientID, CoherenceSync> clientQueries = new();

    public void Start()
    {
        bridge.ClientConnections.OnCreated += HandleClientCreated;
        bridge.ClientConnections.OnDestroyed += HandleClientDestroyed;
    }

    public void OnDestroy()
    {
        bridge.ClientConnections.OnCreated -= HandleClientCreated;
        bridge.ClientConnections.OnDestroyed -= HandleClientDestroyed;
    }

    private void HandleClientCreated(CoherenceClientConnection client)
    {
        if (client.IsMyConnection)
        {
            // This example is only creating live queries for other clients.
            return;
        }

        // Create a live query for the client and transfer the input authority so
        // they get the benefit of the query, but are not allowed to modify it 
        // directly.
        var clientQuery = Instantiate(liveQueryPrefab);
        clientQuery.TransferAuthority(client.ClientId, Coherence.AuthorityType.Input);

        clientQueries.Add(client.ClientId, clientQuery);
    }

    private void HandleClientDestroyed(CoherenceClientConnection client)
    {
        if (client.IsMyConnection)
        {
            return;
        }

        // Destroy the query that belonged to the disconnected client.
        var clientQuery = clientQueries[client.ClientId];
        Destroy(clientQuery.gameObject);

        clientQueries.Remove(client.ClientId);
    }
}
</code></pre>

Constructing a Prefab that has Simulator authority but benefits a Client with the area of interest is simple. It requires that the *Authority Transfer* mode is *Request* and that there is a [Coherence Live Query](https://docs.coherence.io/manual/components/coherence-live-query) component. It is also possible to add any kind of query to these Prefabs or multiple types and the Client will benefit from them all.

<figure><img src="https://content.gitbook.com/content/CMCtKgV0bk1lwR4tWK3W/blobs/d4phKimgwqj1ubVMyBNz/image.png" alt=""><figcaption><p>The <strong>Simulate in</strong> field is set to <em>Client Side</em>.</p></figcaption></figure>

{% hint style="warning" %}
Note that the **Simulate In** field is set to *Client Side*. This is appropriate for this setup even though the state authority of this entity is a Simulator, since from the point of view of the Replication Server all Simulators are Clients as well.
{% endhint %}
