Destroy scene object when leaving room

Options
I'm trying to destroy a local game object that was instantiated via Photon when I leave a room. The use case is this:

I start the game, and I join an initial room with no one else in it. There is also a single monster in this room, that is created via PhotonNetwork.InstantiateSceneObject (ignore for a moment the fact that I'm the only one in the room, but am instantiating via Photon -- eventually, there will be random people in the initial room). The PhotonView associated with the monster is set to "Takeover."

When I click a "party" button (i.e., join a party), I leave my current room and join the "party" room. This room might already exist with one or more players in it, or I might be creating it. Either way, the master client of this room creates a new monster object via PhotonNetwork.InstantiateSceneObject when the room is created.

All of this is working fine, so far, but there's an order of operations issue that's causing multiple monsters to exist:
  1. I join the "party" room, and am the first player there. I am the master client of the room, and PhotonNetwork.InstantiateSceneObject is called to create the room's monster.
  2. Bob joins the "party" room, and is not the master client. The monster that was instantiated in step 1 is there, and we fight it together.
  3. I leave the "party" room, and re-join my initial room. Bob becomes the master client of the "party" room, and on his screen, the monster is still there. However, when I try to call PhotonNetwork.Destroy on the monster (which I do in OnLeftRoom), I can't destroy it, because Photon has transferred control of it to Bob (due to the "Takeover" functionality on the PhotonView).
  4. Back in my initial room, I now see two copies of the monster: The monster created when I joined the "party" room (that I can't destroy, since it is owned by Bob), and the monster that was created when I re-joined my initial room.

Even doing a DestroyImmediate on the object in OnLeftRoom doesn't seem to remove it from my local client. Does Photon have functionality to deal with this automatically, or am I thinking about this incorrectly?

Comments

  • Tobias
    Options
    I just had the time to play this through in the new Ownership Transfer demo (load DemoChangeOwner-Scene) and it worked for me in PUN 1.50.3. Maybe you just need to update.

    To test this, I entered a 1 in the script "Instantiate Cube" as Instantiate Type. Then a click on the yellow quad will use InstantiateSceneObject(). I also added a small button that allows me to leave the room (which is usually not in that demo). Run this two times. The "One" creates the GO as scene object. "Two" joins and "One" leaves. With the latest fixes, the instantiated GO will be destroyed in both cases.

    Is that what you need?
  • atraver
    Options
    Thanks for the response, Tobias.

    I was using 1.50, so I was at least a couple of versions behind. It seems as though this fix in 1.50.2 might be what I'm looking for:
    Fixed: When leaving a room, GameObjects created by InstantiateSceneObject() will be cleaned up (destroyed). For this, the PhotonViews now have a new (internal!) value isRuntimeInstantiated. Objects loaded with the scene are not cleaned up (as before).
  • Tobias
    Options
    There is one more fix in v1.50.3 (current version). And there will be yet another minor update soon.
    The fix you need in that case was in v1.50.2.