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.

Your scripts need to inherit from the NetworkBehaviour class, which is part of the com.onlineobject.objectnet namespace. Read Network Behaviour for more information on this topic.


Server and Clients:

You can execute any method on all players (Server - Host and Clients) using NetworkExecute(MethodName)

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

// 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 NetworkExecuteOnServer(MethodName) this will ensure only the (server-host) will trigger the method.

// 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 NetworkExecuteOnClient(MethodName). this can only be called by the server-host.

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

Using Parameters:

ObjectNet also allow to execute methods with parameters if needed.

// 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);
Supported Parameters types

NetworkMethods support only theses types of data.

int   uint   long   ulong   short   ushort   float   double 
-------------------------------------------------------------
byte   sbyte   byte[]   bool   string   char   char[]   enum
-------------------------------------------------------------
Vector2   Vector3   Vector4   Quaternion   Color   Matrix4x4

If you want to send custom data types then use Events with Custom DataStream


FAQ:

How can I target only the Active ?

But what about if you want to target the Active player only, here is a quick tip to how to utilize NetworkExecute and Ownership to do that.

// 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);

Can I target methods outside the class ?

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

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 .

Can I execute annonymus methods ?

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.


Last updated