# Common Functions

Below are some commonly used methods available within the `NetworkBehaviour` class that can assist with implementing multiplayer logic.

<details>

<summary>GetNetworkId()</summary>

Each NetworkObject has its unique id thats how ObjectNet diffrentiate between diffrent objects on the scene.

```csharp
int id = this.GetNetworkId();
```

</details>

<details>

<summary>HasNetworkElement()</summary>

Check wheather the NetworkObject is initialized and NetworkElement was created

```csharp
if(this.HasNetworkElement()){
}
```

</details>

<details>

<summary>GetNetworkElement()</summary>

Returns the `INetworkElement` associated with the current `NetworkObject`.\
This provides access to a broader set of functions that allow you to control and customize the behavior of the networked object.

```csharp
this.GetNetworkElement()
```

</details>

<details>

<summary>SetActiveRate()</summary>

After retrieving the `INetworkElement`, you can use it to **dynamically adjust the send rate** of the associated `NetworkObject` by calling:

```csharp
//change active send rate of this network object
GetNetworkElement().SetActiveRate(int rate)
//or change minmum send rate when no changes are detected only for that object
GetNetworkElement().SetMinimunRateValue(int minimumRateValue)
```

</details>

<details>

<summary>Pause and Resume Behaviours</summary>

You can pause and resume Behaviours like Position sync, Animations sync, Variables sync. through code and resume them when ever you want.

```csharp
 //Pause
this.GetNetworkElement().GetBehavior<PositionNetwork>().Pause();
this.GetNetworkElement().GetBehavior<RotationNetwork>().Pause();
this.GetNetworkElement().GetBehavior<ScaleNetwork>().Pause();
this.GetNetworkElement().GetBehavior<AnimationNetwork>().Pause();
this.GetNetworkElement().GetBehavior<VariablesNetwork>().Pause();
```

```csharp
 //Resume
this.GetNetworkElement().GetBehavior<PositionNetwork>().Resume();
this.GetNetworkElement().GetBehavior<RotationNetwork>().Resume();
this.GetNetworkElement().GetBehavior<ScaleNetwork>().Resume();
this.GetNetworkElement().GetBehavior<AnimationNetwork>().Resume();
this.GetNetworkElement().GetBehavior<VariablesNetwork>().Resume();
```

</details>

<details>

<summary>Teleport()</summary>

You can teleport the Networkobject without interpolating it on the passive.

```csharp
this.Teleport(targetPosition);
```

</details>

<details>

<summary>IsActive() and IsPassive()</summary>

Determines whether the current NetworkObject is **Active** or **Passive** (a remote instance).\
For a detailed explanation of Active vs Passive objects, see [ownership](https://onlineobject.gitbook.io/objectnet/general/network-behaviour/ownership "mention").

```csharp
if (IsActive()) { 
    // This object is the data owner 
}

if (IsPassive()) { 
    // This object is a remote replica 
}
```

</details>

<details>

<summary>IsRuningLogic()</summary>

This checks if your current device is the <mark style="color:yellow;">**Host**</mark> — the one responsible for running all the **authoritative game logic**.

> Think of this as: *"Am I in charge of running the game world?"*

```csharp
this.IsRunningLogic()
```

</details>

<details>

<summary>IsOwner()</summary>

This is useful when useing [remote-controls](https://onlineobject.gitbook.io/objectnet/general/network-components/network-manager/remote-controls "mention") as all objects will be passive on clients even player object, and the only way for clients to know if they own the player will be by using

```csharp
if(this.IsOwner()){
}
```

</details>

<details>

<summary>IsPlayer()</summary>

Determines whether the object is a Player GameObject.

```csharp
if (this.IsPlayer()){
}
```

</details>

<details>

<summary>TakeControl()</summary>

Takes control over the passive object turining it to Active. Check [#dynamic-ownership-transfer](https://onlineobject.gitbook.io/objectnet/general/ownership#dynamic-ownership-transfer "mention") for more details.

```csharp
//Call on Passive
TakeControl()
```

{% hint style="info" %}
Note: TakeControl() will automatically configure the object for **Active mode**, enabling relevant scripts and setting the Rigidbody accordingly.\
The previously active instance will also be updated to reflect **Passive mode**, with its components adjusted as needed.
{% endhint %}

</details>

<details>

<summary>SetBehaviourMode()</summary>

The `TakeControl()` method is used to switch a `NetworkObject` from **Passive** to **Active** mode.\
However, this process is **not instant** — it requires approval from the server before the mode change is applied.

If you need to **immediately switch a local object to Active mode** (e.g., for instant responsiveness), you can do so by calling `SetBehaviorMode(BehaviorMode.Active)` directly  after calling **TakeControl()**. This updates the local state immediately while still allowing `TakeControl()` to handle proper server-side ownership and passive transition of the previously active object.

Here’s how to combine both calls for smooth control handoff:

```csharp
// Notify the server to handle ownership changes
this.TakeControl();

// Instantly switch the local object to Active mode without delay
this.SetBehaviorMode(BehaviorMode.Active, false);
//make sure to set the second parameter detectOnStart to false 
//just as shown in the example above or else it will trigger OnNetworkStart().
```

{% hint style="danger" %}
Use this approach carefully to avoid conflicts with server authority or synchronization logic.
{% endhint %}

</details>

These are specific Methods in the NetworkBehaviour class, for a more global methods check Network Manager [common-functions](https://onlineobject.gitbook.io/objectnet/general/networkmanager-functions/common-functions "mention")

For a complete list, refer to the  [API Documentation](https://onlineobject.net/objectnet/docs/api/html/N-com.onlineobject.objectnet.htm).
