# Network Behaviour

ObjectNet provides a special class that can be extended to provide a bunch of events and methods, this class is NetworkBehaviour.

The NetworkBehaviour is a class that replaces Unity MonoBehaviour class and provides a lot of embedded methods and events.

NetworkBehavour can be used even if your game doesn't implement MultiPlayer features, this means if your game must work on the single-player mode you can keep this inheritance without affecting your single-player mode behavior.

This is pretty useful since the developer can create your game to run in Single-player mode and multi-player while keeping the same code base.

To extend your MonoBehaviour script from NetworkBehaviour it's just change inheritance.

```csharp
public class MyGameScript : NetworkBehaviour {
                   // Your class code
        }
```

Once inherited, you can implement some special method to handle your multiplayer game, the following script will illustrate the main methods:<br>

{% hint style="info" %}
To use `NetworkBehaviour` functionality in multiplayer, the object must be **registered as a `NetworkObject`** in the **NetworkManager's Prefab Database**.
{% endhint %}

<details>

<summary>OnNetworkStarted</summary>

This event is called once when Network is available to this object.

```csharp
       /// <summary>
       /// This event is called when Network features starts
       ///
       /// Note : This event is controlled internally by framework and ensure that
       ///        network is up and running
       /// </summary>
       public override void OnNetworkStarted() { 
               // Here you can do any that you need after ensure that Network is initialized
       }
```

</details>

<details>

<summary>Awake</summary>

<mark style="color:purple;">Active Awake:</mark>

This event is the Awake execute only when object is running on Active Mode.

```csharp
       /// <summary>
       /// Active Awake is executed when objects on Active mode Awake
       /// </summary>
       public void ActiveAwake() {
       }
```

<mark style="color:purple;">Passive Awake:</mark>

This event is the Awake execute only when object is running on Passive Mode.

<pre class="language-csharp"><code class="lang-csharp"><strong>      /// &#x3C;summary>
</strong>      /// Passive Awake is executed when objects on Passive mode Awake
      /// &#x3C;/summary>
      public void PassiveAwake() {
      }
</code></pre>

</details>

<details>

<summary>Start</summary>

<mark style="color:purple;">Active Start:</mark>

This event is the Start execute only when object is running on Active Mode.

```csharp
       /// <summary>
       /// Active Start is executed when objects on Active mode Start
       /// </summary>
       public void ActiveStart() {
       }
```

<mark style="color:purple;">Passive Start:</mark>

This event is the Start execute only when object is running on Passive Mode.

```csharp
       /// <summary>
       /// Passive Start is executed when objects on Passive mode Start
       /// </summary>
       public void PassiveStart() {
       }
```

</details>

<details>

<summary>OnEnable</summary>

<mark style="color:purple;">Active OnEnable:</mark>

This event is the OnEnable execute only when object is running on Active Mode.

```csharp
       /// <summary>
       /// Active OnEnable is executed when objects on Active mode 
       /// </summary>
       public void ActiveOnEnable() {
       }
```

<mark style="color:purple;">Passive OnEnable:</mark>

This event is the OnEnable execute only when object is running on PassiveMode.

```csharp
       /// <summary>
       /// Active OnEnable is executed when objects on Passive mode 
       /// </summary>
       public void PassiveOnEnable() {
       }
```

</details>

<details>

<summary>OnDisable</summary>

<mark style="color:purple;">Active OnDisable:</mark>

This event is the OnDisable execute only when object is running on ActiveMode.

```csharp
       /// <summary>
       /// Active OnDisable is executed when objects on Active mode
       /// </summary>
       public void ActiveOnDisable() {
       }
```

<mark style="color:purple;">Passive OnDisable:</mark>

This event is the OnDisable execute only when object is running on PassiveMode.

```csharp
       /// <summary>
       /// Passive OnDisable is executed when objects on Passive mode
       /// </summary>
       public void PassiveOnDisable() {
       }
```

</details>

<details>

<summary>Update</summary>

<mark style="color:purple;">Active Update:</mark>

This event is the Update execute only when object is running on Active Mode.

```csharp
       /// <summary>
       /// Active Update is executed every frame for Active object
       /// </summary>
       public void ActiveUpdate() {
       }
```

<mark style="color:purple;">Passive Update:</mark>

This event is the Update execute only when object is running on Passive Mode.

```csharp
       /// <summary>
       /// Passive Update is executed every frame for Passive object
       /// </summary>
       public void PassiveUpdate() {
       }
```

</details>

<details>

<summary>FixedUpdate</summary>

<mark style="color:purple;">Active FixedUpdate:</mark>

This event is the FixedUpdate execute only when object is running on Active Mode.

```csharp
       /// <summary>
       /// Active FixedUpdate is executed every physics frame for Active object
       /// </summary>
       public void ActiveFixedUpdate() {
       }
```

<mark style="color:purple;">Passive FixedUpdate:</mark>

This event is the FixedUpdate execute only when object is running on Passive Mode.

```csharp
       /// <summary>
       /// Passive FixedUpdate is executed every physics frame for Passive object
       /// </summary>
       public void PassiveFixedUpdate() {
       }
```

</details>

<details>

<summary>LateUpdate</summary>

<mark style="color:purple;">Active LateUpdate:</mark>

This event is the LateUpdate execute only when object is running on Active Mode.

```csharp
   /// <summary>
   /// Active LateUpdate is executed every frame after Update for Active object
   /// </summary>
   public void ActiveLateUpdate() {
   }
```

<mark style="color:purple;">Passive LateUpdate:</mark>

This event is the LateUpdate execute only when object is running on Passive Mode.

```csharp
       /// <summary>
       /// Passive LateUpdate is executed every frame after Update for Passive object
       /// </summary>
       public void PassiveLateUpdate() {
       }
```

</details>

### Example:

Here is an example on how to use them

```csharp
public class MyGameScript : NetworkBehaviour {
                   
       public override void OnNetworkStarted() { 
              Debug.Log("Network is Initialized");
       }

       public void ActiveStart() {
              Debug.Log("This is an Active Object");
       }
       
       public void PassiveStart() {
              Debug.Log("This is a Passive Object");
       }

}
```
