# Network Methods

Network Method  allows to execute methods over the network, this can be a powerful mechanism to keep you game synchronized and handle with aspects of your multiplayer game.

{% hint style="info" %}
Your scripts need to inherit from the <mark style="color:orange;">**`NetworkBehaviour`**</mark> class, which is part of the <mark style="color:purple;">**`com.onlineobject.objectnet`**</mark> namespace. Read [](https://onlineobject.gitbook.io/objectnet/general/network-behaviour "mention") for more information on this topic.
{% endhint %}

***

## Server and Clients:

You can execute any method on all players (Server - Host and Clients) using <mark style="color:yellow;">NetworkExecute(MethodName)</mark>&#x20;

anywhere in the same class. in the example bellow all players console will display Hello World! log.

```csharp
// You class must have a method        
void MethodName() {
  Debug.Log("Hello World!");
}
// And in any part of you code on the same class you can have
this.NetworkExecute(this.MethodName);
```

***

## Server Only:

But in some cases you might need to execute a method solely on the server using <mark style="color:yellow;">NetworkExecuteOnServer(MethodName)</mark> this will ensure only the (server-host) will trigger the method.

```csharp
// Execute method on server only
this.NetworkExecuteOnServer(this.MethodName);
```

***

## Clients Only:

In other cases the server may need to execute a method on all clients execluding it self using <mark style="color:yellow;">NetworkExecuteOnClient(MethodName)</mark>. this can only be called by the server-host.

```csharp
// Execute method on all clients
this.NetworkExecuteOnClient(this.MethodName);
```

***

## <mark style="color:blue;">Using Parameters:</mark>

ObjectNet also allow to execute methods with parameters if needed.&#x20;

```csharp
// You class must have a method        
void MethodName(string name, int age) {
   // Do something
}
// And in any part of you code at same class you can have
this.NetworkExecute<int, string>(this.MethodName, "MyName", 20);
```

{% hint style="success" %}
*You can execute methods with up to 10 arguments. if you needed more than that you will have to use DataStream to send it mannually.*
{% endhint %}

<details>

<summary>Supported Parameters types</summary>

NetworkMethods support only theses types of data.&#x20;

<pre class="language-csharp"><code class="lang-csharp"><strong>int   uint   long   ulong   short   ushort   float   double 
</strong>-------------------------------------------------------------
byte   sbyte   byte[]   bool   string   char   char[]   enum
-------------------------------------------------------------
Vector2   Vector3   Vector4   Quaternion   Color   Matrix4x4
</code></pre>

If you want to send custom data types then use [Events ](https://onlineobject.gitbook.io/objectnet/general/network-behaviour/sending-events)with [Custom DataStream](https://onlineobject.gitbook.io/objectnet/advance/custom-datastream)

</details>

***

## FAQ:&#x20;

<details>

<summary>How can I target only the Active ?</summary>

But what about if you want to target the Active player only, here is a quick tip to how to utilize NetworkExecute and [ownership](https://onlineobject.gitbook.io/objectnet/general/network-behaviour/ownership "mention") to do that.

```csharp
// You class must have a method        
void MethodName() {
  if (isActive()) { //ensures that only the active object execute the method  
      Debug.Log("Am the Owner of this object");
  }
}

// And in any part of you code on the same class you can have
this.NetworkExecute(this.MethodName);
```

</details>

<details>

<summary>Can I target methods outside the class ?</summary>

NetworkExecute only works on Methods inside the class but here is a work around for that:

```csharp
using UnityEngine;
using com.onlineobject.objectnet;

public class Player: NetworkBehaviour
{
    //Refrence to the Test Class
    private Test test;
                 
    //Or in any part of you code
    private void Start(){
        NetworkExecute(TestMessage);
    }   
    //Trigger the method on the Test class
    private void TestMessage(){
        test.Hello();
    }
 
}
---------------------------------------------------------------

public class Test: MonoBehaviour
{
    public void Hello(){
        Debug.Log("Hello World");
    }
}
```

As you can observe from the example above, you can Only NetworkExecute methods on the same Class, exectuing it directly like this ***NetworkExecute(test.Hello);*** will not work because the method belongs to another class .

</details>

<details>

<summary>Can I execute annonymus methods ?</summary>

Executing Annonymus methods will not work  for example ***NetworkExecute(() ⇒  Debug.Log(""));*** wont work because NetworkExecute needs a  method name to sync across the network and annonymus methods dont provide that.

</details>

***
