Search Unity

Objectpool used in Networking

Discussion in 'Multiplayer' started by Spy-Shifty, Aug 20, 2014.

  1. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    This subject prepares me a headache...

    My problem:
    The player can place bombs wich should be synch other the network, because of time and position and velocity (can be kicked away etc) so they have attached a networkview. All the important decisions should do the server, like damage on objects, players, etc.

    I'll use a ObjectPooler Script for pool the bombs.
    Code (CSharp):
    1. public class BombPoolerScript : MonoBehaviour {
    2.     public GameObject pooledObject;
    3.     public int pooledAmount = 20;
    4.     public bool willGrow = true;
    5.  
    6.     public List<GameObject> pooledObjects;
    7.  
    8.     void Start() {
    9.         pooledObjects = new List<GameObject>();
    10.         for (int i = 0; i < pooledAmount; i++) {
    11.             GameObject obj = Instantiate(pooledObject) as GameObject;
    12.             obj.SetActive(false);
    13.             obj.GetComponent<BombScript>().enabled = true;
    14.             pooledObjects.Add(obj);
    15.         }
    16.     }
    17.  
    18.     public GameObject GetPooledObject() {
    19.         for (int i = 0; i < pooledObjects.Count; i++) {
    20.             if (!pooledObjects[i].activeInHierarchy) {
    21.                 return pooledObjects[i];
    22.             }
    23.         }
    24.  
    25.         if (willGrow) {
    26.             GameObject obj = Instantiate(pooledObject) as GameObject;
    27.             pooledObjects.Add(obj);
    28.             obj.GetComponent<BombScript>().enabled = true;
    29.             return obj;
    30.         }
    31.  
    32.         return null;
    33.     }
    34. }
    What is the best way to do this over the network?

    my first approach was to always allocate a new networkview id to a bomb...
    Code (CSharp):
    1. void Update() {
    2.         if (!networkView.isMine) {
    3.             return;
    4.         }
    5.  
    6.         bool placeBomb = false;
    7.         #if UNITY_ANDROID
    8.             placeBomb = CustomInput.GetButtonUp("PlaceBomb");
    9.         #else
    10.             placeBomb = Input.GetButtonUp("PlaceBomb");
    11.         #endif
    12.         if (Input.GetButtonUp("PlaceBomb") && CanPlaceBomb()) {        
    13.            GameResults.placedBombs++;
    14.        
    15.             var pos = transform.position;
    16.             pos.x = Mathf.Round(pos.x);
    17.             pos.z = Mathf.Round(pos.z);
    18.             pos.y = 0;
    19.             foreach (var poolBomb in bombpooler.pooledObjects) {
    20.                 if (poolBomb.activeInHierarchy && poolBomb.transform.position == pos) {
    21.                     return;
    22.                 }
    23.             }
    24.             var viewID = Network.AllocateViewID();
    25.             networkView.RPC("PlaceBomb", RPCMode.All, pos, viewID);
    26.         }
    27.    
    28.     }
    29.  
    30.     [RPC]
    31.     void PlaceBomb(Vector3 pos, NetworkViewID viewID) {
    32.         var bombObj = bombpooler.GetPooledObject();
    33.         bombObj.networkView.viewID = viewID;
    34.         var bomb = bombObj.GetComponent<BombScript>();
    35.         bomb.time = time;
    36.         bomb.range = range;
    37.  
    38.         bombObj.transform.position = pos;
    39.         bombObj.SetActive(true);
    40.     }

    But i run into problems with synch. if I want to find the networkview of the bomb to get the reference to bombscript.

    With static ViewId I run into the following problem:
    The following state is possible with an static viewID attached to the bomb:
    1. placed bomb by the Client A explode on the Client A but not on Client B (some ms later)
    2. bomb is again available in the poolerscript on Client A but not on Client B (some ms later)
    3. Client A places the same bomb again but on Client B its still active and not exploded....
    4. the bomb on Client B dont exploded and placed to the new position.... (should not happened)


    But I need static viewID for finding the bomb... on other objects, because of the reference to the bombscript


    I hope I could convey my problem properly ;)
     
    Last edited: Aug 20, 2014
  2. Cha0os

    Cha0os

    Joined:
    Feb 22, 2014
    Posts:
    17
    Could you elaborate on that? Why would you need a static NetworkViewID to find the Script-Component of an Object?
     
  3. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    I love this moments :D

    On thinking about the answer of your question, I solved my problem...
    I don't realy need a bomb script reference.

    This happeneds if you port a singleplayer game to multiplayer :D
    So ok, finaly my solution of Objectpool script works for me ;)

    Maybe I inspirit someone else, who need a Objectpool script, that works for Multiplayer... ;)