Adding Network View in Code

My game features many destructible objects, which makes it (I assume) unfeasible to add a Network View to all of them in the editor. I would like to add a Network View at runtime only when needed, but I’m stuck.

If I add a Network View through code, I can’t add it for all players, because…that would require a Network View in the first place. See what I mean?

Here’s what I’m trying to achieve:

  1. Player X smashes an object.
  2. Object immediately has Network View added. All following info is communicated to other players.
  3. Object is moved and its health stat decreases.
  4. When health reaches zero, object is destroyed and replaced with destruction prefab.

Can anyone help with this?

I tried simply adding a Network View to every object. Given that I’m using RPCs and therefore have State Synchronization set to Off and Observed set to None, it seems to run okay.

I think. I’m only testing with one client.

Does anyone know how expensive a Network View is? I have…I think around 40,000. Yes.

networkviews, if the state synchro is off, won’t actually do anything. Only if that is on will they send over the network.

However, because these objects are built in to your scene, when the server starts, all the viewid’s need to synchro. That’s a lot of networkviewid’s, and the viewid pool will probably fill and start reusing numbers. Kind of surprised it hasn’t warned you about this.

Your original plan to add networkviews can be done, however. Just add a networkview component to the object, and then sync the viewid between everyone, using this:

Don’t copy that exactly though, as that deals with a prefab.

Some psuedocode:

<this part is in a manager of all the objects, called when the player hits a block>
var objPlayerHit = passedObjectThePlayerHitParameter
var view = objPlayerHit.AddComponent(NetworkView)
var vid = Network.AllocateId()
view.ViewId = vid
<RPC call to clients, with vid as a parameter, and some other parameter like an int for finding the correct block to assign to>

var obj = findObj(passedObjectIdPameter) var view = obj.AddComponent(NetworkView) view.ViewId = passedvidParameter

… and now you can do rpcs on the blocks themselves. I would recommend waiting a little bit before you do to make sure all the clients have found their objects and done the instantiation.