Search Unity

Best way to have the client instantiate objects from server.

Discussion in 'Multiplayer' started by colorcraft, May 28, 2015.

  1. colorcraft

    colorcraft

    Joined:
    Jun 16, 2013
    Posts:
    41
    I'm looking to setup a system where the server adds game objects positions to an array then sends this information to the client when they connect. So the client can instantiate the objects according to their transform.position. I know a fair amount about networking so far but this however is a bit ahead of me.

    Cheers.
     
  2. Crystalline

    Crystalline

    Joined:
    Sep 11, 2013
    Posts:
    171
    Well, you could RPC the connected client including the array as an argument. Then the client would read the array it gets and spawning the objects.
     
  3. colorcraft

    colorcraft

    Joined:
    Jun 16, 2013
    Posts:
    41
    Hi Crystalline Thank's for your reply! This was my first thing to look at but I had a lot of issues with trying to send gameobjects and transforms over Network. Even when I tried to Serialize the array of gameobjects it said "UnityEngine.GameObject is not marked as Serializable".
     
  4. colorcraft

    colorcraft

    Joined:
    Jun 16, 2013
    Posts:
    41
    I managed to find a solution to this by searching around and putting bits together but for the sake of people who also may see this and wish to achieve this. This is how I did it.

    On the Server!
    Code (CSharp):
    1. void OnPlayerConnected (NetworkPlayer client)
    2. {
    3.  
    4. GameObject[] obj = GameObject.FindGameObjectsWithTag ("OBJECTSTOSEND");
    5.          
    6.             NetworkViewID[] networkViewIDs = new NetworkViewID[obj.Length];
    7.             Vector3[] positions = new Vector3[obj.Length];
    8.             Quaternion[] quaternions = new Quaternion[obj.Length];
    9.          
    10.             for (int i = 0; i< obj.Length; i++) {
    11.                 networkViewIDs [i] = obj [i].GetComponent<NetworkView>().viewID;
    12.                 positions [i] = obj [i].transform.position;
    13.                 quaternions [i] = obj [i].transform.rotation;
    14.              
    15.             }
    16.                    GetComponent<NetworkView>().RPC ("InitNewSceneObject", client, networkViewIDs, positions, quaternions);
    17.  
    18.  
    19. }
    On the Client!
    Code (CSharp):
    1. [RPC]
    2.     public void InitNewSceneObject (NetworkViewID[] networkViewIDs, Vector3[] positions, Quaternion[] quaternions)
    3.     {
    4.         for(int i = 0; i < networkViewIDs.Length; i++)
    5.         {
    6.             if(!networkViewIDs[i].isMine)
    7.             {
    8.                 GameObject OBJECTNAME = Instantiate(OBJECTPREFAB, positions[i], quaternions[i]) as GameObject;
    9.                 OBJECTNAME.GetComponent<NetworkView>().viewID = networkViewIDs[i];
    10.             }
    11.         }
    12.     }


    Also another thing I forgot the add. If you can don't run this OnPlayerConnected get the client to request the function because if noticed that there's about a 30% chance of receiving the RPC from the server when its send to you OnPlayerConnected.

    If anyone know's a solution to this like getting the client to wait or something. I would love to hear how you do it.
     
    Last edited: May 28, 2015
  5. luquitadenuevo

    luquitadenuevo

    Joined:
    Apr 1, 2015
    Posts:
    8
    Hi colorcraft,

    Messaging at game startup have proven to be kind of a pain for me. I had to implement a manual ACK system between server and client to switch scene because the channels are shut down while the loading process.
    Kinda recommend to do the same to have a consistent behaviour.

    You could request the server OnConnectedToServer and make the server reply, this is if you are sure the server is up by that time of course.

    Have fun!