3

I have a somewhat complex tree of objects that is generated and configured at runtime. Since the information required to do this is available to both the server and the client, I would like to have both sides generate the objects independently and then link the networked parts up for syncing afterwards.

That is to say, I need a way to make SyncVar work for an object that exists on the server and client but was not originally spawned via NetworkServer.Spawn. Is there a way to manually configure NetworkIdentity such that the Unity networking system understands that something is the same object?

I have the ability to uniquely identify these objects across the network myself, I just need a way to communicate that to Unity. NetworkIdentity.netId is readonly, so that's not an option.

user3071284
  • 6,955
  • 6
  • 43
  • 57
Mispy
  • 879
  • 8
  • 21

1 Answers1

0

If you make all the initialisation done purely by the server and then pushed to the clients you remove the need to sync afterwards. This also would remove the need to deal with duplicate information (which would ultimately be wasted CPU time at client end).

However, if you are wanting to have clients create the data as well, then I would suggest you have them send appropriate messages to the server with their data, the server can then create the objects for them.

Setup message handlers with NetworkServer.RegisterHandler on the server instance for each type of message you need it to handle,

public enum netMessages{
    hello = 101, 
    goodbye = 102, 
    action = 103,
}    

...

NetworkServer.RegisterHandler((short)netMessages.hello, new NetworkMessageDelegate(hdl_hello));
NetworkServer.RegisterHandler((short)netMessages.goodbye, new NetworkMessageDelegate(hdl_goodbye));

...

private void hdl_hello (NetworkMessage msg){
  nmgs_hello m = msg.ReadMessage<nmgs_hello>();
  ...
}

and use the Send method of NetworkClient to send messages to the server.

You will also need to define message classes based on MessageBase for the actual messages.

public class nmsg_hello : MessageBase {
  public int x;
  public float welcomeness;
}

NOTE: Make sure you don't base any of your network messages off each other, seems to be bug/feature in Unity (at least the last time I tried it) where it doesn't work if your message is derived from anything other than MessageBase as it's immediate ancestor.

Graeme
  • 1,643
  • 15
  • 27