Simulator Query Transfer
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);
}
}
Last updated
Was this helpful?

