Search Unity

Third Party (PUN) How to sync the object hierarchy ?

Discussion in 'Multiplayer' started by Rainbooow, Feb 7, 2016.

  1. Rainbooow

    Rainbooow

    Joined:
    Jan 26, 2016
    Posts:
    8
    Hello,

    I am quite new to Unity, and to networking in general, and I am currently facing an issue, with the Photon framework.

    I have a solo game (a RTS), that I would like to extend to multiplayer (without P2P lockstep for a start). In my current code base, I am heavily relying on the objet hierarchy, to know the units of a player, his buildings, etc. However, when I use "PhotonNetwork.Instantiate" to create a new object, then assign the parent, this hierarchy is not passed to the other players (the objet is created in the Root).

    To solve this, I am planning to set the object name to its instance ID, and in "OnPhotonSerializeView", pass the parent ID so that I can recreate the hierarchy manually by using the "GameObject.Find" method. However, this feels a bit hacky to me.

    So my questions are :
    - Is it a good idea to rely heavily on the objects hierarchy in my code, or should I just have a parent variable and a list of children for each of my objects, and not care if all my objetcs are in the root?
    - Is there a clean way to pass the object hierarchy to other players? I do not like the fact that I have to set the name of my object to its ID to be able to retrieve it later on...

    Thanks in advance !
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    There is no built-in way to pass hierarchy of objects through PUN, sorry.
    You could send some data along with the instantiation (there is a Instantiate method with an extra parameter at the end) and use that to adjust the hierarchy. In OnPhotonInstantiate (see: PunBehaviour) you could adjust the hierarchy.
    Or, if your hierarchy changes over time, you could also try to reproduce it via RPCs, when it changes.

    It depends on how often you are going to change hierarchy.
     
  3. Rainbooow

    Rainbooow

    Joined:
    Jan 26, 2016
    Posts:
    8
    Ok thank you for the answer, I will try to test it this evening with the OnPhotonInstantiate method :). I think it should do the trick, as my hierarchy does not change overtime (at least for now).

    While I am on it, do you have any idea on how much objects can I handle through PUN, without having to implement a lockstep architecture? For each object, I will have to roughly pass the position, rotation, state of animation and hitpoints.
     
  4. Rainbooow

    Rainbooow

    Joined:
    Jan 26, 2016
    Posts:
    8
    I was able to pass the parent with the OnPhotonInstantiate, thanks ;).

    For those with the same issue, I post the code here :

    Sending the data :
    Code (CSharp):
    1. void OnJoinedRoom() {
    2. ...
    3.    object[] data = new object[1];
    4.    data[0] = units.name;  // units is my parent object
    5.    GameObject builder1 = PhotonNetwork.Instantiate("Builder", playerPosition, Quaternion.identity, 0, data);
    6. ...}
    Retrieving the data (in the script attached to the PhotonView):
    Code (CSharp):
    1. void OnPhotonInstantiate(PhotonMessageInfo info) {
    2.         object[] data = this.gameObject.GetPhotonView().instantiationData;
    3.         if (data != null && data.Length == 1) {
    4.             this.parentName = (string)data[0];
    5.         }
    And then in the update method of the same script :
    Code (CSharp):
    1. if (parentName != "" && transform.parent == null ) {
    2.        if (GameObject.Find(parentName)) transform.parent = GameObject.Find(parentName).transform;
    3. }
    Btw, after having retrieved the parentName, when trying to assign the parent in the OnPhotonInstantiate, or in the Start method, the "GameObject.Find(parentName)" was not finding anything (but in Update, it works..).
    Is it linked to the fact that the objects created in the OnJoinRoom are not necessarily instancied in the same order on the other players side?
     
    AM-Dev and tobiass like this.
  5. conceptfac

    conceptfac

    Joined:
    Feb 15, 2018
    Posts:
    23
    void OnPhotonInstantiate ( PhotonMessageInfo info ) is not calling!
     
  6. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    Do you implement the IPunInstantiateMagicCallback on the script that has OnPhotonInstantiate? Without that, the callback won't be called.
     
    Mashimaro7 likes this.