# Authority transfer

### Overview

The state of network entities that are currently not simulated locally (either because they are being simulated on another game client or on a simulator) cannot be affected directly.

Network [commands ](https://docs.coherence.io/0.7.4/authority/commands)can help us affect state indirectly, but for anything more involved, an authority transfer might be necessary.

### 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 receiving party (i.e. when *Approve requests* is false).
* **Steal.** Authority will always be given to the requesting party on a FCFS ("first come first serve") basis.
* **Not transferable**. Authority cannot be transferred.

Note also that you need to set up Auto-adopt Orphan if you wan't orphans to be adopted  automatically by nearest player.&#x20;

<div align="left"><img src="https://2821114902-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fv0ksjqOy2jWVMwsg4f9k%2Fuploads%2FReFtCaPxpstNADm5uWJ1%2FScreenshot%202021-12-23%20at%2015.52.02.png?alt=media&#x26;token=36e82a0c-65b7-4378-9da0-8aef47e6ec09" alt=""></div>

If you uncheck the Approve Requests, you can use the unity event `OnAuthorityRequest` to respond to the request.

![](https://2821114902-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MWd0ZPEK7vE9nkE0b7G%2F-MexxNdGjyZdHQ_27ynC%2F-MexxwMZMnMTQN5KJEmt%2Fimage.png?alt=media\&token=e596061b-b4ab-4010-8bad-6d8c6cb79e3a)

The request can be approved or rejected in the callback.&#x20;

```
public void OnRequest(ushort client)
{
    var coherenceSync = target.GetComponent<CoherenceSync>();
    // do some extra checks
    // transfer authority
    coherenceSync.TransferAuthority(client);
    
    // or reject authority request
    // coherenceSync.RejectAuthorityRequest(client);
}
```

### Requesting Authority in Code

Requesting authority is very straight-forward.

```csharp
var coherenceSync = target.GetComponent<CoherenceSync>();
coherenceSync.RequestAuthority();
```

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

The request will first go to the replication server and be passed onto the receiving simulator or game client, so it may take a few frames to get a response.

```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 OnAuthorityGained;

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

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

These events are also exposed to the Unity inspector in the *Events on authority transfer* section of the CoherenceSync behaviour.

![Authority events in the CoherenceSync inspector](https://2821114902-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MWd0ZPEK7vE9nkE0b7G%2F-Me1G8yGB5v0-hB25aG6%2F-Me1I-ooYkkX8Tj9brQn%2FScreen%20Shot%202021-07-07%20at%2011.44.43%20AM.png?alt=media\&token=2c07f2fe-f861-4963-9fa1-06fb9848603d)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.coherence.io/0.7.4/authority/authority-transfer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
