# 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.
* **Not transferable**. Authority cannot be transferred.

{% hint style="info" %}
Using the **Not transferable** option will also disable giving the authority to other Clients, even if you own the entity.

If you want to instantiate entities on the host or Simulator and then transfer the authority to a Client, it is recommended to use the **Request** option, with **Approve by Default** set to false.
{% endhint %}

<figure><img src="https://2400155846-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNGFZGdbLA4bzHQXTuDMT%2Fuploads%2Fd82DwlTmn7gWh19IQJ8V%2Fimage.png?alt=media&#x26;token=39684fe7-f580-46a7-8774-460bdc55a455" alt=""><figcaption></figcaption></figure>

When using the Request mode, you can use `CoherenceSync.OnAuthorityRequest` to decide which transfers should go through.

```csharp
using UnityEngine;
using Coherence.Toolkit;
using Coherence;

public class AuthorityTransferRequestApproval : MonoBehaviour
{
    public CoherenceSync sync;

    private void Awake()
    {
        sync.OnAuthorityRequest.AddListener(OnAuthorityRequest);
    }

    private void OnAuthorityRequest(AuthorityRequest request, CoherenceSync sync)
    {
        // In this example, we want to only allow input authority to be transferred.
        request.Respond(request.AuthorityType == AuthorityType.Input);
    }
}
```

### 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 an entity is abandoned by its owner, the Replication Server will assign it to a Client again, as soon as possible. For instance, this can be useful 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 straightforward.

<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.7/manual/networking-state-changes/commands) for entities that they have already transferred. Such commands are dropped.&#x20;

These events are public and available on the **CoherenceSync** inspector.

<figure><img src="https://2400155846-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNGFZGdbLA4bzHQXTuDMT%2Fuploads%2FQb4MFIuSZhyGheTMwStq%2Fimage.png?alt=media&#x26;token=421bb525-4d48-46e9-8610-b91707147f5b" alt=""><figcaption></figcaption></figure>
