Search Unity

Having a hard time with InstantiateSceneObject

Discussion in 'Connected Games' started by Avalin, Feb 7, 2019.

  1. Avalin

    Avalin

    Joined:
    Oct 12, 2018
    Posts:
    37
    Forgot to mention in the title that this is a question related to Photon PUN

    So what I want is this.
    A player disconnects the game, so OnPlayerLeave() I instantiate a scene object with a lot of the same variables/properties on it, that the leaving game object had... Basically its a copy-prefab with despawn animations.
    I added this to the script of my PlayerDespawn prefab:


    Code (CSharp):
    1.  
    2.     void IPunInstantiateMagicCallback.OnPhotonInstantiate(PhotonMessageInfo info)
    3.     {
    4.         FindObjectOfType<SceneObjectContainer>().DespawnContainer.Add(this.gameObject);
    5.         SceneObjectValue = FindObjectOfType<SceneObjectContainer>().DespawnContainer.Count;
    6.     }
    7.  
    And I have this in my OnPlayerLeftRoom
    Code (CSharp):
    1.     public override void OnPlayerLeftRoom(Player player)
    2.     {
    3.         base.OnPlayerLeftRoom(player);
    4.  
    5.         var playerTagObj = (GameObject)player.TagObject;
    6.         if (PhotonNetwork.IsMasterClient)
    7.         {
    8.             GameObject playerDespawn = PhotonNetwork.InstantiateSceneObject(Path.Combine("Prefabs/Player", "PlayerDespawn"), playerTagObj.transform.position, Quaternion.identity);
    9.             photonView.RPC("RPC_SetPlayerDespawnProperties", RpcTarget.All, player);
    10.         }
    11.         var sceneHolder = FindObjectOfType<SceneObjectContainer>();
    12.         var playerDespawnRef = sceneHolder.DespawnContainer[sceneHolder.DespawnContainer.Count-1];
    13.  
    14.         photonPlayers = PhotonNetwork.PlayerList;
    15.         playersInRoom--;
    16.  
    17.         if (MultiplayerSettings.multiplayerSettings.maxPlayers == playersInRoom)
    18.         {
    19.             if (!PhotonNetwork.IsMasterClient) return;
    20.             PhotonNetwork.CurrentRoom.IsOpen = true;
    21.         }
    22.     }

    The non-masterclients never receives the properties I set through the RPC, even though the object does get instantiated to the scene. What gives? Afaik, custom properties can't be given to Scene Objects, am I wrong?
     
    Last edited: Feb 8, 2019
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,197
    I'm not at my PC so I can't check it will work (however,I think it should), but have you tried using PhotonInstantiationData to pass the player data to the remote clients rather than using an RPC?
     
    Avalin likes this.
  3. Avalin

    Avalin

    Joined:
    Oct 12, 2018
    Posts:
    37
    Thank you, I didnt realise you could send InstantiationData as a sort of equivalent to custom properties, this worked. Important to note is that retrieving this data had to be done in Awake() as opposed to Start() otherwise it wouldn't work
     
    Munchy2007 likes this.
  4. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,197
    I seldom if ever use Start or Awake for networked objects, in this case I would usually retrieve and use the InstantiationData in the OnPhotonInstantiate callback.
     
    Last edited: Feb 10, 2019
    Avalin likes this.
  5. Avalin

    Avalin

    Joined:
    Oct 12, 2018
    Posts:
    37
    Ah thank you very much for clarifying Munchy!
    I just encountered another issue related to this, not on this specific object though.
    The object I'm having trouble with now is an instance I already have in my scene prior to starting the game. This means I gave it a photonview through the prefab, and I never call PhotonNetwork.InstantiateSceneObject, but other than that I call it as a scene object. Players can interact with it (move it around) and it all works, up until a new client comes in and all the "changes" don't load. Ah, so another case with InstantiationData right?

    But how am I supposed to load these in on a Scene object of which I never call Photon.InstantiateSceneObject on?
    Is this a necessesity to do or is there a workaround I'm not seeing?
     
  6. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,197
    In this instance, I would probably dispense with PhotonView and use RaiseEvent, that's how I handle objects that are built into the scene but need to be networked, along the lines that I talk about here.
     
    Avalin likes this.