'Syncronising' Gameobjects and Components

After multiple methods and hours of programming to try to come up with a solution, I still cannot see a way of doing this. Let me explain what I am trying to do:

I have a server, which when started, will generate loads of GameObjects with a script (lootableScript.cs) attached. This script defines the GameObject to be ‘lootable’ and allows the player to bring up a loot menu and take objects etc. The script contains a List (lootTable) which can be transfered to the players inventory and at the same time, will remove it from the container - that all works on a single machine.

To ‘create’ the GameObjects, a script running on the server will spawn an amount of items using :

Network.Instantiate(objectToSpawn, position, rotation, 0);

The objectToSpawn is a prefab already loaded - they are all the same object. Each of these objects has a lootableScript with an identical lootTable - each contains the same two items in the List. If I remove an item from one of the objects by looting it, it will be removed in that instance of the game - but obviously not in the other connected players.

I have tried a few ways to sync these components, here are my issues:

  • You cannot pass GameObjects or Components via an RPC - so I cannot simply send the state of each List
  • networkView seems to be the same, you can use state sync to do this, however, only the owner can send, in this case, the server is the owner, therefore the server will not know when a player has taken something from a list.

TL;DR

I want to know what the best way of having the server ‘owning’ a group of GameObjects (as the server will be instantiating them) and a simple way to not only keep the existance, position and rotation of each one in sync across the network, but also to be able to keep a script WITHIN these GameObjects in sync - or more importantly, the List<Inv_InventoryItem> in sync across the network.

REALLY LAZY VERSION.

I take gun out of box, it disappears from box on all clients. That’s MY GUN.

Thanks guys in advanced, any help will be appreciated.

As far as I understand your problem, networkView should actually be the right way to solve this.
Take a look at OnSerializeNetworkView. Maybe thats the way to do it.

I am actually about to release a Asset Store package that will allow users to keep scripts (custom or built in) in sync. That being said, it covers only basic built in C# datatypes and basic Unity datatypes. If you would like to keep objects in sync I would suggest doing some binary serialization and passing the data across RPC. This will allow you to be able to use that data to keep your objects in sync. (I am speaking of course about passing a byte[ ] through RPC and de-serializing it on the other end)

Example of byte[ ] RPC

[RPC]
private void SyncComponents(byte[] componentData)
{
    // Deserialize the componentData array to your custom objects and apply the new values
}

Or instead of using serialization (which is very useful for sending stuff over network though, but I use it mainly to send player characters etc (how each player has configured their looks)) just use RPCs for it.

Something like:

[RPC]
private void RemoveItemFromInventoryList(int itemIndex) 
{
     inventoryList.RemoveAt(itemIndex);
}