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.
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);
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.
FAQ:
Last updated