# Authority transfer

Authority over an Entity is **transferrable**, so it is possible to move the authority between different Clients or even to a Simulator. This is useful for things such as balancing the simulation load, or for exchanging items. It is possible for an Entity to have no Client or Simulator as the authority - these Entities are considered orphaned and are not simulated.

## Types of authority transfer

In the design phase, **CoherenceSync** objects can be configured to handle authority transfer in different ways:

* **Request**. Authority transfer may be requested, but it may be rejected by the current authority.
* **Steal.** Authority will always be given to the requesting party on a FCFS ("first come first serve") basis.
* **Disabled**. Authority cannot be transferred.

<figure><img src="https://content.gitbook.com/content/8utFtIPIKpqSCWkfmEu3/blobs/XLijjMeSGLJ3H4RFBLoh/CoherenceSync_Baked.png" alt="" width="563"><figcaption></figcaption></figure>

When using Request, an optional callback `OnAuthorityRequested` can be set on the **CoherenceSync** behaviour. If the callback is set, then the results of the callback will override the Approve Requests setting in the behaviour.

The request can be approved or rejected in the callback.

```csharp
// connectionID is the network connection ID of the requesting client.
// sync is the CoherenceSync behaviour of the object the other client is
// requesting authority over.
public bool OnRequest(ushort connectionID, CoherenceSync sync)
{
    return (sync == theRightSync && connectionID == goodConnectionID);
}
```

{% hint style="info" %}
Support for requests based on [`CoherenceClientConnection.ClientID`](https://docs.coherence.io/1.6/manual/client-connections) is coming soon.
{% endhint %}

### Auto-adopt Orphan

When **Lifetime** is set to **Persistent**, you will see an extra checkbox called **Auto-adopt Orphan**.

Enabling this option makes it so that if the entity is abandoned by its owner, as soon as possible the Replication Server will assign it to a Client again. This can be useful for instance in a big game world, where entities often go out of LiveQueries. When they are first seen again by a Client, the **Auto-adopt Orphan** option ensures that the Client takes over that entity (i.e. its State authority) without you having to write code for it.

{% hint style="warning" %}
**Note:** If you abandon an entity but it's still in your LiveQuery, on the next frame the Replication Server might assign it to you again. If you want more control over that, then perhaps you should turn **Auto-adopt Orphan** off, and implement callbacks to the authority events for that entity.
{% endhint %}

## Requesting authority in code

Requesting authority is very straight-forward.

<pre class="language-csharp"><code class="lang-csharp">var coherenceSync = target.GetComponent&#x3C;CoherenceSync>();
<strong>bool requestSuccess = coherenceSync.RequestAuthority(AuthorityType.Full);
</strong></code></pre>

`RequestAuthority` returns `false` if the request was not sent. This can be because of the following reasons:

* The sync is not ready yet.&#x20;
* The entity is not allowed to be transferred because`authorityTransferType` is set to `NonTransferable`.
* There is already a request underway.
* The entity is orphaned, in which case you must call `Adopt` instead to request authority.

The request itself might fail depending on the response of the current authority.&#x20;

As the transfer is asynchronous, we have to subscribe to one or more **UnityEvents** in **CoherenceSync** to learn the result.

Also because of their asynchronous nature, clients can receive [commands](https://docs.coherence.io/1.6/manual/networking-state-changes/commands) for entities that they have already transferred. Such commands are dropped.&#x20;

```csharp
// There Unity Events in CoherenceSync help us understand 
// what happened with authority requests and act accordingly.

// called when the CoherenceSync entity becomes the simulation authority
public UnityEvent OnStateAuthority;

// called when the CoherenceSync entity loses simulatino authority
public UnityEvent OnStateRemote;

// called when a request to assume authority over the CoherenceSync entity
// is rejected
public UnityEvent OnAuthorityRequestRejected;
```

These events are also exposed in the *Custom Events* section of the **CoherenceSync** inspector.
