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 you can create a live query Prefab that has a CoherenceSync 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.
using UnityEngine;
using System.Collections.Generic;
using Coherence.Connection;
using Coherence.Toolkit;
public class SimulatorConnectionHandler : MonoBehaviour
{
public CoherenceBridge bridge;
public CoherenceSync liveQueryPrefab;
private Dictionary<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);
}
}
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 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.
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.
Last updated
Was this helpful?