Search Unity

Instantiation with multiple network views

Discussion in 'Multiplayer' started by Orion, Oct 7, 2008.

  1. Orion

    Orion

    Joined:
    Mar 31, 2008
    Posts:
    261
    Alright, here's my problem:

    My players' prefabs are composed of multiple objects, and some of them have one or more network views on them. The hierarchy and amount of those will VERY likely change during development.

    Now the problem is instantiating all this.
    I take it, if I use Network.Instantiate, it will take care of all the viewIDs in the hierarchy. But it will also fill up the RPC buffer with it. As I use a full authority server approach, the server is the only one who may create new objects - so I can not use DestroyPlayerObjects (playerID : NetworkPlayer) or RemoveRPCs (playerID : NetworkPlayer, group : int).

    So once a player object is destroyed, how would I take care of the buffer? Or otherwise, how would I instantiate this madness "dynamically" (irrelevant of the exact structure of the prefab)?

    Any ideas?
     
  2. Orion

    Orion

    Joined:
    Mar 31, 2008
    Posts:
    261
    Okay, I went the approach to serialize my objects with the XMLserializer and send them as a string.

    Now I just had to realize, that viewIDs do not get serialized to xml! They can't be turned to integers either and even if, they can't be converted back from integer to viewID because there is no constructor for viewIDs (besides it being not possible to set their ID manually).

    How do you cope with an unspecified amount of viewIDs on an object that you need to instantiate across the network? Why can RPCs serialize viewIDs, but the XML Serializer can't?
     
  3. ProtonOne

    ProtonOne

    Joined:
    Mar 8, 2008
    Posts:
    406
    In the networking demo, check out
    Code (csharp):
    1. Authoritative Server/AuthServerSpawnPlayer.js
    You will not be able to use Network.Instantiate since that is always buffered. But the method in that file is not so bad.

    I think that instead of
    Code (csharp):
    1. var networkViews = instantiatedPlayer.GetComponents(NetworkView);
    You could use
    Code (csharp):
    1. var networkViews = instantiatedPlayer.GetComponentsInChildren(NetworkView);
    to handle the many NetworkViews that you may have in child transforms.
     
  4. Orion

    Orion

    Joined:
    Mar 31, 2008
    Posts:
    261
    Thanks! I've looked at the demo, but it doesn't quite fix my problem. I'm already using GetComponentsInChildren() - the problem is that even an RPC can't send "unlimited" viewIDs. While you can send an array of viewIDs and also receive that array, for whatever reason, the viewIDs get completely scrambled (e.g. you get numbers like -751938 or plain 0). I haven't yet tried if arrays of other data make it through unharmed.
    I was thinking about sending multiple RPCs, but that is quite tricky, because then I can't "instantly" spawn the object but have to wait until all the RPCs are there and compose the object from that.

    What I'm doing now is send one RPC, with the object's data as an xml string, and a hardcoded big number of viewIDs, that are used or not (the client knows from the prefab that is used, how many viewIDs are needed and only reads those).
    The downside is that it's quite ugly code:
    Code (csharp):
    1. function Spawn(xml: String, ID0: NetworkViewID, ID1: NetworkViewID, ID2: NetworkViewID, ID3: NetworkViewID, ...)
    and that a lot of unused data is being sent (empty viewIDs are still viewIDs).

    I noticed with "new NetworkViewID()" I can get a dummy viewID, but it's not possible to fill it's data :roll: