2

In our multiplayer project on Unity, each user imports model at runtime, then joins room, and then configures runtime imported object as networked object. The problem is, the imported model gets destroyed when host leaves. Here is my code snippet:

 private void Start()
    {
        pView = GetComponent<PhotonView>();
        pView.ownershipTransfer = OwnershipOption.Takeover;
        pView.ObservedComponents = new List<Component>();

        photonTransformView = transform.GetComponent<PhotonTransformView>();
        pView.ObservedComponents.Add(photonTransformView);
        pView.synchronization = ViewSynchronization.UnreliableOnChange;

        photonTransformView.m_PositionModel.SynchronizeEnabled = true;
        photonTransformView.m_RotationModel.SynchronizeEnabled = true;

        if (GetComponent<OVRGrabbable>() != null)
            transform.GetComponent<OVRGrabbable>().runtimeObj = this;
        partInfo = transform.GetComponent<PartInfo>();
        if (partInfo)
            partInfo.dynamicDync = this;

        if (PhotonNetwork.isMasterClient)
        {
            int objId = PhotonNetwork.AllocateViewID();
            // pView.viewID = objId;
            //TakeOwnership(PhotonNetwork.player);

            MSetPhotonViewId(PhotonNetwork.player.ID, gameObject.name, objId);
        }
    }

    public void MSetPhotonViewId(int id, string objName, int objId)
    {
        //pView.RPC("SetOwnerForObject", PhotonTargets.Others, id);
        object[] content = new object[3];
        content[0] = id;
        content[1] = objName;
        content[2] = objId;
        PhotonNetwork.RaiseEvent(ownerId, content, true, new RaiseEventOptions() { Receivers = ReceiverGroup.All, CachingOption = EventCaching.AddToRoomCache });
    }

    private void OnEvent(byte eventcode, object content, int senderid)
    {
        if (eventcode == ownerId)
        {
            object[] data = (object[])content;
            if (gameObject.name == (string)data[1])
            {
                if (!pView)
                    StartCoroutine(MGetPhotonViewId(content, senderid));
                else
                {
                    pView.viewID = (int)data[2];
                    //Debug.Log("transfering ownership of: " + gameObject.name + " to: " + ((int)data[0]).ToString());
                    pView.TransferOwnership((int)data[0]);
                }
            }
        }
    }

    IEnumerator MGetPhotonViewId(object content, int senderid)
    {
        while (!pView)
        {
            yield return null;
        }

        object[] data = (object[])content;
        if (gameObject.name == (string)data[1])
        {
            pView.viewID = (int)data[2];
            //Debug.Log("transfering ownership of: " + gameObject.name + " to: " + ((int)data[0]).ToString());
            pView.TransferOwnership((int)data[0]);
        }
    }

How do I avoid object getting destroyed on other systems, when host leaves?

1 Answers1

0

When a player quits a game, usually his/her actions are no longer relevant for new players. To avoid congestion on join, Photon server by default automatically cleans up events that have been cached by a player, that has left the room for good.

If you want to manually clean up the rooms' event cache you can create rooms with RoomOptions.CleanupCacheOnLeave set to false.

See the docs.

Tobias
  • 164
  • 2
  • Hi Tobias, thanks for sharing solution. I had solved problem by disabling 'autoCleanUpPlayerObjects'. But as a consequence I am facing this problem: https://stackoverflow.com/questions/55958661/transfer-ownership-after-leaving-multiplayer-room – Harsh Priyadarshi May 03 '19 at 03:54