Adding network functionallity to GameObjects

To run code synchronised on every connected game client you need to have a NetworkController present in your scene that is successfully connected to a game server. If you don't have setup all of this you should start here.

Configuring the Network game object connector

AwesomeNET does not synchronize everything that happens in the scene as this is usually not needed and would result in very high bandwith requirements. To run some game objects function simmultaneously on multiple game clients you need to add the NetworkGameObjectConnector (found in Scripts/Networking) to the game object.

Screenshot of the Network GameObjec Connector

The script automatically lists all the scripts and their public methods attached to the game object. Just search for the function you want to run over the network and click "Add"!

If you cant find the methods you want to run, make sure your script is attached to the same game object as the NetworkGameObjectConnector and the method is public.

Now your function is registered and ready to be executed via the network.

Screenshot of the Network GameObjec Connector with two registered methods

Running a function synchronised on every connected client

Every registered method can be called simmultaneously on every connected client as simple as calling its NetworkGameObjectConnector like this:

NetworkGameObjectConnector.ExecuteNetworkCommand("Script.Method", new object[] {arrayOfArguments});

This searches for objects with the same object id on every client and executes their method that is registered with the same action id.

Please note that even if your method doesn't accept any arguments you still have to pass an empty object array like this:

NetworkGameObjectConnector.ExecuteNetworkCommand("Script.Method", new object[] {})


Let's assume you have a cube with an attached cube controller. It contains a function called LaunchCube that launches the cube in the air:

public void LaunchCube(Vector3 force, Vector3 torque) {
    // Assign some force and touque to the cube
    GetComponent<Rigidbody>().AddForce(force * 10f);
    GetComponent<Rigidbody>().AddTorque(torque * 10f);

Local call

To execute this locally the moment the player clicks you would just run:

if (Input.GetMouseButtonDown(1)) {
    Vector3 f = new Vector3(10f, 10f, 10f);
    Vector3 t = new Vector3(5f, 5f, 5f);

    LaunchCube(f, t);

Networked call

If you want to run this synchronised on every client just add a NetworkGameObjectConnector to your game object and execute the method via the NetworkGameObjectConnector and the name shown in the inspector: Registered method in the inspector

if (Input.GetMouseButtonDown(1)) {
    Vector3 f = new Vector3(10f, 10f, 10f);
    Vector3 t = new Vector3(5f, 5f, 5f);

    // Getting the NetworkGameObject connector attached to the game object
    ngoc = GetComponent<NetworkGameObjectConnector>();

    // Running the method on every connected client
    ngoc.ExecuteNetworkCommand("CubeController.LaunchCube", new object[] {f, t});

Please make sure that you pass all the needed arguments via the object array.

That's all. Now the game server takes care of sending a message to all clients including the local game client that tells them to execute CubeController.LaunchCube for this specific cube with this specific id.

You can also run functions directly by their object and action id without calling their specific NetworkGameObjectConnector. To see how this works have a look at the Network Controller Documentation.

Real world example

To see a commented example implementation of the above mentioned features take a look at the CubeController contained in the Asset Store packgage.